mirror of https://github.com/python/cpython
bpo-41902: Micro optimization for range.index if step is 1 (GH-22479)
This commit is contained in:
parent
5f22741340
commit
c0f22fb8b3
|
@ -0,0 +1 @@
|
||||||
|
Micro optimization for range.index if step is 1. Patch by Dong-hee Na.
|
|
@ -582,15 +582,21 @@ range_index(rangeobject *r, PyObject *ob)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (contains) {
|
if (contains) {
|
||||||
PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start);
|
PyObject *idx = PyNumber_Subtract(ob, r->start);
|
||||||
if (tmp == NULL)
|
if (idx == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
/* idx = (ob - r.start) // r.step */
|
}
|
||||||
idx = PyNumber_FloorDivide(tmp, r->step);
|
|
||||||
Py_DECREF(tmp);
|
if (r->step == _PyLong_One) {
|
||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* idx = (ob - r.start) // r.step */
|
||||||
|
PyObject *sidx = PyNumber_FloorDivide(idx, r->step);
|
||||||
|
Py_DECREF(idx);
|
||||||
|
return sidx;
|
||||||
|
}
|
||||||
|
|
||||||
/* object is not in the range */
|
/* object is not in the range */
|
||||||
PyErr_Format(PyExc_ValueError, "%R is not in range", ob);
|
PyErr_Format(PyExc_ValueError, "%R is not in range", ob);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Reference in New Issue