]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41902: Micro optimization for compute_item of range (GH-22492)
authorDong-hee Na <donghee.na92@gmail.com>
Wed, 21 Oct 2020 01:29:14 +0000 (10:29 +0900)
committerGitHub <noreply@github.com>
Wed, 21 Oct 2020 01:29:14 +0000 (10:29 +0900)
Misc/NEWS.d/next/Core and Builtins/2020-10-02-13-32-05.bpo-41902.ZKTxzW.rst [new file with mode: 0644]
Objects/rangeobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-10-02-13-32-05.bpo-41902.ZKTxzW.rst b/Misc/NEWS.d/next/Core and Builtins/2020-10-02-13-32-05.bpo-41902.ZKTxzW.rst
new file mode 100644 (file)
index 0000000..b118a6a
--- /dev/null
@@ -0,0 +1,3 @@
+Micro optimization when compute :c:member:`~PySequenceMethods.sq_item` and
+:c:member:`~PyMappingMethods.mp_subscript` of :class:`range`. Patch by
+Dong-hee Na.
index ba6d4257174950f730b4b7817247f7f0df9a7ed6..eaa48d5f44fcc7dfff2b2d60a7604485c597354d 100644 (file)
@@ -254,11 +254,17 @@ compute_item(rangeobject *r, PyObject *i)
     /* PyLong equivalent to:
      *    return r->start + (i * r->step)
      */
-    incr = PyNumber_Multiply(i, r->step);
-    if (!incr)
-        return NULL;
-    result = PyNumber_Add(r->start, incr);
-    Py_DECREF(incr);
+    if (r->step == _PyLong_One) {
+        result = PyNumber_Add(r->start, i);
+    }
+    else {
+        incr = PyNumber_Multiply(i, r->step);
+        if (!incr) {
+            return NULL;
+        }
+        result = PyNumber_Add(r->start, incr);
+        Py_DECREF(incr);
+    }
     return result;
 }