-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-116477: Improve performance of range for the single argument case #116478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 15 commits
bf88016
f4d1162
8973518
24d9b9a
cbbf211
b59002c
b26893b
a110276
602501b
1014c0f
cbf8cab
7462d77
70f6f74
05780ea
2c2ae84
f1f9bd6
48da228
73eed5d
f389fb4
a28e2dc
903c7ab
732a90b
7696583
f619889
565d1d8
7b1e6f9
7d2428e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Speed up the :class:`range` object constructor and the :class:`!range` | ||
| iterator constructor in the case of a single argument. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -240,6 +240,14 @@ 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); | ||
| } | ||
| 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 +1143,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, lstop)); | ||
| } | ||
|
|
||
| lstart = PyLong_AsLong(r->start); | ||
| if (lstart == -1 && PyErr_Occurred()) { | ||
| PyErr_Clear(); | ||
| goto long_range; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.