Skip to content

gh-82088: Improve performance of PyLong_As*() for multi-digit ints #135585

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
test
  • Loading branch information
eendebakpt committed Jun 17, 2025
commit 750c676dcb5c0e8917c993c8b5b1866ae117f720
50 changes: 31 additions & 19 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ PyLong_FromDouble(double dval)
#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)

static inline unsigned long
_unroll_digits(PyLongObject *v, Py_ssize_t *i)
unroll_digits_ulong(PyLongObject *v, Py_ssize_t *i)
{
digit *digits = v->long_value.ob_digit;
assert(*i >= 2);
Expand All @@ -518,6 +518,26 @@ _unroll_digits(PyLongObject *v, Py_ssize_t *i)
return x;
}

static inline size_t
unroll_digits_size_t(PyLongObject *v, Py_ssize_t *i)
{
digit *digits = v->long_value.ob_digit;
assert(*i >= 2);
/* unroll 1 digit */
--(*i);
assert(SIZE_MAX >= ((1UL << PyLong_SHIFT) - 1));
size_t x = digits[*i];

#if ( (SIZE_MAX >> PyLong_SHIFT) >= ( ( 1 << PyLong_SHIFT) - 1) )
/* unroll another digit */
x <<= PyLong_SHIFT;
--(*i);
x |= digits[*i];
#endif

return x;
}

/* Get a C long int from an int object or any object that has an __index__
method.

Expand All @@ -532,7 +552,6 @@ PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
{
/* This version originally by Tim Peters */
PyLongObject *v;
unsigned long x;
long res;
Py_ssize_t i;
int sign;
Expand Down Expand Up @@ -576,7 +595,7 @@ PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
i = _PyLong_DigitCount(v);
sign = _PyLong_NonCompactSign(v);

x = _unroll_digits(v, &i);
unsigned long x = unroll_digits_ulong(v, &i);
while (--i >= 0) {
if (x > (ULONG_MAX >> PyLong_SHIFT)) {
*overflow = sign;
Expand Down Expand Up @@ -646,7 +665,6 @@ PyLong_AsInt(PyObject *obj)
Py_ssize_t
PyLong_AsSsize_t(PyObject *vv) {
PyLongObject *v;
size_t x;
Py_ssize_t i;
int sign;

Expand All @@ -666,7 +684,7 @@ PyLong_AsSsize_t(PyObject *vv) {
i = _PyLong_DigitCount(v);
sign = _PyLong_NonCompactSign(v);

x = _unroll_digits(v, &i);
size_t x = unroll_digits_size_t(v, &i);
while (--i >= 0) {
if (x > SIZE_MAX >> PyLong_SHIFT) {
goto overflow;
Expand Down Expand Up @@ -697,7 +715,6 @@ unsigned long
PyLong_AsUnsignedLong(PyObject *vv)
{
PyLongObject *v;
unsigned long x;
Py_ssize_t i;

if (vv == NULL) {
Expand Down Expand Up @@ -729,9 +746,9 @@ PyLong_AsUnsignedLong(PyObject *vv)
}
i = _PyLong_DigitCount(v);

x = _unroll_digits(v, &i);
unsigned long x = unroll_digits_ulong(v, &i);
while (--i >= 0) {
if (x > SIZE_MAX >> PyLong_SHIFT) {
if (x > ULONG_MAX >> PyLong_SHIFT) {
goto overflow;
}
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
Expand All @@ -751,7 +768,6 @@ size_t
PyLong_AsSize_t(PyObject *vv)
{
PyLongObject *v;
size_t x;
Py_ssize_t i;

if (vv == NULL) {
Expand All @@ -774,7 +790,7 @@ PyLong_AsSize_t(PyObject *vv)
}
i = _PyLong_DigitCount(v);

x = _unroll_digits(v, &i);
size_t x = unroll_digits_size_t(v, &i);
while (--i >= 0) {
if (x > SIZE_MAX >> PyLong_SHIFT) {
PyErr_SetString(PyExc_OverflowError,
Expand All @@ -793,7 +809,6 @@ static unsigned long
_PyLong_AsUnsignedLongMask(PyObject *vv)
{
PyLongObject *v;
unsigned long x;
Py_ssize_t i;

if (vv == NULL || !PyLong_Check(vv)) {
Expand All @@ -810,7 +825,7 @@ _PyLong_AsUnsignedLongMask(PyObject *vv)
}
i = _PyLong_DigitCount(v);
int sign = _PyLong_NonCompactSign(v);
x = _unroll_digits(v, &i);
unsigned long x = unroll_digits_ulong(v, &i);
while (--i >= 0) {
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
}
Expand Down Expand Up @@ -1611,7 +1626,6 @@ static unsigned long long
_PyLong_AsUnsignedLongLongMask(PyObject *vv)
{
PyLongObject *v;
unsigned long long x;
Py_ssize_t i;
int sign;

Expand All @@ -1629,7 +1643,7 @@ _PyLong_AsUnsignedLongLongMask(PyObject *vv)
}
i = _PyLong_DigitCount(v);
sign = _PyLong_NonCompactSign(v);
x = _unroll_digits(v, &i);
unsigned long long x = unroll_digits_ulong(v, &i);
while (--i >= 0) {
x = (x << PyLong_SHIFT) | v->long_value.ob_digit[i];
}
Expand Down Expand Up @@ -1675,7 +1689,6 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow)
{
/* This version by Tim Peters */
PyLongObject *v;
unsigned long long x, prev;
long long res;
Py_ssize_t i;
int sign;
Expand Down Expand Up @@ -1717,15 +1730,14 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow)
else {
i = _PyLong_DigitCount(v);
sign = _PyLong_NonCompactSign(v);
x = 0;
unsigned long long x = unroll_digits_ulong(v, &i);
while (--i >= 0) {
prev = x;
x = (x << PyLong_SHIFT) + v->long_value.ob_digit[i];
if ((x >> PyLong_SHIFT) != prev) {
if (x > ULLONG_MAX >> PyLong_SHIFT) {
*overflow = sign;
res = -1;
goto exit;
}
x = (x << PyLong_SHIFT) + v->long_value.ob_digit[i];
}
/* Haven't lost any bits, but casting to long requires extra
* care (see comment above).
Expand Down
Loading