]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42161: Hoist the _PyLong_GetOne() call out of the inner loop. (GH-30656)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Tue, 18 Jan 2022 08:02:35 +0000 (02:02 -0600)
committerGitHub <noreply@github.com>
Tue, 18 Jan 2022 08:02:35 +0000 (00:02 -0800)
Lib/test/test_sys.py
Objects/enumobject.c

index f05cd75af97b5e5ee705d3972170e0ba2153c77d..2c8c6ab6cee7672bc663b9c25de2a415d5322b86 100644 (file)
@@ -1375,7 +1375,7 @@ class SizeofTest(unittest.TestCase):
         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
index b78230ddaebaaac150b08466959b85a2591fdae0..8fbf4fd6e470b0b658b1a00de11e3d3cfbc5cf90 100644 (file)
@@ -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;
 
 
@@ -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;
 }
 
@@ -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;