]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41902: Micro optimization for range.index if step is 1 (GH-22479)
authorDong-hee Na <donghee.na92@gmail.com>
Wed, 21 Oct 2020 02:29:56 +0000 (11:29 +0900)
committerGitHub <noreply@github.com>
Wed, 21 Oct 2020 02:29:56 +0000 (11:29 +0900)
Misc/NEWS.d/next/Core and Builtins/2020-10-01-22-44-23.bpo-41902.iLoMVF.rst [new file with mode: 0644]
Objects/rangeobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-10-01-22-44-23.bpo-41902.iLoMVF.rst b/Misc/NEWS.d/next/Core and Builtins/2020-10-01-22-44-23.bpo-41902.iLoMVF.rst
new file mode 100644 (file)
index 0000000..738ef5a
--- /dev/null
@@ -0,0 +1 @@
+Micro optimization for range.index if step is 1. Patch by Dong-hee Na.
index eaa48d5f44fcc7dfff2b2d60a7604485c597354d..babf55b108b9abf89e6960ed3a34ac4194ef508f 100644 (file)
@@ -582,13 +582,19 @@ range_index(rangeobject *r, PyObject *ob)
         return NULL;
 
     if (contains) {
-        PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start);
-        if (tmp == NULL)
+        PyObject *idx = PyNumber_Subtract(ob, r->start);
+        if (idx == NULL) {
             return NULL;
+        }
+
+        if (r->step == _PyLong_One) {
+            return idx;
+        }
+
         /* idx = (ob - r.start) // r.step */
-        idx = PyNumber_FloorDivide(tmp, r->step);
-        Py_DECREF(tmp);
-        return idx;
+        PyObject *sidx = PyNumber_FloorDivide(idx, r->step);
+        Py_DECREF(idx);
+        return sidx;
     }
 
     /* object is not in the range */