bpo-24076: Inline single digit unpacking in the integer fastpath of s… · python/cpython@debd804 (original) (raw)

File tree

2 files changed

lines changed

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 +sum() was further optimised for summing up single digit integers.
Original file line number Diff line number Diff line change
@@ -2479,7 +2479,15 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
2479 2479 return PyLong_FromLong(i_result);
2480 2480 }
2481 2481 if (PyLong_CheckExact(item) |
2482 -long b = PyLong_AsLongAndOverflow(item, &overflow);
2482 +long b;
2483 +overflow = 0;
2484 +/* Single digits are common, fast, and cannot overflow on unpacking. */
2485 +switch (Py_SIZE(item)) {
2486 +case -1: b = -(sdigit) ((PyLongObject*)item)->ob_digit[0]; break;
2487 +case 0: continue;
2488 +case 1: b = ((PyLongObject*)item)->ob_digit[0]; break;
2489 +default: b = PyLong_AsLongAndOverflow(item, &overflow); break;
2490 + }
2483 2491 if (overflow == 0 &&
2484 2492 (i_result >= 0 ? (b <= LONG_MAX - i_result)
2485 2493 : (b >= LONG_MIN - i_result)))