You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Restore (or beat) Python 2 performance for arithmetic operations on ints that fit into a single word #101291
In Python 2 ints and longs were different objects, and the design of each was tailored to the different size and use cases.
In Python3 we dropped the distinction, but we also dropped the design for ints that fit into a single word.
We have added various fast paths for "medium" integers (e.g. #89109) but the underlying data structure gets in the way.
We should layout the int/long object so that it supports fast operations for most integers.
Maybe @markshannon can answer that? AFAICT all the commits linked above are his. I know we had someone who was interested in pursuing this further but she had to bow out.
@markshannon Where are we with documentation for this? If it's not documented, do we need to start working to roll this back? I'm not comfortable with this change in rc1 if it's not documented.
I did a little research. It looks like there are two key changes. First, struct _longobject (defined in Include/cpython/longintrepr.h but considered an internal implementation detail) has changed. It used to be
I.e., there was an array ob_digit whose length was abs(ob_size), where sign(ob_size) gave the sign of the overall value.
The new (still internal) representation is as follows:
typedef struct _PyLongValue {
uintptr_t lv_tag; /* Number of digits, sign and flags */
digit ob_digit[1];
} _PyLongValue;
struct _longobject {
PyObject_HEAD
_PyLongValue long_value;
};
and there are new internal macros to determine the number of digits and the sign, and a bunch of internal macros to handle "compact" values (which fit in 1-2 "digits").
There are two new public, unstable APIs to support the concept of "compact" values: PyUnstable_Long_IsCompact and PyUnstable_Long_CompactValue. See https://docs.python.org/3.12/c-api/long.html#c.PyUnstable_Long_IsCompact. (In reality these are implemented as macros, and not intended to be part of any ABI.) Everything else that digs through the internals is defined in Include/internal/pycore_long.h, and requires defining Py_BUILD_CORE.
Details of what the bits in lv_tag mean are intentionally not published -- these are meant to be opaque. Applications that used to dig through ob_digits using ob_size as guidance will break, and have two options: Switch to calling the Python-level APIs int.to_bytes() and int.from_bytes() via PyObject_CallMethod() (see note at https://docs.python.org/3.12/c-api/long.html#c.PyLong_FromString). Or go hard-core, defining Py_BUILD_CORE and importing pycore_long.h. Or, I guess, an intermediate path is to use the new unstable public APIs for dealing with "compact" values and use the slower arbitrary-precision API for non-compact values.
I think in the What's New in 3.12 we should at least mention the change in the struct (calling out that using ob_size and ob_digits is no longer supported) and the new unstable public APIs (and what they're for). I don't think we need to call out the hard-core option, but maybe a reminder about to_bytes() and from_bytes() would be useful (even though that's been in the docs at least since 3.10).
@Yhg1s@markshannon What do you think of this? I volunteer to make a PR for what's new 3.12 along the lines of what I wrote above.
Activity
GH-101291: Refactor the `PyLongObject` struct into object header and …
pythonGH-101291: Refactor the `PyLongObject` struct into object heade…
GH-101291: Rearrange the size bits in PyLongObject (GH-102464)
pythonGH-101291: Rearrange the size bits in PyLongObject (pythonGH-10…
pythonGH-101291: Rearrange the size bits in PyLongObject (pythonGH-10…
GH-101291: Add low level, unstable API for pylong (GH-101685)
GH-101291: Avoid using macros with casts in low-level long API. (#104742
pythonGH-101291: Avoid using macros with casts in low-level long API. (…
[3.12] GH-101291: Avoid using macros with casts in low-level long API. (
Yhg1s commentedon Jul 6, 2023
Has there been any progress in documenting the changes made in Python 3.12? (#101292 (comment))
gvanrossum commentedon Jul 7, 2023
Maybe @markshannon can answer that? AFAICT all the commits linked above are his. I know we had someone who was interested in pursuing this further but she had to bow out.
Yhg1s commentedon Jul 20, 2023
@markshannon Where are we with documentation for this? If it's not documented, do we need to start working to roll this back? I'm not comfortable with this change in rc1 if it's not documented.
gvanrossum commentedon Jul 20, 2023
Mark is at EuroPython. If you are there too you can talk to him. We will get it documented.
gvanrossum commentedon Jul 21, 2023
I did a little research. It looks like there are two key changes. First,
struct _longobject(defined in Include/cpython/longintrepr.h but considered an internal implementation detail) has changed. It used to beI.e., there was an array
ob_digitwhose length wasabs(ob_size), wheresign(ob_size)gave the sign of the overall value.The new (still internal) representation is as follows:
and there are new internal macros to determine the number of digits and the sign, and a bunch of internal macros to handle "compact" values (which fit in 1-2 "digits").
There are two new public, unstable APIs to support the concept of "compact" values:
PyUnstable_Long_IsCompactandPyUnstable_Long_CompactValue. See https://docs.python.org/3.12/c-api/long.html#c.PyUnstable_Long_IsCompact. (In reality these are implemented as macros, and not intended to be part of any ABI.) Everything else that digs through the internals is defined in Include/internal/pycore_long.h, and requires definingPy_BUILD_CORE.Details of what the bits in
lv_tagmean are intentionally not published -- these are meant to be opaque. Applications that used to dig throughob_digitsusingob_sizeas guidance will break, and have two options: Switch to calling the Python-level APIsint.to_bytes()andint.from_bytes()viaPyObject_CallMethod()(see note at https://docs.python.org/3.12/c-api/long.html#c.PyLong_FromString). Or go hard-core, definingPy_BUILD_COREand importing pycore_long.h. Or, I guess, an intermediate path is to use the new unstable public APIs for dealing with "compact" values and use the slower arbitrary-precision API for non-compact values.I think in the What's New in 3.12 we should at least mention the change in the struct (calling out that using
ob_sizeandob_digitsis no longer supported) and the new unstable public APIs (and what they're for). I don't think we need to call out the hard-core option, but maybe a reminder aboutto_bytes()andfrom_bytes()would be useful (even though that's been in the docs at least since 3.10).@Yhg1s @markshannon What do you think of this? I volunteer to make a PR for what's new 3.12 along the lines of what I wrote above.
GH-101291: Add warning to "what's new" that `PyLongObject` internals …
pythonGH-101291: Add warning to "what's new" that `PyLongObject` inte…
[3.12] GH-101291: Add warning to "what's new" that `PyLongObject` int…
gvanrossum commentedon Jul 31, 2023
@Yhg1s Assuming the changes Mark made to what's new in 3.12 are what you wanted?
Yhg1s commentedon Jul 31, 2023
Yep, that's adequate.
gh-101291: Add versionadded directives for PyUnstable_Long_* (#125384)
pythongh-101291: Add versionadded directives for PyUnstable_Long_* (p…
pythongh-101291: Add versionadded directives for PyUnstable_Long_* (p…
[3.13] gh-101291: Add versionadded directives for PyUnstable_Long_* (G…
[3.12] gh-101291: Add versionadded directives for PyUnstable_Long_* (G…