bpo-41902: Micro optimization for compute_item of range (GH-22492)

This commit is contained in:
Dong-hee Na 2020-10-21 10:29:14 +09:00 committed by GitHub
parent a460d45063
commit 25492a5b59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

@ -0,0 +1,3 @@
Micro optimization when compute :c:member:`~PySequenceMethods.sq_item` and
:c:member:`~PyMappingMethods.mp_subscript` of :class:`range`. Patch by
Dong-hee Na.

View File

@ -254,11 +254,17 @@ compute_item(rangeobject *r, PyObject *i)
/* PyLong equivalent to:
* return r->start + (i * r->step)
*/
incr = PyNumber_Multiply(i, r->step);
if (!incr)
return NULL;
result = PyNumber_Add(r->start, incr);
Py_DECREF(incr);
if (r->step == _PyLong_One) {
result = PyNumber_Add(r->start, i);
}
else {
incr = PyNumber_Multiply(i, r->step);
if (!incr) {
return NULL;
}
result = PyNumber_Add(r->start, incr);
Py_DECREF(incr);
}
return result;
}