Skip to content

gh-162150: Use full PyLongObject space in freelist - #162173

Open
cmaloney wants to merge 4 commits into
python:mainfrom
cmaloney:int_60bit_freelist
Open

cmaloney wants to merge 4 commits into
python:mainfrom
cmaloney:int_60bit_freelist

Conversation

@cmaloney

@cmaloney cmaloney commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

The freelist was hardcoded to only allow one digit int. Each digit is 32 bits and can hold 30 bits of data. ob_digit is the last member and sizeof(PyLongObject) actually has space for two on 64 bit machines.

Refactor to add a MAX_DIGITS_FOR_FREELIST which computes at compile time what fits and use the freelist for all PyLongObject that fit. For 64 bit platforms that means 2-digit int now use the freelist while taking no extra space.

Each digit stores 30 bits so this still doesn't reach platform integer length on 64 bit (2 * 30 < 64). Adding another digit would change the allocation class unfortunately so need other approaches.

PGO+LTO, upstream/main b90572a vs this change, mean ns:

statement main branch delta
s + s (1 digit + 1 digit -> 2) 17.2 13.7 -3.4
s * t (1 digit * 1 digit -> 2) 16.8 13.6 -3.2
a - b (2 digits) 16.9 14.0 -2.8
a // 7 20.8 17.7 -3.1
a & b 16.1 13.1 -2.9
a >> 3 17.6 14.6 -3.0
id(m) 17.2 14.5 -2.7
int('1234567890123') 39.2 34.6 -4.6
time.time_ns() 35.1 30.8 -4.4
time.perf_counter_ns() 34.9 29.3 -5.6
sum(range(2**31, 2**31 + 1000)) 9189.7 6125.9 -3063.8
str(5) 25.1 21.5 -3.5
str(123456789) 29.8 26.8 -3.0
f'{n}' (1 digit) 30.0 25.6 -4.4
'%d' % n 50.2 46.0 -4.3
b'%d' % n 38.2 35.3 -2.9
a + b (allocates 3 digits) 16.9 15.8 -1.1
a * 3 (allocates 3 digits) 20.9 21.0 +0.2
a << 1 (allocates 3 digits) 19.1 18.3 -0.9
str(2**62) (3-digit scratch) 35.0 35.2 +0.2
m + k (1 digit, control) 9.6 9.0 -0.5
c + d (3 digits, control) 16.0 16.6 +0.7
str(10**40) (control) 113.6 113.0 -0.6

pyperformance, one pass per side:

Benchmark main branch
crypto_pyaes 34.5 ms 31.3 ms: 1.10x faster
mako 6.26 ms 6.05 ms: 1.03x faster
json_dumps 4.33 ms 4.21 ms: 1.03x faster
pprint_safe_repr 364 ms 353 ms: 1.03x faster
pprint_pformat 739 ms 721 ms: 1.02x faster
deepcopy 110 us 109 us: 1.01x faster
pathlib 9.12 ms 9.26 ms: 1.02x slower
chameleon 4.60 ms 4.69 ms: 1.02x slower
telco (control) 3.49 ms 3.38 ms: 1.03x faster
pyflate (control) 205 ms 199 ms: 1.03x faster
richards (control) 21.1 ms 20.8 ms: 1.01x faster
spectral_norm (control) 45.1 ms 44.7 ms: 1.01x faster
pidigits (control) 109 ms 110 ms: 1.01x slower
tomli_loads (control) not significant

The freelist was hardcoded to only allow one digit ints. `ob_digit` is
the last member and `sizeof(PyLongObject)` actually has space for two on
64 bit machines.

Refactor to add a MAX_DIGITS_FOR_FREELIST which computes what fits and
use the freelist for all PyLongObject. For 64 bit platforms that means
2-digit `int` now use the freelist while taking no extra space.

Each digit stores 30 bits so this still doesn't reach platform integer
length on 64 bit (2 * 30 < 64). Adding another digit would change the
allocation class unfortunately so need other approaches.

PGO+LTO, upstream/main b90572a vs this change, mean ns:

| statement                         | main   | branch | delta   |
|-----------------------------------|-------:|-------:|--------:|
| `s + s` (1 digit + 1 digit -> 2)  | 17.2   | 13.7   | -3.4    |
| `s * t` (1 digit * 1 digit -> 2)  | 16.8   | 13.6   | -3.2    |
| `a - b` (2 digits)                | 16.9   | 14.0   | -2.8    |
| `a // 7`                          | 20.8   | 17.7   | -3.1    |
| `a & b`                           | 16.1   | 13.1   | -2.9    |
| `a >> 3`                          | 17.6   | 14.6   | -3.0    |
| `id(m)`                           | 17.2   | 14.5   | -2.7    |
| `int('1234567890123')`            | 39.2   | 34.6   | -4.6    |
| `time.time_ns()`                  | 35.1   | 30.8   | -4.4    |
| `time.perf_counter_ns()`          | 34.9   | 29.3   | -5.6    |
| `sum(range(2**31, 2**31 + 1000))` | 9189.7 | 6125.9 | -3063.8 |
| `str(5)`                          | 25.1   | 21.5   | -3.5    |
| `str(123456789)`                  | 29.8   | 26.8   | -3.0    |
| `f'{n}'` (1 digit)                | 30.0   | 25.6   | -4.4    |
| `'%d' % n`                        | 50.2   | 46.0   | -4.3    |
| `b'%d' % n`                       | 38.2   | 35.3   | -2.9    |
| `a + b` (allocates 3 digits)      | 16.9   | 15.8   | -1.1    |
| `a * 3` (allocates 3 digits)      | 20.9   | 21.0   | +0.2    |
| `a << 1` (allocates 3 digits)     | 19.1   | 18.3   | -0.9    |
| `str(2**62)` (3-digit scratch)    | 35.0   | 35.2   | +0.2    |
| `m + k` (1 digit, control)        | 9.6    | 9.0    | -0.5    |
| `c + d` (3 digits, control)       | 16.0   | 16.6   | +0.7    |
| `str(10**40)` (control)           | 113.6  | 113.0  | -0.6    |

pyperformance, one pass per side:

| Benchmark        | main    | branch                |
|------------------|:-------:|:---------------------:|
| crypto_pyaes     | 34.5 ms | 31.3 ms: 1.10x faster |
| mako             | 6.26 ms | 6.05 ms: 1.03x faster |
| json_dumps       | 4.33 ms | 4.21 ms: 1.03x faster |
| pprint_safe_repr | 364 ms  | 353 ms: 1.03x faster  |
| pprint_pformat   | 739 ms  | 721 ms: 1.02x faster  |
| deepcopy         | 110 us  | 109 us: 1.01x faster  |
| pathlib          | 9.12 ms | 9.26 ms: 1.02x slower |
| chameleon        | 4.60 ms | 4.69 ms: 1.02x slower |
| telco (control)  | 3.49 ms | 3.38 ms: 1.03x faster |
| pyflate (control) | 205 ms | 199 ms: 1.03x faster  |
| richards (control) | 21.1 ms | 20.8 ms: 1.01x faster |
| spectral_norm (control) | 45.1 ms | 44.7 ms: 1.01x faster |
| pidigits (control) | 109 ms | 110 ms: 1.01x slower |
| tomli_loads (control) | not significant | |

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cmaloney
cmaloney requested a review from eendebakpt November 17, 2026 20:29
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 17, 2026
Comment thread Objects/longobject.c Outdated
size_t nbytes = (offsetof(PyLongObject, long_value.ob_digit) +
ndigits*sizeof(digit));
if (ndigits <= MAX_DIGITS_FOR_FREELIST) {
nbytes = sizeof(PyLongObject);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not obvious at first sight, I think we need a comment (either here or in the comment right above)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eendebakpt

Copy link
Copy Markdown
Contributor

The case a * 3 (allocates 3 digits) not improving is due to the 3-digit allocation. We can fix that with e190841 (in itself the impact is neutral). I will open a PR if this gets merged.

@cmaloney

Copy link
Copy Markdown
Contributor Author

working on a fix for the c analyzer. Could also remove the static assert here, but I'd rather keep it. Parens with more than one comma should be supported. Separate issue and PR (Time to think about this isn't a bad thing / still early in 3.16 dev cycle)

@cmaloney

Copy link
Copy Markdown
Contributor Author

Opened gh-162182 for the c-analyzer failure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants