Skip to content

perf: avoid copying temporary bignum results - #15162

Closed
kim-em wants to merge 7 commits into
leanprover:masterfrom
kim-em:perf-move-mpz-results
Closed

kim-em wants to merge 7 commits into
leanprover:masterfrom
kim-em:perf-move-mpz-results

Conversation

@kim-em

@kim-em kim-em commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

This PR reduces allocation and copying overhead for bignum arithmetic and conversions.

Move owned temporary limb storage into Lean objects, falling back to copying when capacity exceeds twice the used limb count. Preserve copying lvalue calls, object layout, small-value normalization, and mimalloc header initialization. Add extern comparisons, compiled-caller tests, and a runtime benchmark documenting timings, allocations, and retained storage.

This change is independent of #15160, which retains the existing allocator.

🤖 prepared with codex

This PR reduces allocation and copying overhead for bignum arithmetic and conversions by transferring temporary result storage into Lean objects.

Reuse the existing GMP and built-in bignum move constructors while preserving copying lvalue calls, object layout, small-value normalization, and mimalloc header initialization. Add exact extern/reference comparisons, retained-input and retained-result tests, direct copy/move ownership checks, and an isolated runtime benchmark with allocation, retained-storage, and peak-storage measurements.
This PR bounds spare limb capacity retained by move-aware bignum allocation.

Copy temporary results whose capacity exceeds twice the used limb count. Add independent arithmetic and capacity regression checks, align native test compilation with the runtime, and document guarded allocation benchmarks.
@kim-em

kim-em commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator Author

!bench

@leanprover-radar

leanprover-radar commented Sep 15, 2026

Copy link
Copy Markdown

Benchmark results for 344f681 against dc34e5f are in. There are significant results. @kim-em

  • 🟥 build//instructions: +1.0M (+0.00%)

Large changes (1✅)

  • compiled/liasolver//instructions: -193.7M (-5.83%)

Medium changes (1✅)

  • vcgen/GetThrowSetGrind/100/kernel//wall-clock: -6ms (-10.00%)

Small changes (2✅, 1🟥)

  • 🟥 build/profile/grind ring//wall-clock: +1ms (+47.93%)
  • compiled/unionfind//task-clock: -77ms (-8.99%)
  • compiled/unionfind//wall-clock: -77ms (-8.98%)

This PR summarizes representative timings, the capacity policy, and benchmark invocation.
This PR keeps representative Int and Nat heap-result and heap-to-scalar checks, with a concise test description.
This PR describes the Int and Nat bignum results and heap-to-scalar normalization checked by the extern tests.
Use the test environment’s CXX and query leanc for public runtime flags, preserving sanitizer instrumentation without using the bundled C-only compiler setup.
@TwoFX

TwoFX commented Sep 15, 2026

Copy link
Copy Markdown
Member

See also #14978.

@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Sep 15, 2026
@kim-em kim-em closed this Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-compiler Compiler, runtime, and FFI fsanitize-ci toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants