Open and answer this issue on GitHub.
compiler, performance, blocked · opened 2026-11-20 · updated 2026-11-20 · 4 comments
Found while a Counting global allocator in the nikaia binary costs +4.2 % instructions (#2368)
The compiler allocates less. In a compile of tests/compiler/tests/byteset_every_search_is_the_byte_by_byte_one.nika (callgrind, nikaia built from develop with the system allocator, 4,439,441,817 Ir in all) glibc’s allocator alone takes about 31 %:
| function | Ir |
|---|---|
_int_free |
413,726,467 |
_int_malloc |
350,735,000 |
malloc |
280,007,027 |
free |
169,201,028 |
malloc_consolidate |
67,425,282 |
realloc |
54,453,074 |
for about 6.05 M allocations, 6.04 M frees and 0.88 M reallocations. Visible callers in the same profile: core::fmt::write with Text as Write (text built by formatting), List<u8, Place>::grow, BTreeMap<Text, _>::insert and its IntoIter::dying_next, StrSearcher::new, proofs_write_term, text_tiers.
malloc per caller in callgrind) and list the ten largest sites here..nika source (crates/nikaia-std/src/compiler/*.nika) that removes the allocation (reuse a buffer, build text once, a view instead of a copy), or the compiler defect that makes the lowering allocate where the source does not (fixed in the compiler, ADR-294).docs/measuring.md).The ten largest sites are handled or filed, and the compile above spends at most half the Ir in the allocator it spends today.
Brief queued: issue-2382
agent: builder model: opus max_turns: 200 budget_usd: 30 effort: medium —
Your work order is issue #2382, 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/2382/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-2382.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-2382, Result: partial
Tokens: input 372, cache read 12430813, cache write 272364, output 81491 Cost: 6.64 USD, time 1 min 28 s, turns 186
Result: partial
Files: none (3 local commits: 2955466dd emitter write_fmt, dae91bdbb logic Fourier-Motzkin on rank forms, 13e0ea10b pre-sized key texts)
Commit: 2955466dd dae91bdbb 13e0ea10b
Body: Allocator Ir of nikaia check b.nika (callgrind, malloc+free+realloc inclusive): 1,009.4M -> 911.9M (push_str(f”…”) lowers to write_fmt into the text) -> 692.8M (lg_contradictory_at/lg_eliminate on rank forms with checked merges, atoms by view not cloned, names moved via BTreeSet::drain, vectors pre-sized, lg_add/lg_add_constant take their operand by value, arena.nodes[id] views not at() clones, lg_copy/variables clone only new names) -> 677.6M (lg_written, LgNormal::text, proofs_written pre-sized); total Ir 2,764M -> 2,190M; output identical to baseline (cmp), logic package 48/48.
Fixes:
Part: #2382
Pushed: local 13e0ea10b
Tests: push_str_of_formatted_text_formats_into_the_text –both-settings green (passes on old binary too: an optimisation); nikaia test –project packages/logic 48 passed; check output byte-identical to baseline; clippy clean; pre-push green for each commit
Gate: green
Deviation: Allocator Ir is down 33 % (1,009M -> 678M), not halved; below the top 16 sites the tail is flat (no site over 1.3 %), so the rest needs many small changes or an allocator decision.
Found: use std::hash then fn main() throws { let mut v: Vec[String] = ["b", "a", "b"]; v.sort(); let r = v.binary_search("b"); println(f"{v.len()}"); match r { i => println(f"{i}"), null => println("no") } } passes the checker and then rustc refuses it (“mismatched types, expected &&_ found &'static _”, exit 101, “This is a bug in Nikaia”); it should print 3 and 1. Suspected cause: the emitter passes the literal key as &str, not &String, in the binary_search argument (emit_core_args/emit_core_calls, unchecked).
Needs:
Changelog: The prover and its integer solver allocate a third less: Fourier-Motzkin elimination works on rank forms and borrows its atoms, key texts start with room, and s.push_str(f"…") formats into s; allocator Ir of a check 1,009M -> 678M, total Ir 2,764M -> 2,190M.
Issue: #2382 meets I1-I13 except that “allocator Ir at most half” in Done when did not say whether a different allocator (a Rust dependency, a decision) is allowed; site-by-site changes reach about a third.
Owner:
Comment: #2382 Left after 3 commits (allocator Ir 678M of 1,009M): lg_contradictory_at 100M (per call it clones each name into a BTreeSet and then into ranks; one clone needs BTreeMap values_mut, refused NK1307, or a sorted Vec with binary_search, whose is_sorted proof is lost after dedup and across a call), proofs_ask 63M (hash::sha256_hex(normal.text().bytes().collect()) lowers to a Vec
.nika change in packages/logic re-lowers std and rebuilds (about 10+ minutes with the fixpoint). Two cycles were wasted: one was killed by a driver interruption, and one tried items by value, which was then refused (NK1102).Vec(capacity:) and String(capacity:) exist; reserve does not.values_mut/iter_mut on maps are refused (NK1307).binary_search needs a proven is_sorted(), and the proof is lost after dedup or across a call.TermArena::at clones the node; arena.nodes[id] is a view.sha256_hex takes &[u8], and String has no as_bytes.cmp of the check output against the baseline, the logic package tests, clippy, and pre-push. Wasted: the logic tests on the old binary, and the cycle over the refused items change. A 3-line probe would have caught that refusal first.packages/* rewrites nikaia.contracts, .derived, .lock and .proofs, and that they go into the same commit. Also that a signature change re-lowers its callers in other files, e.g. logic_alethe.rs.rules-gap: Done when asks for “allocator Ir at most half” without saying whether switching the allocator (a Rust dependency, a decision) is in scope. Site-by-site work reaches about a third.broke I5: the 50 % target came with no reference run showing that it can be reached. The flat tail (no site over 1.3 % below the top 16) makes it unlikely.knowledge: Core | measuring allocator cost per call site needs a callgrind caller breakdown of malloc/free/realloc; a script for it in scripts/ would save the first turns.Fact: Slices | a for loop over an owned Vec[T] yields views, so passing the element to a by-value parameter draws NK1102; take the parameter by ref and clone the leaf.Fact: Lowering | a .bytes().collect() passed to a &[u8] parameter lowers to a full Vec<u8> copy; String has no as_bytes view.Fact: Core | after changing a packages/logic signature, commit the package's nikaia.contracts/.derived/.lock/.proofs and the callers' lowered files with it.Generated by Claude Code
From the report of the run of issue-2382 (#2382):
Left after 3 commits (allocator Ir 678M of 1,009M): lg_contradictory_at 100M (per call it clones each name into a BTreeSet and then into ranks; one clone needs BTreeMap values_mut, refused NK1307, or a sorted Vec with binary_search, whose is_sorted proof is lost after dedup and across a call), proofs_ask 63M (hash::sha256_hex(normal.text().bytes().collect()) lowers to a Vec
Generated by Claude Code
This run is accepted as partial: allocator Ir fell from 1,009M to 678M. The remaining half waits on two things:
Status blocked until the owner decides #2383.
Generated by Claude Code