Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f63f525

Browse files
eendebakptblurb-it[bot]mdickinson
authored
gh-100726: Optimize construction of range object for medium sized integers (#100810)
Use C long arithmetic instead of PyLong arithmetic to compute the range length, where possible. Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Mark Dickinson <[email protected]>
1 parent b4e11a7 commit f63f525

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Lib/test/test_range.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ def test_range_iterators(self):
542542
for start in limits
543543
for end in limits
544544
for step in (-2**63, -2**31, -2, -1, 1, 2)]
545+
test_ranges += [(-2**63, 2**63-2, 1)] # regression test for gh-100810
545546

546547
for start, end, step in test_ranges:
547548
iter1 = range(start, end, step)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimize construction of ``range`` object for medium size integers.

Objects/rangeobject.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,49 @@ range_dealloc(rangeobject *r)
171171
PyObject_Free(r);
172172
}
173173

174+
static unsigned long
175+
get_len_of_range(long lo, long hi, long step);
176+
177+
/* Return the length as a long, -2 for an overflow and -1 for any other type of error
178+
*
179+
* In case of an overflow no error is set
180+
*/
181+
static long compute_range_length_long(PyObject *start,
182+
PyObject *stop, PyObject *step) {
183+
int overflow = 0;
184+
185+
long long_start = PyLong_AsLongAndOverflow(start, &overflow);
186+
if (overflow) {
187+
return -2;
188+
}
189+
if (long_start == -1 && PyErr_Occurred()) {
190+
return -1;
191+
}
192+
long long_stop = PyLong_AsLongAndOverflow(stop, &overflow);
193+
if (overflow) {
194+
return -2;
195+
}
196+
if (long_stop == -1 && PyErr_Occurred()) {
197+
return -1;
198+
}
199+
long long_step = PyLong_AsLongAndOverflow(step, &overflow);
200+
if (overflow) {
201+
return -2;
202+
}
203+
if (long_step == -1 && PyErr_Occurred()) {
204+
return -1;
205+
}
206+
207+
unsigned long ulen = get_len_of_range(long_start, long_stop, long_step);
208+
if (ulen > (unsigned long)LONG_MAX) {
209+
/* length too large for a long */
210+
return -2;
211+
}
212+
else {
213+
return (long)ulen;
214+
}
215+
}
216+
174217
/* Return number of items in range (lo, hi, step) as a PyLong object,
175218
* when arguments are PyLong objects. Arguments MUST return 1 with
176219
* PyLong_Check(). Return NULL when there is an error.
@@ -191,6 +234,21 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
191234
PyObject *zero = _PyLong_GetZero(); // borrowed reference
192235
PyObject *one = _PyLong_GetOne(); // borrowed reference
193236

237+
assert(PyLong_Check(start));
238+
assert(PyLong_Check(stop));
239+
assert(PyLong_Check(step));
240+
241+
/* fast path when all arguments fit into a long integer */
242+
long len = compute_range_length_long(start, stop, step);
243+
if (len >= 0) {
244+
return PyLong_FromLong(len);
245+
}
246+
else if (len == -1) {
247+
/* unexpected error from compute_range_length_long, we propagate to the caller */
248+
return NULL;
249+
}
250+
assert(len == -2);
251+
194252
cmp_result = PyObject_RichCompareBool(step, zero, Py_GT);
195253
if (cmp_result == -1)
196254
return NULL;

0 commit comments

Comments
 (0)