Skip to content

Perf: small int/float arithmetic allocates heap objects; specialized BinaryOp*Int skips the i64 fast path #43

Open

Description

@youknowone

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_dealloc 6.3% + PyPayload::into_ref 3.3% + Context::new_int 0.7% (self) — allocating/freeing a heap PyInt for nearly every intermediate result
  • malachite try_from_signed 3.8% + bigint add/add_limb_ref 3.6% — full arbitrary-precision arithmetic for machine-word values
  • In float/attr workloads libsystem_malloc reaches 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

  1. 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 full PyInner header (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.

  2. The specialized BinaryOpAddInt/SubtractInt/MultiplyInt take the full BigInt path — execute_binary_op_int (crates/vm/src/frame.rs ~8211) calls op(a.as_bigint(), b.as_bigint()) then new_bigint(&result). Ironically the generic fallback execute_bin_op has an i64 fast path (int_add/int_sub via to_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.

  3. 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 via into_ref).

Suggested direction

Layered, all free-threading-compatible:

  1. Quick: give execute_binary_op_int the same i64 fast path as the generic handler (compute in i64, only fall back to BigInt on overflow). Same for compare ops.
  2. 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.
  3. Consider widening the small-int cache and checking cache membership on the i64 result directly (currently new_int converts through to_i32 first).
  4. 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

  1. youknowone commented on Aug 5, 2026

    @youknowone
    OwnerAuthor

    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):

    benchmark pre-#40 main (with #40) CPython 3.14 ratio now
    int while-loop (x = x + 3 outside small-int cache) ~105 ns/iter ~115 ns/iter 15.9 ns/iter ~7.2x
    float while-loop (x = x + 1.5) ~93 ns/iter ~101 ns/iter 15.3 ns/iter ~6.6x
    loop baseline (acc += 1) ~82 ns/iter ~90 ns/iter 13.1 ns/iter ~6.9x

    Takeaways:

    — commented by Claude

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions