Issue #2824: Small BigInt values are stored without a heap buffer, so BigInt::of and gcd on small numbers do not allocate

Open and answer this issue on GitHub.

performance, wip · opened 2026-11-23 · updated 2026-11-23 · 2 comments

Part of: Spike: automatic memory reuse for values replaced at their last use (#2795), its work item 4.

Found

Every BigInt, even 0 or 1, holds its digits in a Vec[u64] on the heap. The spike (#2795) measured the LRA test lra_pivot_past_i64 in packages/logic (callgrind, io-method blocking): 12.6 M Ir, of which allocator 40.5 % + glue 18.0 % + memcpy 1.3 % = 59.8 %; 25,762 allocations + 2,377 grows; gcd 36.7 % inclusive, BigInt::of 3.8 %. Most values in the solver’s rationals fit one machine word, so most of those allocations hold a single digit. Sure that the allocator dominates (measured); how much inline storage saves is a hypothesis this issue measures.

To build

  1. Write tests/compiler/tests/bigint_small_values.nika with .stdout: values 0, 1, -1, i64 max and min, u64 max, and each operation (add, sub, mul, div_rem, gcd, compare, to_text) crossing between one digit and two in both directions. It passes before and after.
  2. In crates/nikaia-std/src/bigint.nika, store a value of at most one digit without a heap buffer (one choice: an enum of a small form and the digit list; the representation is the builder’s, in Nikaia, no new Rust). The public API does not change.
  3. BigInt::of on a value that fits does not allocate.
  4. Measure lra_pivot_past_i64 and tests/compiler/tests/perf_bigint_spigot.nika per docs/measuring.md, Ir and allocations before/after, in the commit message. If the spigot (large values) rises by more than 1 % Ir, say so in the report.

Done when

bigint_small_values and the packages/logic tests are green, and the commit message shows the LRA test’s allocations falling.

Comments

keywan-ghadami, 2026-11-23

Brief queued: issue-2824


agent: builder model: opus max_turns: 200 budget_usd: 30 effort: medium —

Issue #2824

Your work order is issue #2824, inlined below with its comments. There is no brief written around it: plan your own steps from its To build list, build them in order, commit after each finished step, and end when its Done when holds (or at the limits of builder.md, reporting what is left). The facts in your prompt are Core of docs/builder-facts.md; more on a topic: nikaia run scripts/syntax.nika -- KEYWORD, a probe: nikaia run scripts/probe.nika -- SNIPPET.

Measure the issue against docs/issues.md while you work: what it lacked, what was wrong in it. That answer goes into the report’s Issue: field. A finding, a changed plan or a finished item also goes into the issue as a comment (gh api repos/Nikaia-Foundation/Nikaia/issues/2824/comments -F body=@file, ending with the Claude Code footer), rule I13.

Rules (CLAUDE.md; shared checkout)

Report (at most 15 lines, to exactly /home/user/Nikaia/docs/briefs/performance/issue-2824.md.report.md)

Fields: Result, Files, Commit, Body (optional), Fixes, Pushed, Tests, Gate, Deviation, Found, Needs, Changelog, Issue, Owner (a question for the owner), Comment (#N <text>, what to say on another issue; the driver posts it, one line each), Friction, Disk freed, and last Attribution: with your commit trailer lines below it.


Generated by Claude Code

keywan-ghadami, 2026-11-23

Run: issue-2824, Result: done

Tokens: input 100, cache read 3604046, cache write 86263, output 35365 Cost: 2.17 USD, time 2 min 57 s, turns 50

Report

Result: done Files: crates/nikaia-std/src/bigint.nika crates/nikaia-std/src/bigint.rs Commit: std::bigint keeps a value of one digit without a heap buffer Body: BigInt gains a field low: a magnitude of one digit sits there with digits empty (vec[] allocates nothing), one of two digits or more is all in digits with low 0, zero is sign 0 low 0; each value has one form, so derived == and hash stay those of value. of/from_wide, add/sub (one summed helper; sub no longer clones a negated operand), mul, div_rem, gcd’s word loop, compare, narrow, bits and to_text take a one-digit path, and a one-digit operand beside a long one goes through new digit loops (magnitude_add_digit, _sub_digit, _mul_digit and their in-place twins for add_own/sub_own), so no one-element list is made; the public API is unchanged. Measured opt-level 3, codegen-units 1, io-method blocking, same output byte for byte: perf_bigint_spigot Ir 27,515,737 -> 21,018,870 (-23.6 %), allocs (memcheck) 18,083 -> 12,740 (-29.5 %); logic an_lra_pivot_past_i64_resumes_in_bigint Ir 15,477,701 -> 13,246,805 (-14.4 %), allocs 35,244 -> 25,934 (-26.4 %). Fixes: #2824 Pushed: local 5686dd296b7574ee31f49e7ccff824774e116615 Tests: bigint_small_values (recorded before the change, passes after), the 9 bigint* and tethers_narrate_consuming_twins programs of tests/compiler green; packages/logic 54 passed; lower-std fixpoint clean; pre-push green Gate: green Deviation: none Found: use std::bigint / fn main() throws { let a = bigint::BigInt::of(1); println(a.compare(bigint::BigInt::of(2)) == Ordering::Less) } (scripts/probe.nika, target/fast/nikaia at HEAD) stops at NK1343 “Rustc output: method compare is private”, should print true (the checker accepts it; Part I Ord’s compare is callable): the emitter writes an impl Ord’s compare as a private inherent fn (bigint.rs impl BigInt { fn compare(&self, ..) }), so a caller outside the crate that declares the type cannot reach it; suspected crates/nikaia-std/src/compiler/emit_item.nika, the inherent twin of a trait impl’s method is emitted without pub (check: the same call on a type of a dependency package) Needs: none Changelog: BigInt values of one digit live without a heap buffer: BigInt::of, and add, sub, mul, div_rem, gcd and compare on small numbers allocate nothing; the pidigits spigot runs 23.6 % fewer instructions and 29.5 % fewer allocations, the LRA pivot past i64 test 14.4 % and 26.4 % Issue: complete Friction: none Disk freed: none (project-tests cache 1.8G, disk 9.4G free, no new entry over 100 MB; scratch projects in the scratchpad only) Reviewed: 6d4113b3031faaacdc525fc1b132394ad3aa7d7ae18763ec5130772890362384 Attribution: Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com Claude-Session: https://claude.ai/code/session_01UVxJDukWgopUzVU2M1TjK7

Debrief

  1. Most cost: getting the new test to build. It took three tries: NK1111 on a last-use .clone(), NK2105 because compare keeps its argument, then NK1343 because compare is private. Setting up the measurement went quickly, since #2823’s debrief already gave the method.
  2. Looked up: how #2823 measured, in its debrief: an opt-level 3 project with [build.x86_64-linux], runtime.toml, memcheck for allocation counts, and tests/smtlib copied in. Also that lower-std re-lowers a std .nika in under a second, and that programs build against the source tree’s bigint.rs. That last point meant the before/after comparison needed no compiler build.
  3. Checks run: the BigInt filter in tests/compiler (9 programs), the whole logic package (3 min), measure before/after, the compiler build plus the lower-std fixpoint, pre-push, and the dry runs. None were wasted. The compiler build changed no behaviour, but the rules require it.
  4. One change: a committed measuring script for programs, e.g. scripts/measure_program.nika FILE [--dep PKG] [--data DIR], which builds at opt-level 3, prints Ir and allocations and checks the output is unchanged. Two builders have now written the same shell script by hand.
  5. Issue:
    • broke I5: item 4 says “per docs/measuring.md”, but that file has no recipe for allocation counts or for a package test that reads data files.
    • knowledge: Measuring | a std .nika change shows up in a program built against the source tree right after lower-std, without a compiler rebuild, so the before/after figures need only one tree.
  6. Traps for the next builder:
    • Fact: Core | calling an Ord/Eq trait method directly on a std type (a.compare(b)) passes the checker but fails NK1343 "method is private": the generated inherent method has no pub.
    • Fact: Tests and the gate | Ord::compare keeps its argument (NK2105 on a later read), so a test writes x.compare(x.clone()).
  7. For the next builder in the measuring area:
    • Fact: Measuring | recipe: a scratch project with [build.x86_64-linux] opt-level = 3, codegen-units = 1, incremental = false, plus [dependencies] logic = { path = … } for the LRA test and tests/smtlib/lra_pivot_past_i64.smt2 copied in; run with NIKAIA_RUNTIME_CONFIG set to a file holding io-method = “blocking”, under callgrind and then valgrind –tool=memcheck (the "total heap usage" line).
    • Fact: Measuring | perf_bigint_spigot: Ir 27,515,737 → 21,018,870, allocs 18,083 → 12,740 (2026-11-23, commit 5686dd29).
    • Fact: Measuring | logic an_lra_pivot_past_i64_resumes_in_bigint: Ir 15,477,701 → 13,246,805, allocs 35,244 → 25,934; gcd 24.4 % inclusive, malloc+free still about 35 % of Ir (2026-11-23, commit 5686dd29).
    • Fact: Measuring | key file crates/nikaia-std/src/bigint.nika: a one-digit magnitude sits in low with digits empty, a longer one is all in digits with low 0 (2026-11-23, commit 5686dd29).

Generated by Claude Code

Back to Open Issues