https://github.com/python/cpython/commit/32fe0cca23d9e9677107abe4d701cb20768c7926
commit: 32fe0cca23d9e9677107abe4d701cb20768c7926
branch: main
author: Pieter Eendebak <[email protected]>
committer: StanFromIreland <[email protected]>
date: 2026-10-30T12:08:31Z
summary:

gh-116477: Improve performance of `range()` when `start` is 0 and `step` is 1 
(#116478)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Erlend E. Aasland <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst
M Objects/rangeobject.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst
new file mode 100644
index 00000000000000..420912ace188fa
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst
@@ -0,0 +1 @@
+Speed up the :class:`range` object constructor in the case of a single 
argument.
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index 2279d4ce17d2f6..f9347e9563961b 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -259,6 +259,14 @@ compute_range_length(PyObject *start, PyObject *stop, 
PyObject *step)
     assert(PyLong_Check(stop));
     assert(PyLong_Check(step));
 
+    /* fast path for start == 0 and step == 1: length is max(stop, 0) */
+    if (start == zero && step == one) {
+        if (_PyLong_IsPositive((const PyLongObject *)stop)) {
+            return Py_NewRef(stop);
+        }
+        return zero;
+    }
+
     /* fast path when all arguments fit into a long integer */
     long len = compute_range_length_long(start, stop, step);
     if (len >= 0) {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to