From c0f22fb8b3006936757cebb959cee94e285bc503 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 21 Oct 2020 11:29:56 +0900 Subject: [PATCH] bpo-41902: Micro optimization for range.index if step is 1 (GH-22479) --- .../2020-10-01-22-44-23.bpo-41902.iLoMVF.rst | 1 + Objects/rangeobject.c | 16 +++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-10-01-22-44-23.bpo-41902.iLoMVF.rst 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 00000000000..738ef5aec95 --- /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 eaa48d5f44f..babf55b108b 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 */