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).
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).
--tethers narrates it as D5 does (“add works in the buffer of acc”).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.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).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)._own forms in crates/nikaia-std/src/bigint.nika, working in self’s buffer.--tethers prints one line per such site (D5), with a .stdout test of it.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).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).
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).
Brief queued: issue-2823
agent: builder model: opus max_turns: 200 budget_usd: 30 effort: medium —
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.
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-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.
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-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
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
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.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.[build] table, runtime.toml, valgrind --tool=memcheck for allocation counts.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.`broke I5. Its measure line named the figure, not the command.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).`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