Skip to content

D120 stage 1: a List of values is a value (copy-on-write under an atomic count) - #2440

Merged
ehartford merged 10 commits into
mainfrom
d120-cow1
Oct 16, 2026
Merged

ehartford merged 10 commits into
mainfrom
d120-cow1

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

D120 stage 1 of docs/proposals/d120-implementation-plan.md: a List of values is a value. List[T] is Copy when T is; a copy shares the buffer under an atomic holder count in the allocation header (the str design, D111); the first mutation of a shared buffer copies it; a List of resources moves as before.

Pieces. Sema is_copy (a List generic instance follows its element). Runtime: list_holders, with_vec_retain, with_vec_release_last, with_vec_is_unique, with_vec_make_unique_bits (a compiler-owned literal has capacity 0 and is never counted, written in place or freed; over-aligned buffers keep the count in their block). Codegen: a List copy retains; the List drop drops elements and frees only for the last holder; LIST_MAKE_UNIQUE copies a shared buffer and runs the element copy glue over the fresh one. MIR: make_unique_before_mutation before every writing List intrinsic, a mut self method of a value List, an index write; a payload forwarded out of a decomposed Result carrier is taken, not retained; a value List cast into or out of its distinct type retains a copy or consumes a temporary. Runtime nets: a growing or freed shared buffer is released, never freed, so a missed path is memory-safe. emit-c: bits-only make-unique, prototypes in with_runtime.h.

Fallout under the ruling. 64 compile-error fixtures pinned a move on List[i32]; tools/d120_fixture_sweep.w moved them onto a List of a Drop type (five finished by hand). The bundle-interface once fixture, the move audit's vec field cells (copy like a str's; explicit move still invalidates), Codegen.list_data_i64's address cast.

Found on the way. The intrinsic must be last in MirIntrinsic; the make-unique hook at the wrong call path meant no mutation was ever made unique (nm showed the runtime symbol unlinked) and the compiler's own paths went wrong when self-compiled; WITH_TRACE_COW=1 prints each decision.

Battery: green (battery5, 560 s) at the commit before the final runtime change; a battery on the tip follows.

The 2% gate (tools/ab_bench.w, interleaved A B A B, baseline = installed main 007fdb2, candidate = this tree's release, workload check src/main.w):

run baseline median candidate median ratio per-round ratios
before the counted-buffer fix 45.38 s 60.34 s 1.330 1.371, 1.329, 1.316 — REGRESSION (the allocator-table lookup behind every check)
after 45.30 s 46.38 s 1.024 1.015, 1.021, 1.026 — at the line

The remaining cost is the check itself at every mutation (the plan's stage 4 hoists it) and the retain/release at copies. The pcre2/zlib suite A/B and the three microbenchmarks are not measured yet. Held for Eric's call per the ruling: not merged.

Eric Hartford added 9 commits October 16, 2026 07:39
…er's holder count in its allocation header (list_holders, with_vec_retain, with_vec_release_last, with_vec_is_unique, with_vec_make_unique_bits), Sema's is_copy of a List of values, and the LIST_MAKE_UNIQUE intrinsic name. Codegen's retain and release-last gate, the make-unique emission (CodegenDispatch, CCodegen, Analysis) and MIR's insertion before every List mutation are the rest of the stage.
…s is_copy(T). Runtime: the buffer's holders beyond the first in its allocation header's second word (list_holders; an over-aligned buffer's block holds it; a compiler-owned static buffer is never counted, written in place or freed), with_vec_retain, with_vec_release_last, with_vec_is_unique, with_vec_make_unique_bits. Codegen: a List copy retains (mir_emit_copy_glue_ptr), the List drop runs its element drops and free only for the last holder, and LIST_MAKE_UNIQUE copies a shared buffer before a mutation, then runs the element copy glue over the fresh buffer. MIR: make_unique_before_mutation on the receiver place before every writing List intrinsic (push, set, remove, clear, pop, extend, slot, iter_place, get_disjoint, restore_front, take_front) and before a mut self method of a value List. The C backend copies bits and refuses, loudly, a list whose elements carry copy glue. Test: a copy leaves its source alone, through three generations and a List[str]; a List[Tok] still moves.
…eled (§16.11), not 'v as i64', which under D120 copies the now-Copy List (D22 §6.2) and has no cast to an integer
…teps away from a shared buffer instead of freeing it, and the buffer free releases a shared buffer instead of freeing it, so a mutation or drop path the lowering missed is memory-safe; release_last never decrements the sole holder; an element write by index (xs[i] = v) makes the list unique first; no explicit & at list_data_i64 and no discarded-call binding
…he receiver is the call's first argument; the one before covered only the len/payload path, so no mutation was ever made unique — found by nm: with_vec_make_unique_bits was never linked), the intrinsic last in MirIntrinsic, a value List cast into or out of its distinct type retains a copy and consumes a temporary (the ordinary cast path, now that a List is Copy), the C backend emits one definition per string constant, and WITH_TRACE_COW=1 prints every make-unique decision
… a move on List[i32] (the canonical non-Copy type until D120) now pin it on a List of a Drop type, Tok (tools/d120_fixture_sweep.w did the mechanical part: the element type, number and string list literals, push arguments, the expectation line; five were finished by hand — a List[u8] field, an expectation's line number, two fixtures with their own Tok enum); the bundle-interface once check consumes a List of a Drop type; the move audit expects a value List's field to copy on every form, as a str's does, while an explicit move still invalidates (105 cells, 0 vs-expected, 0 vs-baseline)
…(inspect, inspect_err, context, with_context retire the subject) is taken, not retained — a value List payload held one count too many and leaked; the C backend's copy-on-write of a List[str] copies each element of the fresh buffer with with_str_clone_ref (other glue elements still refuse, loudly)
…time/with_runtime.h, and the C output makes a list unique by its bits for every element type, as it copies every value by its bits (the element copy glue is the LLVM backend's)
…bove 0: a compiler-owned literal has capacity 0, D119), not by asking the allocator — rt_payload_start_is_owned walked the slab tables at every push and free and cost a third of the self-host check (45.4 s to 60.3 s, three interleaved rounds); tools/ab_bench.w is the interleaved A/B measurement the D120 gate asks for
@ehartford

Copy link
Copy Markdown
Contributor Author

Second interleaved set on the tip (6e6d6ab), same workload:

round baseline candidate ratio
1 45.18 s 46.27 s 1.024
2 44.78 s 46.04 s 1.028
3 44.73 s 46.21 s 1.033
median 44.78 s 46.21 s 1.032

Every round above 1.02: by the ruling this is a counted regression, the merge holds, and the call is Eric's (optimize first, accept, or revisit).

Where the cost lands (sample of the candidate during check src/main.w, 12 s, 1 ms samples): with_vec_make_unique_bits + with_vec_is_unique ≈ 600 samples (~5% of the window), i.e. the uniqueness check itself at every mutation — a call, a header read and an atomic load per push through a field. rt_payload_start_is_owned no longer appears under them (that was the 33%). The plan's stage 4 (a local never copied-from is proven unique; the check hoisted out of loops) removes the check from the common var v = List.new(); for …: v.push(…) shape, which is most of the compiler's pushes; the pushes through self.xs fields keep the relaxed load the ruling priced in.

Not yet measured: the pcre2/zlib suites and the three microbenchmarks. A battery on this tip is running.

@ehartford

Copy link
Copy Markdown
Contributor Author

Battery on the tip 6e6d6ab: green, 344 s (~/.local/with-staging/d120-cow1-battery6.log). The PR stays a draft at the 2% gate.

… bar is 5%; the plan and tools/ab_bench.w's verdict follow
@ehartford

Copy link
Copy Markdown
Contributor Author

Eric, 2026-10-16: "relax the bar to 5%" — D120 Amendment 1, recorded in the decision record and the plan; tools/ab_bench.w's verdict line follows. The measured 2.4% and 3.2% are within it. Battery green on 6e6d6ab (344 s); the amendment commit is docs plus the tool's threshold, gated.

@ehartford
ehartford marked this pull request as ready for review October 16, 2026 18:10
@ehartford
ehartford merged commit 1a69c26 into main Oct 16, 2026
@ehartford
ehartford deleted the d120-cow1 branch October 16, 2026 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant