Issue #1278: Precise compiler identity for the lower-std cache: key an entry on the lowered functions that actually ran

Open and answer this issue on GitHub.

performance, blocked · opened 2026-11-07 · updated 2026-11-20 · 6 comments

Status

Decided (owner, 2026-11-14): build it. This is the correct way to get fast warm lower-std runs. #1306 (option B, drop tools/ from the key) is closed as not correct: a changed compiler can lower std differently, and a cold fixpoint does not show that every std file’s output is unchanged. The measurements below say how much this gains. They are no longer a condition for building it.

Order: build after the move of crates/nikaia-std/src/tools/ to compiler/ (an issue for it follows), so the new cache is keyed on the new paths. #1160 is on develop (7427fd7a, 0faad64e), so it no longer conflicts.

Problem

NIKAIA_COMPILER (crates/nikaia/build.rs, compiler_fingerprint) hashes all of crates/nikaia-std/src/tools: 265 files and about 4,900 functions, .nika and lowered .rs alike. After any tools edit and a rebuild, every lower-std cache entry misses (about 128 s cold), and so does target/nikaia-project-tests. The gate’s fixpoint run (scripts/gate.nika gate_fixpoint) always hits that case.

Idea: a trace per cache entry

  1. Identity per function. lower-std hashes each lowered function’s Rust text, plus the type definitions it uses (enum variants, fields, constants). The Rust host (crates/nikaia/src, nikaia-logic), the rustc version and std.contracts stay one coarse block each.
  2. Trace. Every lowered function sets its bit in a thread-local bitset on entry. The bitset is cleared before each file is lowered and read out after it. The cache entry stores the list of (function, hash) that ran. For the ledger, the same per fact once #1277 makes it incremental.
  3. Hit condition. The inputs are unchanged, and every function in the trace has the same hash in the new compiler.
  4. Early cutoff. A rerun that produces byte-identical output keeps the dependents’ cache. That is the normal case in the fixpoint run.

Why it is sound. Lowering is deterministic. The path through the code depends only on the inputs and on the code of the functions that run. A changed function that did not run can only start running if a function that does run branches differently, and that function’s hash would have changed. This needs three things, and the build has to show each one holds:

Limit. An edit to a core function (expression emit, type checking) is in every file’s trace and still misses everything. That is correct, but it is not faster.

To build

Done when

Not this issue

The causes of the time itself (ledger 54 s, a fixed emit cost of about 1 s per file): #1277.

Comments

keywan-ghadami, 2026-11-19

Brief queued: issue-1278


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

Issue #1278

Your work order is issue #1278, 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/1278/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-1278.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-19

Run: issue-1278, Result: blocked

Tokens: input 30, cache read 591711, cache write 48098, output 13302 Cost: 0.81 USD, time 2 min 44 s, turns 15

Report

Result: blocked Pushed: none Tests: none run; nothing built or committed (I stopped before the first edit, on reading the code) Gate: green Deviation: none Found: The trace has no way to see memos that hand-written host Rust keeps of lowered results, so “no state outside the trace” (the issue’s soundness condition) does not hold on develop e6d25eb1. traced: (1) crates/nikaia/src/parser/mod.rs:372 bounds_beside: std::sync::OnceLock<BoundsBeside> on a shared Parsed. Whichever parallel per-file lowering walks it first fills it through lowered bounds_frame::bounds_beside, so those bits land in that thread’s bitset, and every other file reuses the value without them. (2) crates/nikaia/src/text_tiers.rs:148 static METHODS: OnceLock<MethodIndex> = ...get_or_init(|| nikaia_std::compiler::ty::method_index(library)), process-wide, filled once by lowered code. (3) crates/nikaia/src/comptime_run.rs:365 thread_local! REMEMBERED (constants computed by lowered code, reused by later files on the same worker thread), and KNOWN / DEFAULTS / AHEAD (lines 601-660). The result: a change to a function that only such a fill ran gives a cache hit with stale output. To be sound, each memo would have to record the trace bits at fill time and replay them on reuse, which is hand-written Rust in the host, or the memos would have to go during lower-std. The issue forbids that Rust (“No hand-written Rust for this beyond what the lowering writes … If that is not possible, stop and report it as blocked”). no decision yet Needs: supervisor decision: how the host’s memos of lowered results (Parsed::bounds_beside, text_tiers METHODS, comptime_run REMEMBERED/KNOWN/DEFAULTS/AHEAD) join the per-file trace. Options: hand-written Rust record/replay at each memo site, dropping them during lower-std, or moving those host parts to Nikaia first. Changelog: none Issue: broke I7: the soundness section names “no globals” as a condition the build must show, but does not check it, and the host already holds several process-wide and cross-thread memos of lowered results; rules-gap: “No hand-written Rust beyond what the lowering writes” clashes with To build’s “hit check in lower-std (crates/nikaia/src/sysroot.rs, build.rs)”, which is Rust, and with the cache identity, which compiler_identity() also serves for target/nikaia-project-tests (narrowing NIKAIA_COMPILER would make those caches unsound unless lower-std gets its own identity) Comment: #1278 Blocked before building: lowered compiler code fills host memos that hand-written Rust keeps (Parsed::bounds_beside OnceLock shared across the parallel per-file lowerings, text_tiers METHODS static OnceLock, comptime_run REMEMBERED/KNOWN thread_locals). A per-thread trace misses the functions those fills ran, so the hit check would be unsound. The emitter side looks feasible in Nikaia: emit_function.nika writes every fn header, sysroot_package.nika writes the index with LOWERED_DIGEST, and hash::sha256_hex exists. A design decision is needed for the memos, and also for lower-std’s own coarse identity, separate from compiler_identity(), which the project-test cache also uses. Friction: none Disk freed: none; target/nikaia-project-tests 1.8G, / 7.5G free, no new cache entries Attribution: Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com Claude-Session: https://claude.ai/code/session_01UVxJDukWgopUzVU2M1TjK7

Debrief

  1. What cost the most: following the lowering path by hand, from build.rs to compiler_identity, then to sysroot.rs lower_package and on to emit_function.nika, so I could see where a trace could go. The run itself was short: about 12 tool calls and no builds.
  2. What I had to look up:
    • the identity chain: compiler_fingerprint in build.rs, compiler_identity in lib.rs, and LOWERED_DIGEST, written by sysroot_package.nika;
    • sysroot_cache.nika building the key (sysroot_names, sysroot_named_key);
    • emit_function.nika writing every function header;
    • that hash::sha256_hex exists;
    • that the tools/ to compiler/ move had already landed.

    The brief could have named all of these.

  3. Checks: ls for the move, greps of the fingerprint, outlines from where.nika, and a grep for OnceLock/thread_local in the host and the lowered code. The grep that turned up the caches decided the run. Nothing was wasted. I ran no probe and no build, because the block came from reading code. So the Found: rests on reading code, not on a run, which the rules usually ask for.
  4. One change: builder.md should say that for a soundness or design issue, the issue’s own stated conditions (here “no globals”) are checked by grep before planning, as the first-turn blocked check. I found the block by luck of order.
  5. What the issue lacked:
    • broke I7: “No state outside the trace” is stated but never checked, and the host already keeps several shared caches of results computed by lowered code. A grep settles it.
    • rules-gap: the issue asks for no hand-written Rust, yet its To build names sysroot.rs and build.rs, which are Rust.
    • rules-gap: compiler_identity() also keys the project-tests cache, and the issue does not say whether lower-std gets its own identity.
    • knowledge: Measuring | the hit rate over the last N tools commits means building N compilers (about 10 minutes each), so it does not fit one run.
    • wrong I7: the order “build after the move” names no issue, and the move had already happened.
  6. Traps for the next builder:
    • Fact: Core | Host Rust holds memos of lowered-code results (Parsed::bounds_beside OnceLock, text_tiers METHODS, comptime_run REMEMBERED/KNOWN thread_locals), so any per-file trace or cache keyed on "code that ran" must account for them.
    • Fact: Measuring | compiler_identity() keys both the lower-std cache and target/nikaia-project-tests, so narrowing NIKAIA_COMPILER affects both caches.

Generated by Claude Code

keywan-ghadami, 2026-11-19

From the report of the run of issue-1278 (#1278):

Blocked before building: lowered compiler code fills host memos that hand-written Rust keeps (Parsed::bounds_beside OnceLock shared across the parallel per-file lowerings, text_tiers METHODS static OnceLock, comptime_run REMEMBERED/KNOWN thread_locals). A per-thread trace misses the functions those fills ran, so the hit check would be unsound. The emitter side looks feasible in Nikaia: emit_function.nika writes every fn header, sysroot_package.nika writes the index with LOWERED_DIGEST, and hash::sha256_hex exists. A design decision is needed for the memos, and also for lower-std’s own coarse identity, separate from compiler_identity(), which the project-test cache also uses.


Generated by Claude Code

keywan-ghadami, 2026-11-19

The report of the run of issue-1278 (#1278) judged this issue against docs/issues.md; the author fixes the issue by the rule it names (How a run judges its issue):


Generated by Claude Code

keywan-ghadami, 2026-11-19

Driver: not queued, this issue waits for #2176, which is still open (#1819). Set ready again when it is closed.


Generated by Claude Code

keywan-ghadami, 2026-11-20

Open question moved here from the closed improve issue #2177 (Found: #2176): how do the host memos join the per-file trace (Parsed::bounds_beside, text_tiers METHODS, comptime_run REMEMBERED/KNOWN/DEFAULTS/AHEAD)? Options: hand-written record/replay, dropping the memos during lower-std, or moving those host parts to Nikaia. Also: lower-std needs its own cache identity.


Generated by Claude Code

Back to Open Issues