Skip to content

Runtime: return empty small-object slabs to the OS; phase-scoped arenas (study delta 7) #685

Backlog

Description

@ehartford

Part of #679. free_small_block pushes to a freelist and never munmaps (rt/rt_core.w:585-591); only >4 KiB allocations return to the OS (threshold :332, :1096). Every process's small-object RSS is its lifetime high-water mark, and the compiler's whole-program tables are grow-only by construction (AstPool raw-alloc never freed, Ast.w:545-546; intern arena 'never freed or moved', InternPool.w:33-34). During a battery, driver + workers all sit at their high-water marks simultaneously. Tier 1: release empty slabs via madvise(MADV_FREE)/munmap; verify with the debug allocator's committed-bytes counters (docs/debug-allocator.md). Tier 2: phase-lifetime arenas — drop the AST pool's backing vecs after MIR lowering. References: Zig arena-per-analysis-unit (PerThread.zig:1150 et al.); Vale frontend exits before backend starts (peak=max not sum). Feeds #681's memory numbers.

Activity

  1. ehartford commented on Jul 20, 2026

    @ehartford
    ContributorAuthor

    MEASURED ATTRIBUTION (phase-correlated RSS on a stage-equivalent compile, 18-core host) — reshapes the increment order:

    phase RSS
    frontend through sema ~1.1 GB
    + MIR lowering ~1.5 GB
    + serial per-unit generation (16 rounds) ~4.4 GB
    emit threads (W=16) 14.7 GB peak

    The dominant pre-emit holder (+2.9 GB) is the #681 gen loop's per-round sema/intern COPIES — Vec/arena-backed, so under A5/#608 they never free; 16 rounds of dead copies accumulate resident. (#681 flagged the re-copy as "optimize only if it shows" — it shows, as memory rather than wall.)

    Increment plan (revised by the numbers):

    1. Permanent instrumentation: per-phase [profile] rss= lines using the allocator's existing rt_alloc_committed_bytes (plus OS RSS) — the campaign's own gauge, lands standalone.
    2. Per-round copy release (the 2.9 GB): explicit dispose() for the compiler-internal backing stores — InternPool's arena ("never freed or moved", InternPool.w:33), AstPool raw-alloc (Ast.w:545), and the per-cg sema-copy vectors — called from cg.deinit() at each gen round boundary. These are RAW allocations owned by known lifetimes, NOT user-facing Vec semantics: disposable today, no Campaign: the wide flip — containers own their buffers (POD-element drop elaboration) #691 dependency, no language change. Gates: :drop-audit + lane + fixpoint + battery.
    3. AST pool release after its last reader: audit (via analyze facts) what codegen actually reads from the pool under per-unit gen; release the backing at the true boundary (post-gen, not post-MIR — Compiler: window codegen-unit fan-out; dispose base LLVM module and MIR before fan-out (study delta 2) #681's units read the pool during generation).
    4. Tier 1 slab release: per-slab live accounting + empty-slab MADV_FREE/munmap. NOTE the measured constraint: pages are bump-carved with size classes interleaved and freelist nodes live IN the pages, so releasing a page requires evicting its blocks from freelists — a restructure toward per-slab freelists. Sequenced LAST because tiers 2-3 are what create mass frees for it to return; on their own, the compiler's grow-only tables leave few empty pages.

    Expected composition: inc-2 alone should cut the pre-emit envelope from ~4.4 GB toward ~1.6 GB, which also directly improves every 8 GB-host cell in the #681 window model (budget = mem − frontend reserve).

  2. ehartford commented on Jul 20, 2026

    @ehartford
    ContributorAuthor

    ATTRIBUTION CORRECTION (K-scaling discriminator): the per-round holder is NOT "sema/intern copies" — InternPool is a Copy HANDLE (*mut state, InternPool.w) and the per-round Sema pass is shallow header copies. The 2.9 GB is the Codegen instance's OWN per-round tables: 40+ program-scaled Vec/HashMap fields (sema_symbol_texts: Vec[str] materialized per round, fn_values/fn_fn_types/struct registries over ~9k decls, local maps...) — dead at round end, abandoned by cg.deinit() (which disposes only LLVM-side resources), never freed under A5.

    Evidence: gen-phase RSS delta scales with round count — K=16: +2.9 GB; K=2: ~+0.8 GB (same program, plan_cost≈289k both).

    Increment-2 implementation shape (unblocked, no new runtime surface): with_vec_free/with_hashmap_free already exist in rt_core. Add Codegen.dispose_tables() invoked from deinit() — explicit frees over the table fields, with per-field safety: the A5 narrow drop glue no-ops POD containers so explicit frees cannot double-free, but fields holding non-POD payloads (Vec[str] texts are Copy-str ✓ safe; HashMap[i32, Vec[i64]] needs inner-then-outer) and any field read after deinit must be audited. Gates: :drop-audit (0 regressions), allocator lane, fixpoint, full battery, and the phase-RSS probe re-run — expected pre-emit envelope ~4.4 → ~1.8 GB, which directly widens every 8 GB-host cell in #681's window model.

  3. ehartford commented on Jul 20, 2026

    @ehartford
    ContributorAuthor

    INCREMENT 2 LANDED (8d51c2e, battery-green, drop-audit 60/0, reseeded g8d51c2e33 + asset): Codegen.deinit is a consuming move fn that disposes all 148 per-round table backings via the existing with_vec_free/with_hashmap_free primitives.

    Measured (K=16 stage-equivalent): pre-emit envelope 4.4 → ~3.6 GB; peak 14.7 → 13.8 GB; wall 149.1 → 148.0 s (free). Two boundary rules now encoded in the code: decl_source_paths is a header copy of Zcu's vec (never free — the caller reads it next round), and the container ABIs are asymmetric (Vec = inline struct, pass the field address; HashMap = handle, pass the loaded pointer — the wild-free from passing the address was named by a one-shot lldb crash backtrace).

    Open attribution (next increment's target): ~2 GB of per-round gen growth remains outside the 148 fields — candidates: str clones (codegen_owned_text per symbol per round), size-class fragmentation preventing freelist reuse across rounds (tier-1's territory), and per-round LLVM-side residue. Same K-scaling discriminator applies. Then inc-1 (per-phase committed-bytes instrumentation), inc-3 (AST release after its last reader), inc-4 (slab release).

  4. added 2 commits that reference this issue on Jul 20, 2026
  5. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): still open. Increment 2 landed (Codegen tables disposed); tier 1, releasing empty small-object slabs to the OS, did not: rt_core.w has no madvise, and small blocks still return to a freelist only.

    post-release: memory-performance work.

  6. added this to the post-v0.16.0 milestone on Oct 28, 2026
  7. ehartford commented on Nov 17, 2026

    @ehartford
    ContributorAuthor

    Tier 1 merged in #3395: a slab with no live block is unmapped by a sweep that runs when the free blocks have doubled. Measured: a program that frees 13 MB of small blocks returns it (committed at exit 19.7 MB -> 6.6 MB in the synthetic case); a self-check's time is unchanged and its peak is unchanged, as expected, since this returns memory after a peak and does not lower one. wasm keeps its slabs.

    Tier 2 (phase-lifetime arenas: drop the AST pool's backing after MIR lowering, so the compiler's own peak falls) is what remains of this issue.

  8. ehartford commented on Nov 24, 2026

    @ehartford
    ContributorAuthor

    Measured what tier 2 can buy, on a full compile of the compiler (release compiler at cf482f6, macOS arm64), with the allocation-site report (#618) printing its 4,000 largest sites and added up by the stage that allocated them.

    At the allocator's peak (604 MB live; 512 MB in the listed sites):

    allocated by live
    Sema 198 MB
    MIR 170 MB
    AST 88 MB
    interned names 26 MB
    code generation 17 MB
    front end, compile-time evaluation 10 MB

    The AST is one copy, not several: all 88 MB is the pool astpool_clone_deep makes in Sema.comptime_transform_module (ten 8 MB node columns, the extras, the strings). The parsed pool it was cloned from is already gone by then.

    So dropping the AST after MIR lowering is worth at most 88 MB of a 1,477 MB process peak (6%), and the code generator is not ready for it: Codegen.with, CodegenDispatch.with and CodegenTraits.with read the AST in 671 places. The work this issue's tier 2 names is therefore a different one first: the code generator taking those facts from Sema and MIR (D65: a later stage does not re-derive an earlier one's answer), after which the pool can be released before the units are generated. Sema's 198 MB and MIR's 170 MB are read throughout generation and cannot go early.

    The rest of the process peak, about 870 MB, is LLVM with every unit in flight (#2407's table: 1,089 MB with one unit in flight).

  9. ehartford commented on Nov 24, 2026

    @ehartford
    ContributorAuthor

    Reopened: #3584 closed this by accident (its description said "Does not close #685", and GitHub read the last three words). That PR removed 65 MB of copies; the part this issue is still open for, releasing the AST before code generation, is as measured above.

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