[Python-checkins] bpo-41902: Micro optimization for range.index if step is 1 (GH-22479)
corona10
webhook-mailer at python.org
Tue Oct 20 22:30:10 EDT 2020
https://github.com/python/cpython/commit/c0f22fb8b3006936757cebb959cee94e285bc503
commit: c0f22fb8b3006936757cebb959cee94e285bc503
branch: master
author: Dong-hee Na <donghee.na92 at gmail.com>
committer: corona10 <donghee.na92 at gmail.com>
date: 2020-10-21T11:29:56+09:00
summary:
bpo-41902: Micro optimization for range.index if step is 1 (GH-22479)
files:
A Misc/NEWS.d/next/Core and Builtins/2020-10-01-22-44-23.bpo-41902.iLoMVF.rst
M Objects/rangeobject.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-10-01-22-44-23.bpo-41902.iLoMVF.rst b/Misc/NEWS.d/next/Core and Builtins/2020-10-01-22-44-23.bpo-41902.iLoMVF.rst
new file mode 100644
index 0000000000000..738ef5aec9503
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-10-01-22-44-23.bpo-41902.iLoMVF.rst
@@ -0,0 +1 @@
+Micro optimization for range.index if step is 1. Patch by Dong-hee Na.
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index eaa48d5f44fcc..babf55b108b9a 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 */
More information about the Python-checkins
mailing list