summaryrefslogtreecommitdiffstats
path: root/Objects
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 /Objects
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)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/rangeobject.c16
1 files changed, 11 insertions, 5 deletions
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 */