-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
gh-107868 Add an O(1) fastpath for sum(range(...))
#107870
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
Changes from all commits
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 @@ | ||
| Reduce ``sum(range(...))`` from ``O(n)`` to ``O(1)`` time . Patch by Marco Cognetta. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2489,13 +2489,72 @@ This function is intended specifically for use with numeric values and may | |
| reject non-numeric types. | ||
| [clinic start generated code]*/ | ||
|
|
||
| static PyObject* range_sum_fastpath(PyObject* module, PyObject *range) | ||
| { | ||
| PyObject* length = builtin_len(module, range); | ||
|
|
||
| if (PyObject_RichCompareBool(length, PyLong_FromLong(0), Py_EQ)) { | ||
| Py_DECREF(length); | ||
| return PyLong_FromLong(0); | ||
| } | ||
|
|
||
| // compute start * length + step * length * (length - 1) // 2 | ||
| // [ a ] | ||
| // [ b ] | ||
| // [ c ] | ||
| // [ d ] | ||
| // [ e ] | ||
| // [ result ] | ||
|
|
||
| PyObject* start = PyObject_GetAttrString(range, "start"); | ||
| PyObject* step = PyObject_GetAttrString(range, "step"); | ||
|
|
||
| PyObject* one = PyLong_FromLong(1); | ||
| PyObject* a = PyNumber_Subtract(length, one); | ||
|
|
||
| PyObject* b = PyNumber_Multiply(a, length); | ||
|
|
||
| PyObject* two = PyLong_FromLong(2); | ||
| PyObject* c = PyNumber_FloorDivide(b, two); | ||
|
|
||
| PyObject* d = PyNumber_Multiply(step, c); | ||
|
|
||
| PyObject* e = PyNumber_Multiply(length, start); | ||
|
|
||
| PyObject* result = PyNumber_Add(d, e); | ||
|
|
||
| Py_DECREF(length); | ||
| Py_DECREF(start); | ||
| Py_DECREF(step); | ||
|
|
||
| Py_DECREF(one); | ||
| Py_DECREF(a); | ||
| Py_DECREF(b); | ||
| Py_DECREF(two); | ||
| Py_DECREF(c); | ||
| Py_DECREF(d); | ||
| Py_DECREF(e); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| static PyObject * | ||
| builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start) | ||
| /*[clinic end generated code: output=df758cec7d1d302f input=162b50765250d222]*/ | ||
| { | ||
| PyObject *result = start; | ||
| PyObject *temp, *item, *iter; | ||
|
|
||
| if (PyRange_Check(iterable)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (result == NULL) { | ||
| result = PyLong_FromLong(0); | ||
| } | ||
| PyObject* rangesum = range_sum_fastpath(module, iterable); | ||
| result = PyNumber_Add(result, rangesum); | ||
| Py_DECREF(rangesum); | ||
| return result; | ||
| } | ||
|
|
||
| iter = PyObject_GetIter(iterable); | ||
| if (iter == NULL) | ||
| return NULL; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these calls can return
NULL. You should prevent these situations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear, do you mean adding something like:
after every one of these? I would have to do it after
as well then, I think.