gh-126868: Add freelist for compact int objects by eendebakpt · Pull Request #126865 · python/cpython
We can add freelists for the int object to improve performance. Using the new methods from #121934 the amount of code needed for adding a freelist is quite small. We only implement the freelist for compact ints (e.g. a single digit). For multi-digit int objects adding freelists is more complex (we need a size-based freelist) and the gains are smaller (for very large int objects the allocation is not a significant part of the computation time)
bench_long: Mean +- std dev: [main_long] 106 ns +- 5 ns -> [pr_long1c] 99.8 ns +- 4.4 ns: 1.07x faster
bench_alloc: Mean +- std dev: [main_long] 210 us +- 6 us -> [pr_long1c] 177 us +- 10 us: 1.19x faster
Benchmark hidden because not significant (1): bench_collatz
Geometric mean: 1.08x faster
On the pyperformance test suite (actually, a subset of the suite, not all benchmarks run on my system) shows the percentage of successfull freelist allocations increases significantly
Allocations from freelist 2,004,971,371 39.8%
Frees to freelist 2,005,350,418
Allocations 3,034,877,938 60.2%
Allocations to 512 bytes 3,008,791,812 59.7%
Allocations to 4 kbytes 18,648,072 0.4%
Allocations over 4 kbytes 7,438,054 0.1%
Frees 3,142,033,922
Allocations from freelist 3,058,347,887 58.6%
Frees to freelist 3,058,576,117
Allocations 2,159,771,546 41.4%
Allocations to 512 bytes 2,133,373,693 40.9%
Allocations to 4 kbytes 18,802,328 0.4%
Allocations over 4 kbytes 7,595,525 0.1%
Frees 2,267,538,686