bpo-42161: Hoist the _PyLong_GetOne() call out of the inner loop. (GH-30656)

This commit is contained in:
Raymond Hettinger 2022-01-18 02:02:35 -06:00 committed by GitHub
parent a287b31bcb
commit 243c31667c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View File

@ -1375,7 +1375,7 @@ class SizeofTest(unittest.TestCase):
x = codecs.charmap_build(encodings.iso8859_3.decoding_table) x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
check(x, size('32B2iB')) check(x, size('32B2iB'))
# enumerate # enumerate
check(enumerate([]), size('n3P')) check(enumerate([]), size('n4P'))
# reverse # reverse
check(reversed(''), size('nP')) check(reversed(''), size('nP'))
# float # float

View File

@ -16,9 +16,10 @@ class reversed "reversedobject *" "&PyReversed_Type"
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
Py_ssize_t en_index; /* current index of enumeration */ 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_result; /* result tuple */
PyObject* en_longindex; /* index for sequences >= PY_SSIZE_T_MAX */ PyObject* en_longindex; /* index for sequences >= PY_SSIZE_T_MAX */
PyObject* one; /* borrowed reference */
} enumobject; } enumobject;
@ -78,6 +79,7 @@ enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start)
Py_DECREF(en); Py_DECREF(en);
return NULL; return NULL;
} }
en->one = _PyLong_GetOne(); /* borrowed reference */
return (PyObject *)en; return (PyObject *)en;
} }
@ -157,7 +159,7 @@ enum_next_long(enumobject *en, PyObject* next_item)
} }
next_index = en->en_longindex; next_index = en->en_longindex;
assert(next_index != NULL); assert(next_index != NULL);
stepped_up = PyNumber_Add(next_index, _PyLong_GetOne()); stepped_up = PyNumber_Add(next_index, en->one);
if (stepped_up == NULL) { if (stepped_up == NULL) {
Py_DECREF(next_item); Py_DECREF(next_item);
return NULL; return NULL;