Skip to content

Campaign: the wide flip — containers own their buffers (POD-element drop elaboration) #691

Closed

Description

@ehartford

Maintainer-scheduled 2026-07-19 as the headline campaign of the next cycle (post-v0.16.0), sequenced AFTER #685 (arenas attack the same memory pool for the 8 GB goal with zero semantic risk, and de-risk this campaign's perf story). Supersedes the provisional by-design ruling in #608 (which explicitly said "refile as that project when it's actually scheduled" — it now is). Context: #606, #608, #689 investigation.

The problem

Sema.type_needs_drop (A5): Vec[T] needs drop iff T does, so POD-element containers (Vec[i32], Vec[str], …) never free their buffers — not at scope exit, not at reassignment. Every long-running With program (e.g. #658's TCP servers) accumulates buffers forever; migrated C libraries inherit the leak; part of the compiler's own 4.9 GB frontend envelope is never-reclaimed buffers.

The leak is currently load-bearing: the compiler pervasively copies POD Vec headers as cheap value handles (e.g. Backend's per-unit backend_intern copies). If drop glue freed buffers today, stale header copies would be use-after-free and realloc-through-one-copy would strand the others. The flip is therefore an ownership migration first, a drop-glue change second.

Direction (ruled): Rust/Vale unique ownership, not ARC/GC

  • Move-only container headers + drop elaboration (assign-site + scope-exit), the Rust/Vale destination. Vale is the ownership reference (single ownership, deterministic destruction / Higher RAII).
  • NOT Swift-style ARC/CoW: pays runtime cost (atomic retain/release under the Compiler: window codegen-unit fan-out; dispose base LLVM module and MIR before fan-out (study delta 2) #681 emit threads) on every user forever to save compiler-side migration — inverts the mission's trade ("With pays COMPILER complexity").
  • NOT GC ("native by default").
  • App developers type zero new characters. f(xs) is already a D5 share-place borrow — the header-copy-as-borrow idiom is compiler-internal, and D5 is the migration tool: most compiler header copies become the real borrows the language already has. True snapshot sites (per-unit intern copies) become explicit copy/clone — visible, priced, confined to the library-maintainer tier.

Shape (increments, each battery-gated)

  1. Inventory: find every POD-container header copy in src/ via with analyze facts (effect summaries already classify share-place vs owned) — classify borrow-shaped vs snapshot-shaped.
  2. Migrate compiler source: borrow-shaped copies → share-place params; snapshot-shaped → explicit clone. Compiler must be green under BOTH ownership regimes (dual-compatible window, Parser: inner if-chain ending in 'else if' swallows the outer chain's next else at the same column #629-style flip protocol with prompt reseed).
  3. Flip the gate: type_needs_drop wide (containers always); drop glue frees POD buffers; assign-site + scope-exit + partially-moved-field paths all covered (the Place reassignment never drops the old contents — plain 'v = w' on a local leaks (shipped) #689 session's MirLower prototype showed the drop-statement side already works — the gates are at Sema+glue).
  4. Re-pin the lane: test/debug_alloc/da_pod_vec.w expectation flips to leak count=0; the Place reassignment never drops the old contents — plain 'v = w' on a local leaks (shipped) #689 reassignment matrix (session transcript) lands as da_reassign_drops_old_value.w; full battery + allocator lane + fixpoint; measure compiler RSS delta (expect meaningful frontend-envelope reduction — quantify vs the 4.9 GB baseline).

Acceptance

  • var v: Vec[i32] = Vec.new(); v.push(1) → allocator-clean at scope exit.
  • Reassignment matrix (root/field/whole-struct/branch/loop/move-then-reassign/partial-move) allocator-clean.
  • Fixpoint byte-identical; full battery; compiler self-hosts with measured RSS reported.
  • D12 heap-owner bare-self (enabled by the gate-removal commit) becomes leak-free automatically through the same assignment path.

Activity

  1. ehartford commented on Jul 20, 2026

    @ehartford
    ContributorAuthor

    INVENTORY DONE — empirically, and it reframes this campaign. The #608 "coordinated migration, pervasive header copies would double-free" premise is wrong; the flip is bounded and its core is proven sound.

    The core flip is ~10 lines, two coupled gates:

    1. Sema.type_needs_drop(Vec[T]) → 1 (was: iff T needs drop — the A5 narrow gate).
    2. CodegenDispatch.mir_emit_drop_vec_ptr: free the buffer unconditionally; run the element-drop loop only when the element needs drop (was: return false / no-op for POD elements — the codegen half of A5). This second gate is why flipping Sema alone was inert (Place reassignment never drops the old contents — plain 'v = w' on a local leaks (shipped) #689's observation confirmed).

    Generated code is double-free-free. With both gates flipped, built through stage1:

    • Vec[i32] scope-exit, Vec[str] reassign (was leak=2), nested Vec[Vec[i32]] → all leak count=0.
    • tools/drop_audit.w: 58/58 correctness cells still PASS; the only movers are the two POD-leak cells flipping to leak-free (their POD Vec[i32] buffer never freed at scope exit (debug-allocator finding) #608 expectation is now stale). Zero new double-frees. This is the key result — because Vec is already non-Copy (no impl Copy for Vec exists; by-value passing is D5 share-place, not duplication), single-ownership already holds and the flip is sound in generated code.

    The compiler-source cost is a statically-enumerated flag-day, not runtime landmines. A full self-host under the flip fails to compile (not crash) with ~140 unique sites across ~18 files (main.w 69, CodegenDispatch 67, CodegenTraits 22, MirLower 19, …): takes ownership of a non-Copy value (§3.8) and use of moved value. These are places the compiler's own code passed/moved a now-owning POD-Vec-bearing value relying on the old non-drop "effectively free" behavior. The effect system hands every one back at compile time — the #629/D7 "red build is the instrument" protocol.

    So #691 is: the 10-line flip + a 140-site ownership migration (move/copy/borrow per site), each verified by the flipped stage1 checker, gated by drop-audit + fixpoint + battery. Bounded, mechanism-proven, and — since it frees the frontend's never-freed POD buffers — a direct hit on the #685 8 GB target too. Not a post-release unknown; an in-scope flag-day. Increment 2 (the migration) starting now.

  2. ehartford commented on Jul 20, 2026

    @ehartford
    ContributorAuthor

    MAJOR UPDATE — the flip's correctness is trivial; the real obstacle is compile-memory, which vindicates the #685-before-#691 sequencing on memory grounds (not correctness).

    The 140-site "migration" was one bug. After landing #695 (move checker: partial/field moves on a divergent branch wrongly poisoned the fall-through — the return builder.body-in-branch pattern), re-applying the two-gate Vec flip drops the compiler-source migration from 140 sites → 0. The flipped compiler checks its own source clean. #608's "coordinated migration, pervasive header copies" premise is fully refuted: #691-for-Vec = a ~10-line two-gate change + the already-landed #695, nothing else. Generated code stays double-free-free (drop-audit 58/58 correctness cells; POD cells flip leak-free); user Vecs (scalar/str-reassign/nested) all leak count=0.

    But the flip roughly DOUBLES compile memory. A full self-host under the flip does not crash (correctness holds) — it exceeds the 32 GiB tripwire during stage2's build (committed=33.1 GB, reproducible without contention). Cause: making every POD Vec need drop propagates through the type graph — every struct/enum/compiler-table that contains a Vec (AstPool, MirBody, every Sema/Codegen table) now emits buffer-free drop glue at every scope exit. That's a combinatorial codegen-size explosion (inline drop glue, emitted per-site).

    So the flip is correctness-complete but memory-blocked. Two ways forward, both in the #679/#685 memory family:

    1. Drop-glue sharing — emit one __drop_T function per type and call it, instead of inlining the glue at every scope exit. This is a codegen-size win independent of the flip and likely what makes the flip fit; candidate to file under Runtime: return empty small-object slabs to the OS; phase-scoped arenas (study delta 7) #685.
    2. The broader frontend/arena headroom from Runtime: return empty small-object slabs to the OS; phase-scoped arenas (study delta 7) #685.

    Confirming now whether it self-hosts + fixpoints byte-identically with the cap lifted (correctness under memory). If yes: the Vec flip is done modulo the drop-glue-size work — exactly the #685-first ordering, now proven necessary on the numbers.

  3. ehartford commented on Jul 21, 2026

    @ehartford
    ContributorAuthor

    CORRECTION — two claims in the "MAJOR UPDATE" comment above were wrong. Retracting both.

    1. "140 sites → 0" was mis-measured. The site-count check OOM'd before it reported; a grep over the truncated output saw 0 and I read that as "flipped compiler checks its own source clean." It does not. The flip is not a single-#695 fix — it unmasked a family of latent move-checker bugs, because widening type_needs_drop(Vec[T]) turns every POD Vec into a drop-tracked, single-owner value and routes a large swath of previously-untracked code through the loop/branch move checker for the first time. Two are now fixed:

    Residual sites remain to be worked. The migration is a small number of shared checker root causes, not 140 independent edits — but it is not zero.

    2. The "flip doubles compile memory via drop-glue combinatorial explosion" diagnosis was wrong. The ~100 GB was not codegen-size from inlined drop glue. It was #695's concat-without-dedup in the move checker (my own uncommitted code): a field moved on both arms was re-added to the moved-field set at every branch merge, growing the arrays ~2^N across nested branches, and sema_clone_i32_vec (called per branch) then blew to ~100 GB. The dedup fix (membership check before union, committed ee4e5f4) takes the flipped self-host from ~100 GB → ~1.1 GB. The drop-glue outlining I built (4295801) is a real, behavior-neutral codegen-size win, but it did not reduce the flip's memory — measured identical before/after. So the flip is not memory-blocked on drop-glue; that whole line was a misdiagnosis from reasoning instead of profiling. (sample naming my own function was what finally settled it.)

    Corrected picture: the flip's blocker is a family of latent move-checker bugs, not codegen size. #695 and #696 are fixed and committed; the class is now guarded by a move-checker verdict matrix (tools/move_audit.w / with build :move-audit, the compile-time analog of :drop-audit). Next: work the residual sites and re-measure the flipped self-host end to end.

  4. ehartford commented on Jul 21, 2026

    @ehartford
    ContributorAuthor

    STATUS after #695/#696 fixes + reseed — the memory blocker is SOLVED, and the residual is (again) mostly a shared checker bug, not a migration.

    Memory: peak 1.16 GB (was ~100 GB) for the flip-enabled compiler checking its own 160k-line source. The #695 partial-move-merge dedup fix holds under the full flip. This was the actual blocker; it's gone.

    Method: reseed to the #695/#696-fixed compiler, apply the two-gate flip (Sema.type_needs_drop(Vec) → 1; CodegenDispatch.mir_emit_drop_vec_ptr → always free the buffer, drop elements only when the element needs drop), build a flip-enabled stage1, run stage1 check src/main.w.

    118 residual errors, in three classes:

    • 57× mut receiver is too weak; requires move fn — a SHARED CHECKER BUG, not 57 edits. The flip makes Vec fields drop-bearing, and the effect inference then wrongly escalates mut fn → move fn for any method that reassigns/drops such a field. Confirmed on Zcu.reset_for_new_invocation, which is a pure in-place reset (self.extra_source_names = Vec.new() …) — unambiguously mut, never consuming self. Running migrate_receivers here would wrongly consume self on 57 compiler methods and break everything. Root-cause this like Move checker: partial (field) move on a divergent branch wrongly poisons the fall-through #695/Move checker: value moved BEFORE a loop is wrongly flagged at the loop's back-edge #696 (effect inference over-escalating on drop-bearing-field reassignment); expect it to clear most of the 57.
    • 52× this parameter takes ownership … pass move/copy — the predicted move-arg sites. migrate_method_arg_moves handles these, but it (correctly) refuses while unrelated errors exist, so the receiver bug must be fixed first. Some of these 52 may also be false once the receiver bug is fixed — re-check before migrating.
    • 2× use of moved value — likely genuine use-after-move; hand-fix.

    Takeaway: the flip is still a family of shared checker bugs surfaced by widening type_needs_drop, not 111 independent edits. Generated code is already correct (drop-audit 58/58). The flip itself is 2 edits (reverted to keep the tree clean). Next focused session: root-cause the receiver-escalation bug, re-check, then migrate whatever genuine move-args remain + the 2 real moves.

  5. 3 remaining items

  6. ehartford commented on Aug 31, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-08-31: CLOSE (campaign complete). The wide flip landed and verifies: POD Vec buffers free at scope exit AND at reassignment (--debug-alloc leak count=0), test/debug_alloc/da_pod_vec.w expects leak count=0, and 8dc76fa retired the #608 leak-by-design pins. The RSS/8GB follow-through is tracked by the open #679 campaign.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions