From: Dong-hee Na Date: Wed, 21 Oct 2020 01:29:14 +0000 (+0900) Subject: bpo-41902: Micro optimization for compute_item of range (GH-22492) X-Git-Tag: v3.10.0a2~128 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=25492a5b59c5b74328278f195540e318ab87674f;p=thirdparty%2FPython%2Fcpython.git bpo-41902: Micro optimization for compute_item of range (GH-22492) --- diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-10-02-13-32-05.bpo-41902.ZKTxzW.rst b/Misc/NEWS.d/next/Core and Builtins/2020-10-02-13-32-05.bpo-41902.ZKTxzW.rst new file mode 100644 index 000000000000..b118a6a36fae --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-10-02-13-32-05.bpo-41902.ZKTxzW.rst @@ -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. diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index ba6d42571749..eaa48d5f44fc 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -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; }