summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na92@gmail.com>2020-10-21 02:29:56 (GMT)
committerGitHub <noreply@github.com>2020-10-21 02:29:56 (GMT)
commitc0f22fb8b3006936757cebb959cee94e285bc503 (patch)
treee59ec67aa4c7735f6f9c2a56a470b9ceb6db94d4
parent5f227413400c4dfdba210cc0f8c9305421638bc1 (diff)
downloadcpython-c0f22fb8b3006936757cebb959cee94e285bc503.zip
cpython-c0f22fb8b3006936757cebb959cee94e285bc503.tar.gz
cpython-c0f22fb8b3006936757cebb959cee94e285bc503.tar.bz2
bpo-41902: Micro optimization for range.index if step is 1 (GH-22479)
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2020-10-01-22-44-23.bpo-41902.iLoMVF.rst1
-rw-r--r--Objects/rangeobject.c16
2 files changed, 12 insertions, 5 deletions
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
index 0000000..738ef5a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-10-01-22-44-23.bpo-41902.iLoMVF.rst
@@ -0,0 +1 @@
+Micro optimization for range.index if step is 1. Patch by Dong-hee Na.
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index eaa48d5..babf55b 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -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 */