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
Avoid temporary Decimal allocation for int64 operands in _decimal arithmetic #162777
In Modules/_decimal/_decimal.c, the six binary arithmetic slots (+, -, *, /, //, %) currently allocate a temporary Python Decimal object when one operand is an int.
For exact int operands that fit in a signed 64-bit integer, converting the integer directly into a stack mpd_t via mpd_qset_i64_exact avoids the temporary allocation while preserving existing operand order, context flags, traps, and fallback behavior for larger integers and subclasses.
Here are the PGO+LTO benchmark results, including decimal_pi and decimal_factorial from python/pyperformance#491 (as @skirpichev asked on the closed PR) and the standalone microbenchmark script. I'll wait for feedback here before reopening a PR.
Build & environment
Base commit: d88a6b9c3e
Machine: Apple M4 Pro, macOS 27.0.1 (arm64), Apple Clang 21.0.0, system libmpdec 4.0.1
Configure (identical for both builds, deterministic PGO task):
Note the small +0.8 ns (1.02x) cost on decimal_add_decimal_control from checking PyLong_CheckExact before CONVERT_BINOP. If this approach looks worthwhile, checking PyDec_Check(self) && PyDec_Check(other) first (or only taking the PyLong_CheckExact branch when one operand is not already a Decimal) should avoid that branch on Decimal op Decimal.
Instead of "42", can you use larger integers? 42 is a small integer so there might other factors of why it's faster. What about 31-bit integres for instance? or mixed arithmetic with larger values in general
Sure, I re-ran it with 1_234_567_891 and (1 << 62) - 12345. The second one is labeled int63 in the table, which is a bad name since it's really 62 bits, but it's near the top of the int64 range and that's what I was after. I also threw in 10**25 + 7 and 10**60 + 7, which don't fit in int64 at all. Same PGO+LTO build and pyperf settings as last time.
So it wasn't the small int cache. The 31 and 62 bit values get pretty much the same speedup 42 did. It drops off as the Decimal side gets bigger, since at 100 digits the actual arithmetic is most of the cost. Anything past int64 goes through the old fallback, so there's nothing to gain there. I'd guess the one 1.02x slower is noise, but I haven't looked closely.
mixed4 is just d + n, n - d, d * n, n / d in a loop. Script's below if you want to run it yourself.
This is something I was thinking before. Similar optimizations used in the gmpy2 or python-gmp packages. (Though, I was less interested in this. It adds a new optimization, while #144650 is about regression, coming from using heap types.)
Activity
picnixz commentedon Nov 28, 2026
As in your other issue, please share benchmarks, use PGO/LTO builds, and avoid creating a PR until this has been acknowledge.
josh-hurst commentedon Nov 28, 2026
Here are the PGO+LTO benchmark results, including
decimal_pianddecimal_factorialfrom python/pyperformance#491 (as @skirpichev asked on the closed PR) and the standalone microbenchmark script. I'll wait for feedback here before reopening a PR.Build & environment
d88a6b9c3elibmpdec4.0.1PROFILE_TASK="-m test --pgo --randseed 4109" \ ./configure --enable-optimizations --with-lto --with-system-libmpdec --with-ensurepip=nodecimal.Decimal is _decimal.Decimalon both binaries,PYTHONHASHSEED=1, defaultpyperfsettings (-p 20 -w 1 -n 3), interleavedbase/patched.1.
decimal_pi,decimal_factorial(pyperformance#491), andtelco(
decimal_pirunsna + 8andda + 32inside its main loop, anddecimal_factorialmultipliesDecimalleaves withintbase cases.)2. Mixed
Decimal/intmicrobenchmarkNote the small
+0.8 ns(1.02x) cost ondecimal_add_decimal_controlfrom checkingPyLong_CheckExactbeforeCONVERT_BINOP. If this approach looks worthwhile, checkingPyDec_Check(self) && PyDec_Check(other)first (or only taking thePyLong_CheckExactbranch when one operand is not already aDecimal) should avoid that branch onDecimal op Decimal.Microbenchmark script (
bm_decimal_int_micro.py)picnixz commentedon Nov 28, 2026
Instead of "42", can you use larger integers? 42 is a small integer so there might other factors of why it's faster. What about 31-bit integres for instance? or mixed arithmetic with larger values in general
josh-hurst commentedon Nov 28, 2026
Sure, I re-ran it with
1_234_567_891and(1 << 62) - 12345. The second one is labeled int63 in the table, which is a bad name since it's really 62 bits, but it's near the top of the int64 range and that's what I was after. I also threw in10**25 + 7and10**60 + 7, which don't fit in int64 at all. Same PGO+LTO build and pyperf settings as last time.So it wasn't the small int cache. The 31 and 62 bit values get pretty much the same speedup
42did. It drops off as the Decimal side gets bigger, since at 100 digits the actual arithmetic is most of the cost. Anything past int64 goes through the old fallback, so there's nothing to gain there. I'd guess the one 1.02x slower is noise, but I haven't looked closely.mixed4is justd + n,n - d,d * n,n / din a loop. Script's below if you want to run it yourself.bm_decimal_int_sizes.py
picnixz commentedon Nov 28, 2026
This seems a good performance boost but I wonder whether we are not complicating the code too much. WDYT @skirpichev @eendebakpt?
skirpichev commentedon Nov 28, 2026
This is something I was thinking before. Similar optimizations used in the gmpy2 or python-gmp packages. (Though, I was less interested in this. It adds a new optimization, while #144650 is about regression, coming from using heap types.)
I reopened pr, it does make sense.
pythongh-162777: Check exact Decimal operands before int64 fast path