diff options
| author | Dong-hee Na <donghee.na92@gmail.com> | 2020-10-21 11:29:56 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-21 11:29:56 +0900 |
| commit | c0f22fb8b3006936757cebb959cee94e285bc503 (patch) | |
| tree | e59ec67aa4c7735f6f9c2a56a470b9ceb6db94d4 /Objects/rangeobject.c | |
| parent | 5f227413400c4dfdba210cc0f8c9305421638bc1 (diff) | |
| download | cpython-git-c0f22fb8b3006936757cebb959cee94e285bc503.tar.gz | |
bpo-41902: Micro optimization for range.index if step is 1 (GH-22479)
Diffstat (limited to 'Objects/rangeobject.c')
| -rw-r--r-- | Objects/rangeobject.c | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index eaa48d5f44..babf55b108 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 */ |
