[New-bugs-announce] [issue31187] suboptimal code in Py_ReprEnter()

Oren Milman report at bugs.python.org
Sat Aug 12 06:37:02 EDT 2017

New submission from Oren Milman:

in Objects/object.c, Py_ReprEnter() does the following:
    - try to retrieve the Py_Repr list from the thread-state dict.
    - in case the list is not in the dict, add it to the dict as an empty list.
    - check whether the received object is in the Py_Repr list, even in case the
      list was just created, and guaranteed to be empty.

I propose to put this check inside an else clause, so that it wouldn't take
place in case the list is guaranteed to be empty, i.e.:
    list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
    if (list == NULL) {
        list = PyList_New(0);
    else {
        i = PyList_GET_SIZE(list);
        while (--i >= 0) {
            if (PyList_GET_ITEM(list, i) == obj)
                return 1;

I ran the test suite, and it seems that this change doesn't break anything, so
I would be happy to open a PR for it.

components: Interpreter Core
messages: 300193
nosy: Oren Milman
priority: normal
severity: normal
status: open
title: suboptimal code in Py_ReprEnter()
type: performance
versions: Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list