Skip to content

Commit

Permalink
bpo-42161: Hoist the _PyLong_GetOne() call out of the inner loop. (GH…
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger committed Jan 18, 2022
1 parent a287b31 commit 243c316
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Lib/test/test_sys.py
Expand Up @@ -1375,7 +1375,7 @@ class C(object): pass
x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
check(x, size('32B2iB'))
# enumerate
check(enumerate([]), size('n3P'))
check(enumerate([]), size('n4P'))
# reverse
check(reversed(''), size('nP'))
# float
Expand Down
6 changes: 4 additions & 2 deletions Objects/enumobject.c
Expand Up @@ -16,9 +16,10 @@ class reversed "reversedobject *" "&PyReversed_Type"
typedef struct {
PyObject_HEAD
Py_ssize_t en_index; /* current index of enumeration */
PyObject* en_sit; /* secondary iterator of enumeration */
PyObject* en_sit; /* secondary iterator of enumeration */
PyObject* en_result; /* result tuple */
PyObject* en_longindex; /* index for sequences >= PY_SSIZE_T_MAX */
PyObject* one; /* borrowed reference */
} enumobject;


Expand Down Expand Up @@ -78,6 +79,7 @@ enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start)
Py_DECREF(en);
return NULL;
}
en->one = _PyLong_GetOne(); /* borrowed reference */
return (PyObject *)en;
}

Expand Down Expand Up @@ -157,7 +159,7 @@ enum_next_long(enumobject *en, PyObject* next_item)
}
next_index = en->en_longindex;
assert(next_index != NULL);
stepped_up = PyNumber_Add(next_index, _PyLong_GetOne());
stepped_up = PyNumber_Add(next_index, en->one);
if (stepped_up == NULL) {
Py_DECREF(next_item);
return NULL;
Expand Down

0 comments on commit 243c316

Please sign in to comment.