From bf88016b8113aa1ce57b26b1902ab7d770739cb7 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 7 Mar 2024 23:26:00 +0100 Subject: [PATCH 01/11] Add fast path for single-argument range --- Objects/rangeobject.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index ce9eef69ad75a8..e0df4bbca760b3 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -240,6 +240,16 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step) assert(PyLong_Check(stop)); assert(PyLong_Check(step)); + /* fast path for one argument case of range */ + if (start == zero && step == one) { + if (_PyLong_IsPositive( (const PyLongObject *)stop) ) { + return Py_NewRef(stop); + } + else { + return zero; + } + } + /* fast path when all arguments fit into a long integer */ long len = compute_range_length_long(start, stop, step); if (len >= 0) { @@ -1135,13 +1145,19 @@ range_iter(PyObject *seq) /* If all three fields and the length convert to long, use the int * version */ - lstart = PyLong_AsLong(r->start); - if (lstart == -1 && PyErr_Occurred()) { + lstop = PyLong_AsLong(r->stop); + if (lstop == -1 && PyErr_Occurred()) { PyErr_Clear(); goto long_range; } - lstop = PyLong_AsLong(r->stop); - if (lstop == -1 && PyErr_Occurred()) { + + if (r->start == _PyLong_GetZero() && r->step == _PyLong_GetOne() ) { + /* fast path for one argument range */ + return fast_range_iter(0, lstop, 1, Py_MAX(0, (long)lstop)); + } + + lstart = PyLong_AsLong(r->start); + if (lstart == -1 && PyErr_Occurred()) { PyErr_Clear(); goto long_range; } From 89735183ab41e47171ab9b52428f00f454523743 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 7 Mar 2024 22:38:11 +0000 Subject: [PATCH 02/11] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst 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..dc1a29ec43728d --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst @@ -0,0 +1 @@ +Improve performance of the ``range`` function. From 24d9b9aa03b8d81739dfb9f1a23ae091a4a5d400 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 8 Mar 2024 15:07:33 +0100 Subject: [PATCH 03/11] Update Misc/NEWS.d/next/Core and Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst Co-authored-by: Serhiy Storchaka --- .../2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index dc1a29ec43728d..79c13505fcb974 100644 --- 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 @@ -1 +1,2 @@ -Improve performance of the ``range`` function. +Speed up the :class:`range` object constructor and the :class:`!range` +iterator constructor in the case of a single argument. From cbbf2118dbed992cb10f487cb39054d8a2ed00ce Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 8 Mar 2024 15:12:02 +0100 Subject: [PATCH 04/11] whitespace --- Objects/rangeobject.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index e0df4bbca760b3..aafe49c36c5cb1 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -243,10 +243,10 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step) /* fast path for one argument case of range */ if (start == zero && step == one) { if (_PyLong_IsPositive( (const PyLongObject *)stop) ) { - return Py_NewRef(stop); + return Py_NewRef(stop); } else { - return zero; + return zero; } } @@ -1152,8 +1152,8 @@ range_iter(PyObject *seq) } if (r->start == _PyLong_GetZero() && r->step == _PyLong_GetOne() ) { - /* fast path for one argument range */ - return fast_range_iter(0, lstop, 1, Py_MAX(0, (long)lstop)); + /* fast path for one argument range */ + return fast_range_iter(0, lstop, 1, Py_MAX(0, (long)lstop)); } lstart = PyLong_AsLong(r->start); From b26893bc7cc38e02c1748b89e74a61e7c54636a4 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 8 Mar 2024 15:52:31 +0100 Subject: [PATCH 05/11] Update Objects/rangeobject.c Co-authored-by: Serhiy Storchaka --- Objects/rangeobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index aafe49c36c5cb1..9d28d035441709 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -242,7 +242,7 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step) /* fast path for one argument case of range */ if (start == zero && step == one) { - if (_PyLong_IsPositive( (const PyLongObject *)stop) ) { + if (_PyLong_IsPositive((const PyLongObject *)stop)) { return Py_NewRef(stop); } else { From a1102763e5df95d6c0f491be3da3a2a0a51184fc Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 8 Mar 2024 17:26:36 +0100 Subject: [PATCH 06/11] Apply suggestions from code review --- Objects/rangeobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 9d28d035441709..504795666c5dbe 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -1153,7 +1153,7 @@ range_iter(PyObject *seq) if (r->start == _PyLong_GetZero() && r->step == _PyLong_GetOne() ) { /* fast path for one argument range */ - return fast_range_iter(0, lstop, 1, Py_MAX(0, (long)lstop)); + return fast_range_iter(0, lstop, 1, Py_MAX(0, lstop)); } lstart = PyLong_AsLong(r->start); From 7462d777a3d213032df37a4f264caf0a95bf7f53 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 4 Apr 2024 10:26:10 +0200 Subject: [PATCH 07/11] Update Objects/rangeobject.c Co-authored-by: Erlend E. Aasland --- Objects/rangeobject.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 504795666c5dbe..4f802d1c613c1c 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -245,9 +245,7 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step) if (_PyLong_IsPositive((const PyLongObject *)stop)) { return Py_NewRef(stop); } - else { - return zero; - } + return zero; } /* fast path when all arguments fit into a long integer */ From f1f9bd6aa0adfd6e639c5e2dac57c77d036016c3 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 23 May 2024 22:43:25 +0200 Subject: [PATCH 08/11] Update Objects/rangeobject.c Co-authored-by: Serhiy Storchaka --- Objects/rangeobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index b25dfb6dce6afc..6fedc585a10fbd 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -1149,7 +1149,7 @@ range_iter(PyObject *seq) goto long_range; } - if (r->start == _PyLong_GetZero() && r->step == _PyLong_GetOne() ) { + if (r->start == _PyLong_GetZero() && r->step == _PyLong_GetOne()) { /* fast path for one argument range */ return fast_range_iter(0, lstop, 1, Py_MAX(0, lstop)); } From 903c7abed495abfb2da1b368cc39a41e3671f782 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 10 Jun 2024 21:00:12 +0200 Subject: [PATCH 09/11] revert iter optimization --- Objects/rangeobject.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 144977b13cf9cb..f9086ed9f71312 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -1143,19 +1143,13 @@ range_iter(PyObject *seq) /* If all three fields and the length convert to long, use the int * version */ - lstop = PyLong_AsLong(r->stop); - if (lstop == -1 && PyErr_Occurred()) { + lstart = PyLong_AsLong(r->start); + if (lstart == -1 && PyErr_Occurred()) { PyErr_Clear(); goto long_range; } - - if (r->start == _PyLong_GetZero() && r->step == _PyLong_GetOne()) { - /* fast path for one argument range */ - return fast_range_iter(0, lstop, 1, Py_MAX(0, lstop)); - } - - lstart = PyLong_AsLong(r->start); - if (lstart == -1 && PyErr_Occurred()) { + lstop = PyLong_AsLong(r->stop); + if (lstop == -1 && PyErr_Occurred()) { PyErr_Clear(); goto long_range; } From 732a90b189e1ff7e2ba5e460367e969f685fddb6 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 11 Jul 2024 23:36:58 +0200 Subject: [PATCH 10/11] update news entry --- .../2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 index 79c13505fcb974..420912ace188fa 100644 --- 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 @@ -1,2 +1 @@ -Speed up the :class:`range` object constructor and the :class:`!range` -iterator constructor in the case of a single argument. +Speed up the :class:`range` object constructor in the case of a single argument. From 7b1e6f973fb1c68493a87256eaefda450b333f5c Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 11 Feb 2025 10:52:17 +0100 Subject: [PATCH 11/11] lint --- .../2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/{Core and Builtins => Core_and_Builtins}/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst (100%) 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 similarity index 100% rename from Misc/NEWS.d/next/Core and Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst rename to Misc/NEWS.d/next/Core_and_Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst