Issue #2823: BigInt arithmetic works in a dead operand’s buffer: the compiler picks a consuming form at a value’s last use, as replace does

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 2. Follows BigInt sizes its digit buffers up front with Vec(capacity: n) (#2798).

Found

In acc = acc.add(num.mul(k2)) the old acc is dead after the call, yet add builds its sum in a fresh buffer and the old one is freed. The spike (#2795) measured (callgrind, io-method blocking, pidigits 2000 digits, harness on lowered Rust): sized buffers alone (#2798) 1,598.4 M Ir; sized buffers plus working in the dead operand’s buffer 1,479.8 M Ir (-7.4 % on top), fresh allocations 141,462 -> 98,059, allocator+memcpy+glue 2.8 % -> 1.7 %. Sure: measured on a hand-written harness, not a hypothesis.

The mechanism exists for one method. ADR-text-reuse-1 D4: “replace on a source not read afterwards works in its buffer. A choice of how the call is lowered, not a rule … No second method: one way to say it.” Today: check_takes.nika replaces_at_last_use_said fills facts.replaced_last_uses (check_facts.nika:199) for replace/replacen receivers only, and emit_core_calls.nika:1400 / :2195 pick replace_in there. Generalising it is not a new language form (the user still writes a.add(b)); it is D4 applied to more std methods. Supervisor decision (2026-11-23): an obvious extension of D4, approved; the builder writes it into the ADR as below.

The spike’s sorting of BigInt operations (a = in place, b = buffer recyclable, c = unavoidable): add a when self is dead (index-wise, plus one carry digit); sub a for the result; neg/abs a (sign only); mul b only (the product writes ahead of its reads, so no handover; not built here).

To build

  1. ADR-text-reuse-1: add a paragraph to D4 (or a D6 beside it) saying the same lowering applies to any std method that declares a consuming lowering of itself; the user-visible method stays one; --tethers narrates it as D5 does (“add works in the buffer of acc”).
  2. Write tests/compiler/tests/bigint_add_at_last_use.nika with .stdout: acc = acc.add(x), let y = a.sub(b) with a dead, a.neg() dead, and each of them again with the operand read afterwards (the value printed after, so a wrong in-place write shows). It passes before and after.
  3. Generalise replaced_last_uses to a last-use fact for a method receiver (keep the name or rename, one set), and keep replace’s behaviour unchanged (its tests stay green).
  4. A table in the emitter: method -> consuming form (replace -> replace_in as now; BigInt.add -> add_own, sub -> sub_own, neg -> neg_own, abs -> abs_own), picked where the receiver has the last-use fact, as D4 picks replace_in. Not reachable by user code by name (D4: one way to say it).
  5. Write those _own forms in crates/nikaia-std/src/bigint.nika, working in self’s buffer.
  6. --tethers prints one line per such site (D5), with a .stdout test of it.
  7. Measure tests/compiler/tests/perf_bigint_spigot.nika per docs/measuring.md, Ir and fresh allocations before/after, in the commit message; also the LRA test lra_pivot_past_i64 in packages/logic (one figure each).

Done when

The tests of steps 2 and 6 and replace’s existing tests are green, and the commit message shows the spigot’s Ir and fresh allocations falling (spike: about -7 % Ir).

Not here

mul handing over its buffer (needs a rewritten top-down loop); way 3, reusing a matched node’s box (comment on #577); small BigInt values without allocation (own issue).

Comments

keywan-ghadami, 2026-11-23

Brief queued: issue-2823


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

Issue #2823

Your work order is issue #2823, 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/2823/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-2823.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-2823, Result: done

Tokens: input 254, cache read 8389547, cache write 176574, output 60312 Cost: 4.51 USD, time 24 min 54 s, turns 127

Report

Result: done Files: crates/nikaia-std/src/bigint.nika crates/nikaia-std/src/bigint.rs crates/nikaia-std/src/compiler/check_core_method.nika crates/nikaia-std/src/compiler/check_core_stmt.nika crates/nikaia-std/src/compiler/check_facts.nika crates/nikaia-std/src/compiler/check_flow.nika crates/nikaia-std/src/compiler/check_takes.nika crates/nikaia-std/src/compiler/emit_core_args.nika crates/nikaia-std/src/compiler/emit_core_calls.nika crates/nikaia-std/src/compiler/lowered.rs crates/nikaia-std/src/compiler/lowered/check_core_method.rs crates/nikaia-std/src/compiler/lowered/check_core_stmt.rs crates/nikaia-std/src/compiler/lowered/check_facts.rs crates/nikaia-std/src/compiler/lowered/check_flow.rs crates/nikaia-std/src/compiler/lowered/check_takes.rs crates/nikaia-std/src/compiler/lowered/emit_core_args.rs crates/nikaia-std/src/compiler/lowered/emit_core_calls.rs crates/nikaia-std/src/compiler/lowered/logic/lowered.rs crates/nikaia-std/src/compiler/lowered/logic/lowered/sv_num.rs crates/nikaia-std/src/compiler/lowered/logic/lowered/sv_simplex.rs docs/specification/adr/adr-text-reuse-1.md tests/compiler/tests/bigint_add_at_last_use.nika tests/compiler/tests/bigint_add_at_last_use.stdout tests/compiler/tests/tethers_narrate_consuming_twins.nika tests/compiler/tests/tethers_narrate_consuming_twins.stdout Commit: BigInt add/sub/neg/abs work in a dead receiver’s digits (ADR-text-reuse-1 D6) Body: ADR D6 added; replaced_last_uses becomes consumed_last_uses (a last use of any method receiver; replace unchanged); checker marks bigint add/sub/neg/abs on a dead let, on x = x.m(..) (not from inside a lambda: a capture cannot be moved) and on a temporary; emitter maps them to add_own/sub_own/neg_own/abs_own (bigint.nika, absent from std.contracts so no program names them); –tethers narrates each. perf_bigint_spigot (opt-level 3, callgrind, memcheck): Ir 29,799,275 -> 27,515,797 (-7.7 %), allocs 20,974 -> 18,083 (-13.8 %), same output. packages/logic an_lra_pivot_past_i64resumes_in_bigint: Ir 15,555,797 -> 15,468,242 (-0.6 %), allocs 35,744 -> 35,244. Fixes: #2823 Tests: bigint_add_at_last_use (green on old and new binary, incl. a lambda case that Rust refused before the guard), tethers_narrate_consuming_twins, tethers_narrate_gives_and_reuses, text_replace, a_replaced_text_is_its_own, lsp_inlay_hints, bigint_, perf_bigint_* (17 green, –both-settings); logic an_lra_pivot_past_i64 green; clippy, fmt, pre-push green Gate: green Deviation: none Found: none Needs: none Changelog: BigInt arithmetic reuses a dead operand’s digits: add, sub, neg and abs on a BigInt not read again (x = x.add(y), let y = a.sub(b) with a dead, a temporary) work in its buffer; –tethers narrates “add works in the buffer of acc” (ADR-text-reuse-1 D6); the spigot benchmark runs 7.7 % fewer instructions and 13.8 % fewer allocations Issue: met I1-I13; the closure capture case (a twin inside a lambda moving a captured name) was not named in To build Owner: none Comment: none Friction: a temporary made by a type-path call (bigint::BigInt::of(0).add(b)) narrates “the call before it made”, since dotted_path gives no name for a path through a type Disk freed: 13 MB of scratch builds; target/nikaia-project-tests 1.8G, / 11G free (74 % used), no new cache entry over 100 MB Reviewed: 05070ce01fc90810b16b1bde03f779819e98bb23b24b4d0c43b133b562215c81 Pushed: local 234e6689404fc477260b408a0d93287031acb635 Attribution: Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com Claude-Session: https://claude.ai/code/session_01UVxJDukWgopUzVU2M1TjK7

Debrief

  1. Most cost: setting up the measurement. nikaia build wants a project and has no release flag; the optimized profile comes from [build.x86_64-linux] opt-level = 3 in nikaia.toml, found in benches/game/game.nika. The LRA test also reads tests/smtlib/… relative to the project, so its first run failed. Next came two build cycles: one for NK1131 (let mut sum = self.digits under keep self is a view), one for the lambda capture that Rust refused.
  2. Looked up: how the tethers test runs lower --tethers; CheckRepeats.what == "lambda" together with frame_of; how a keep parameter is rebound with let mut x = x; how the benches set the build profile and runtime config.
  3. Checks: both build cycles, the probe matrix, the 17 filtered tests, the LRA test, callgrind and memcheck before and after, fmt, clippy, pre-push, then the commit dry run and the commit. Wasted: one dev-profile build of the spigot.
  4. One change: a builder-facts recipe for measuring a Nikaia program optimized: project layout, [build] table, runtime.toml, valgrind --tool=memcheck for allocation counts.
  5. What the issue lacked:
    • The lambda capture case (a twin moving a captured name): rules-gap.
    • knowledge: Lowering | under \keep self`, `let mut d = self.digits` is a view of the field (NK1131); hand the field straight to a free fn with a `keep` parameter, then `let mut own = kept` there.`
    • The issue does not say how to get allocation counts: broke I5. Its measure line named the figure, not the command.
  6. Traps:
    • Fact: Lowering | A consuming lowering of \x = x.m(..)` must skip names captured by a lambda (CheckRepeats “lambda” with frame > frame_of(name)), else rustc refuses a move out of an FnMut capture.`
    • Fact: Core | \nikaia build` has no release flag: an optimized build needs `[build.x86_64-linux] opt-level = 3` in the project’s nikaia.toml (as benches/game/game.nika writes it).`
  7. Measuring:
    • Fact: Measuring | perf_bigint_spigot, opt-level 3, io-method blocking: Ir 29,799,275 → 27,515,797, allocs (memcheck) 20,974 → 18,083 (2026-11-23, commit 234e6689).
    • Fact: Measuring | logic an_lra_pivot_past_i64_resumes_in_bigint, built as a project with tests/smtlib copied in: Ir 15,555,797 → 15,468,242, allocs 35,744 → 35,244 (2026-11-23, commit 234e6689).

Generated by Claude Code

Back to Open Issues