summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na92@gmail.com>2020-10-21 01:29:14 (GMT)
committerGitHub <noreply@github.com>2020-10-21 01:29:14 (GMT)
commit25492a5b59c5b74328278f195540e318ab87674f (patch)
tree4a78df2e9db249b2c35eb6d7c21a334f3d355228 /Objects
parenta460d45063844a21c20fa8b0d23878165f99f3b5 (diff)
downloadcpython-25492a5b59c5b74328278f195540e318ab87674f.zip
cpython-25492a5b59c5b74328278f195540e318ab87674f.tar.gz
cpython-25492a5b59c5b74328278f195540e318ab87674f.tar.bz2
bpo-41902: Micro optimization for compute_item of range (GH-22492)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/rangeobject.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index ba6d425..eaa48d5 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;
}