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.
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.
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.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.BigInt::of on a value that fits does not allocate.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.bigint_small_values and the packages/logic tests are green, and the commit message shows the LRA test’s allocations falling.
Brief queued: issue-2824
agent: builder model: opus max_turns: 200 budget_usd: 30 effort: medium —
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.
develop in the main checkout; other agents’ uncommitted files are not yours: never touch or stage them.target/fast/nikaia run scripts/commit.nika -- REPORT (it adds exactly the report’s Files:); the driver pushes.cargo build --profile fast -p nikaia, lower-std and its fixpoint when a tools/*.nika changed, cargo fmt, cargo clippy --all-targets -- -D warnings, nikaia run scripts/pre-push.nika, the tests the issue names and the ones your change touches.CHANGELOG.md, badges and NK codes are the supervisor’s. New tests in Nikaia only: a Rust test file an older issue names is built as a tests/compiler/tests/ program, no Deviation: and no Issue: point (I9, #1671).Body:. Found: is only for what lies outside the area, cannot be fixed this run or needs a decision; the driver files it as an issue.std or the toolchain lacks stops the work, unless an ADR or Part I-III refuses it (name it): Result: blocked, Found: the gap with a probe, Needs: supervisor decision.NIKAIA_CACHE_LIMIT=2G; at the end delete your own clone’s target/ and target/release/nikaia-scratch.suspected, or does not mark (docs/issues.md I7), is a hypothesis: run the check that settles it (nikaia run scripts/where.nika -- NAME, the probe with a trace, callgrind for a cost) before you build on it (owner, 2026-11-16). Shown false: say so in Issue: as wrong I7: <what the check showed> (the driver comments it on the issue) and follow the real cause.Result: partial with what you ruled out, the probe, and the suspected file and function./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.
Result:, Gate: (green or red <check>), Deviation:, Found: and the other fields with nothing to say hold the bare word: done, none. Anything after it reads as content.Result: done only when the issue’s Done when holds; partial with the items left otherwise.Deviation: only for an item of the issue built otherwise than written, or not built. Found: only for a defect outside your area, or one this run cannot fix: one defect per Found: line, with its probe and the output it gives, only the shapes that fail today, and the suspected file and function or cause unknown (#1695); a by-design behaviour is none (#1677). A correct program your own change refuses (a fail-closed check’s false refusal) is no Deviation: but one Found: line per shape, with its probe (#1680). Everything else goes into Body:; how you verified goes into Tests:.Issue: by docs/issues.md, How a run judges its issue: complete, or broke I<n>: ..., rules-gap: ..., wrong I<n>: ..., joined with ;; a point your own commit built gets (built) after its kind (rules-gap (built): ...), and the driver does not file it. complete is the bare word: prose after it (complete; the earlier runs ...) is filed as an improvement issue (#1931); history or a note for your own issue goes into Body: or Comment: #N <text>.done <sha>, report <path> (or BLOCKED: ... / PUSH REJECTED ...).Generated by Claude Code
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
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
.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.[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.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.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.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()).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