Skip to content

Compiler: serialized prelude snapshot keyed by compiler fingerprint (study delta 6) #682

Open

Description

@ehartford

Part of #679. Every compile re-lexes/parses/semas the std.prelude transitive closure from embedded source — 13 modules, ~71 KB unconditional (src/Resolve.w:185-201, src/compiler/Frontend.w:1333-1376) — in EACH of the ~2000 test child processes per battery, every stage compile, and every build worker. No serialized AST/Sema artifact exists. Adopt Zig's shape (Zcu/PerThread.zig:483-700: per-file ZIR cached on disk per compiler version per machine, stat-gated, cross-process locked): a versioned mmap-able snapshot of the prelude's AST pool + Sema tables, keyed by compiler fingerprint, loaded at prelude injection. The flat SoA i32 tables are unusually friendly to raw dump/mmap. Ship for the prelude alone first (highest reuse count); it is also the enabling prerequisite for separate compilation. Expected: O(100ms) × ~2000 invocations per battery = minutes of pure redundancy removed.

Activity

  1. added 3 commits that reference this issue on Jul 18, 2026
  2. ehartford commented on Jul 19, 2026

    @ehartford
    ContributorAuthor

    DESIGN + MEASURED PREMISE (2026-07-18 session; implementation to start fresh against this comment).

    Premise correction — the direct win is seconds, not minutes, on the 18-core host. Full hello-world with build: 90 ms wall total. Prelude-attributable frontend work per invocation (WITH_PROFILE): resolve 3.6 + imports 2.7 + comptime 6.7 + frontend.sema 5.5 + sema 4.9 ≈ ~25 ms (392 prelude decls, 342 types, 129 MIR bodies — backend/link dominate the rest). × ~1900 battery children ≈ 47 s CPU ≈ ~3–5 s wall at 16-wide; stage compiles pay it 3 × 25 ms ≈ nothing. The study's O(100 ms) × 2000 = "minutes" figure does not reproduce here (may hold on slower hosts). The campaign's real value is as the enabling pathfinder for #684 separate compilation — the snapshot machinery (serialize/load a compiled-module frontend prefix) IS the module-interface machinery. Maintainer may want to re-weigh #682-now vs #683-first on these numbers; order unchanged until then.

    Architecture facts (read from source):

    • Injection: Frontend.parse_with_prelude_first_mode parses a synthetic use std.prelude into a fresh pool, then USER source into the same pool; Resolve (src/Resolve.w:176-204) then lexes/parses each imported std module into the same growing pool/intern. Prelude-closure AST nodes and intern IDs therefore land AFTER user decls at user-dependent offsets — a raw snapshot is impossible in today's order.
    • Sema (src/Sema.w:355+) is SoA Vec[i32] parallel arrays (type table, sig tables, effects) + HashMap[i32,i32] lookups + HashMap[i32,str] pretty names. Vecs dump/mmap raw; HashMaps must be dumped as sorted pair arrays and rebuilt on load (iteration-order-safe).

    Increment plan (each its own battery):

    1. Prelude-first prefix ordering: resolve/parse the prelude closure into the pool BEFORE user source, so the closure occupies a byte-deterministic pool/intern prefix for a given compiler fingerprint + prelude mode. This reorders decl order globally — the risky, semantics-adjacent step; land alone, watch diagnostics-order and codegen-order effects; fixpoint gates determinism.
    2. Snapshot dump/load of the frontend prefix: after processing exactly the prelude closure (pre-user), dump pool SoA + intern + resolve tables to a versioned file keyed by fingerprint (WITH_VERSION/unstamped hash + prelude mode), stat-gated, cross-process advisory-locked (Zig Zcu/PerThread.zig:483-700 precedent). Load path replaces the 13-module lex/parse/resolve.
    3. Sema/comptime prefix capture: snapshot post-sema (+ comptime side effects) state for the prelude prefix — the largest per-invocation slice (~17 ms of the 25); hardest correctness surface (frozen-Sema invariants, audit:all must stay clean).
    4. Wire + measure: test-runner children and build workers consume the cache; measure battery delta end-to-end; record in docs/build_time_log.md.

    Fingerprint/cache notes: key on with.unstamped identity (D13 — stamp is provenance), prelude mode (core/alloc/full), and snapshot format version; store under out/.build-state/ (per-tree, like verdicts), NOT a global cache, so a stale-seed machine can never load a foreign snapshot.

  3. added 2 commits that reference this issue on Jul 19, 2026
  4. ehartford commented on Jul 20, 2026

    @ehartford
    ContributorAuthor

    INCREMENT 1 LANDED (prelude-first prefix ordering) — the snapshot boundary now exists.

    What changed: the frontend expands the prelude closure into the pool BETWEEN the synthetic use and the user source (expand_prelude_closure_frontend), so the closure's AST nodes, intern symbols, and file ids occupy a prefix that is byte-deterministic for a given compiler fingerprint + prelude mode. The resolve sidecar skips the prefix for root-module attribution (root_prefix_skip); the three-tier import merge collects the prelude tier from the recorded prefix range and dedups its re-expansion to a no-op. Final decl ORDER is unchanged (the merge already emitted prelude → user imports → root), so sema/codegen semantics are order-identical. Three dead functions absorbed (inject_prelude_frontend, parse_with_prelude_first{,_mode}).

    Determinism evidence: across two structurally different user programs, 218/221 AST-level prelude facts are byte-identical (analyze facts diff); the 3 movers are std/result.w trait-default materializations synthesized post-parse — they don't exist at the snapshot boundary and are recomputed after load. Fixpoint held byte-identical through the reordered allocation in every battery run.

    Bring-up findings (both real, both from the reordering shifting which garbage got read):

    • get_generic_inst_base/arg/arg_count read d-slots from NON-inst types; under the new layout the garbage was u7, so a pending unannotated Vec.new() receiver typed push(100) as a 7-bit int (wrong inference, miscompiled reads, a runtime abort in the disjoint-slice spec test). The accessors are now kind-guarded — the whole class is dead. audit:all was blind to it → Sema audit gap: garbage generic-inst slot reads were invisible to audit:all; add a typed-expr well-formedness audit #692 (typed-expr well-formedness audit).
    • Four --dump-mir golden phase tests pin intern-derived field tokens (.f4); the prefix shifted them (.f87). Shapes verified identical old-vs-new (same locals, same sound copy-form in the alias-taint cells); pins updated.

    Perf: parse work is net-unchanged (the closure parses once, earlier; the resolve sidecar's throwaway parses are as before).

    Next increments (unchanged from the plan): 2 — serialize/mmap the prefix (pool SoA + intern + resolve tables), fingerprint-keyed under out/.build-state, advisory-locked; 3 — sema/comptime prefix capture; 4 — wire into test children + measure. Note for inc-2: the snapshot cut is immediately after expand_prelude_closure_frontend returns, before user parse — the 3 trait-default movers above confirm nothing synthesized exists yet at that point.

  5. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): still open. Increment 1 (prelude-first prefix ordering) landed; increments 2–4 (serialize the prefix, capture sema/comptime state, wire into test children) did not: nothing in src/ writes or reads a prelude snapshot.

    post-release: build-performance work.

  6. added this to the post-v0.16.0 milestone on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions