Skip to content

Commit c0f22fb

Browse files
authored
bpo-41902: Micro optimization for range.index if step is 1 (GH-22479)
1 parent 5f22741 commit c0f22fb

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Micro optimization for range.index if step is 1. Patch by Dong-hee Na.

Objects/rangeobject.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,19 @@ range_index(rangeobject *r, PyObject *ob)
582582
return NULL;
583583

584584
if (contains) {
585-
PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start);
586-
if (tmp == NULL)
585+
PyObject *idx = PyNumber_Subtract(ob, r->start);
586+
if (idx == NULL) {
587587
return NULL;
588+
}
589+
590+
if (r->step == _PyLong_One) {
591+
return idx;
592+
}
593+
588594
/* idx = (ob - r.start) // r.step */
589-
idx = PyNumber_FloorDivide(tmp, r->step);
590-
Py_DECREF(tmp);
591-
return idx;
595+
PyObject *sidx = PyNumber_FloorDivide(idx, r->step);
596+
Py_DECREF(idx);
597+
return sidx;
592598
}
593599

594600
/* object is not in the range */

0 commit comments

Comments
 (0)