-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
bpo-36781: Optimize sum() for bools. #13074
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 1 commit
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 |
|---|---|---|
|
|
@@ -2357,7 +2357,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start) | |
| Assumes all inputs are the same type. If the assumption fails, default | ||
| to the more general routine. | ||
| */ | ||
| if (PyLong_CheckExact(result)) { | ||
| if (PyLong_CheckExact(result) || PyBool_Check(result)) { | ||
| int overflow; | ||
| long i_result = PyLong_AsLongAndOverflow(result, &overflow); | ||
| /* If this already overflowed, don't even enter the loop. */ | ||
|
|
@@ -2373,7 +2373,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start) | |
| return NULL; | ||
| return PyLong_FromLong(i_result); | ||
| } | ||
| if (PyLong_CheckExact(item)) { | ||
| if (PyLong_CheckExact(item) || PyBool_Check(item)) { | ||
| long b = PyLong_AsLongAndOverflow(item, &overflow); | ||
| long x = i_result + b; | ||
| if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) { | ||
|
|
@@ -2419,7 +2419,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start) | |
| Py_DECREF(item); | ||
| continue; | ||
| } | ||
| if (PyLong_CheckExact(item)) { | ||
| if (PyLong_Check(item)) { | ||
|
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. Why is this not
Member
Author
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. Because why not? This extends the optimization to other int subclasses. There is a reason why |
||
| long value; | ||
| int overflow; | ||
| value = PyLong_AsLongAndOverflow(item, &overflow); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.