Repository navigation
Description
Problem
Arithmetic-heavy code is 5–7x slower than CPython (int while-loop 5.2x, float loop 5.7x, nbody 5.5x, fannkuch 6.8x), and profiles show the cost is dominated by object allocation churn, not the arithmetic itself. In the int while-loop profile:
default_dealloc6.3% +PyPayload::into_ref3.3% +Context::new_int0.7% (self) — allocating/freeing a heapPyIntfor nearly every intermediate resultmalachite try_from_signed3.8% + bigintadd/add_limb_ref3.6% — full arbitrary-precision arithmetic for machine-word values- In float/attr workloads
libsystem_mallocreaches 13–16% of wall time.
Notably, bigint-heavy code is already faster than CPython (fib with huge ints: 0.6x) — malachite is excellent. The problem is exclusively the small-number fast path.
Causes
-
Every int/float result outside the −5..256 cache is a fresh heap object (
Context::new_int/new_bigint/new_float,crates/vm/src/vm/context.rs~484–508), carrying the fullPyInnerheader (refcount + vtable + gc bits + intrusive GC pointers + type ptr — ~48 bytes before payload,crates/vm/src/object/core.rs~349) even though ints/floats are acyclic and never GC-tracked. -
The specialized
BinaryOpAddInt/SubtractInt/MultiplyInttake the full BigInt path —execute_binary_op_int(crates/vm/src/frame.rs~8211) callsop(a.as_bigint(), b.as_bigint())thennew_bigint(&result). Ironically the generic fallbackexecute_bin_ophas an i64 fast path (int_add/int_subviato_i64()+checked_add, frame.rs ~7156) that the specialized opcodes skip. So specializing an int add currently makes the arithmetic slightly heavier and only saves the protocol dispatch. -
There is a payload freelist mechanism on the allocation path (
object/core.rs~2255), but malloc still dominates these profiles, so it isn't engaged for the hot int/float case (or is bypassed viainto_ref).
Suggested direction
Layered, all free-threading-compatible:
- Quick: give
execute_binary_op_intthe same i64 fast path as the generic handler (compute in i64, only fall back to BigInt on overflow). Same for compare ops. - Per-thread freelists for
PyInt/PyFloat: recycle the fixed-size allocation (header+payload) thread-locally, no locks. CPython does exactly this for floats/frames; combined with (1) this removes malloc/free from the hot loop. - Consider widening the small-int cache and checking cache membership on the i64 result directly (currently
new_intconverts throughto_i32first). - Long term: tagged immediate ints (pointer-tagged i63 or similar) so small-int results allocate nothing at all. This is a deep change to
PyObjectRef(every downcast/refcount site must handle the tag) but is the only way to actually reach CPython-level loop arithmetic, and it interacts well with free-threading since immediates need no refcounting at all. Worth a separate design discussion before committing.
Suggested order: (1) is a small PR; (2)+(3) medium; (4) needs an RFC-style discussion.
Researched and written by Claude on behalf of @youknowone. Part of the performance tracking series.
Part of #38.
Activity
youknowone commentedon Aug 5, 2026
Status update (2026-08-05) — effect of the call-overhead work (#40)
With all of #40's PRs merged (RustPython#8354, RustPython#8434, RustPython#8448, RustPython#8431), I A/B-tested
main@ 1c7759c against the commit just before RustPython#8354 (04c3ecf) with identical scripts, interleaved runs (Apple M5 Max, moderately loaded, CPython 3.14.5 reference):x = x + 3outside small-int cache)x = x + 1.5)acc += 1)Takeaways:
execute_binary_op_int(frame.rs ~9826) still goes straight toop(a.as_bigint(), b.as_bigint(), vm)— the i64 fast path (suggestion 1) is still unimplemented and remains the obvious first PR here, likely alongside a look at the Split InterpreterFrame from FrameObject for stack-allocated execution RustPython/RustPython#8354 dispatch regression.— commented by Claude