From 25492a5b59c5b74328278f195540e318ab87674f Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 21 Oct 2020 10:29:14 +0900 Subject: [PATCH] bpo-41902: Micro optimization for compute_item of range (GH-22492) --- .../2020-10-02-13-32-05.bpo-41902.ZKTxzW.rst | 3 +++ Objects/rangeobject.c | 16 +++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-10-02-13-32-05.bpo-41902.ZKTxzW.rst 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 00000000000..b118a6a36fa --- /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 ba6d4257174..eaa48d5f44f 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; }