On this page

0278. In-place unique updates for long-lived state

Context

v.set(i, x) and m.set(k, v) copy the whole buffer/table (O(n)) unless regions.rs unique_sets proves the target a named local built fresh in the same function (VecNew/MapNew), never snapshotted, captured, passed or field-read; then it rewrites to VecSetUnique / MapSetUnique (0040/0053). Parameters start non-unique and Field reads reject. Long-lived state is exactly that shape: TUI impl App for M { fn update(self, e: Event) -> Step<Self> } and agent transitions do self with { items: self.items.set(k, v) }, so every event copies the table and a session of n updates costs O(n²). --report=copies (0043) already counts these as set/map-set sites. Agent state is promoted out of the transition arena after each step (0115, 0037); trt_transition_colour copies only data owned by transient arenas. with is shallow (0021): it copies a struct of handles, O(fields), not O(n). The interpreter mutates in place on *Unique ops (Rc<RefCell<…>>) and clones otherwise, trusting the proof.

Decision

Extend the static proof; add no syntax, no refcounts, no new container. A set reuses storage when its base is provably the last use of a uniquely held value. Three extensions:

  1. Field take. A record/union local is unique when every heap field it holds is unique (built from fresh or unique values, or a consumed parameter). In s = s with { f: s.f.set(k, v) } (any depth: s with { a: s.a with { m: s.a.m.set(…) } }) the read s.f is a take, not an alias, iff s is unique and its old value is dead after the with (only the new record survives). The set then rewrites to *SetUnique. Liveness is per IR block, as in 0037 placement; anything else (a snapshot let old = s, a capture, a second read of s.f live later) keeps today's copy.
  2. Consumed parameters. Compute a per-function summary consumable[i]: the body would make parameter i unique (only takes/self-updates, never retains it). At a call site where the argument is a unique local at its last use (not captured, not passed to Par, not read after), the call targets a mono clone f$own whose consumable parameters start unique. One clone per function (all consumable parameters owned); a site that cannot own them all calls the shared original. Summaries converge on the existing BTreeSet worklist; recursion and dyn/indirect calls (ADR-0275) use the shared version.
  3. Unique returns. Summary ret_unique: the result is fresh or built only from owned parameters. A unique result (and its fields, e.g. step.app) seeds uniqueness at the call site, so var model = init(); … model = model.update(ev).app stays unique across the loop, as do the run_tools_loop single-root var state (0212) and TUI Step driver loops.

Arenas. An in-place set on a container whose storage is outside the current scratch region places the inserted key and value in the container's home region at insertion (0053 already copies keys on insertion), so an old-region container never points into younger arenas. Transition promotion (0107/0115) then stops at handles trt_transition_colour reports as non-transient: an untouched or in-place-updated table costs nothing per step. Growth reallocates geometrically in the home region (0041 tip rule unchanged). Slice 1 verifies the promote helpers do not recurse into non-transient containers; if they do, it fixes that.

Visibility for agents. --report=copies gains a reason per remaining set/map-set site: alias <var> live at L:C, param not consumed (caller <f> L:C), captured, indirect call. Fixtures pin zero copy sites for the canonical TUI and agent update loops. No consuming keyword now: if inference proves insufficient after measuring, a copyfree fn marker (mirroring checkfree, E07xx allocate at implementation) can fail the build when a copy site remains; that is open question 2, not part of this decision.

Semantics are unchanged: every rewrite is unobservable; copying is always a legal result. Fuel charges are unchanged (no new calls). map.remove lands separately; when it does, the same proof rewrites it to a unique variant with one extra match arm.

Prior art

Alternatives considered

Consequences

Implementation slices

  1. Arena invariant. Confirm/fix that transition and carry promotion skip non-transient containers and that unique inserts place in the home region. runtime/trt_mem.h, runtime/trt_map.h, cgen_carry.rs, regions.rs. Frozen: no. Claude Opus.
  2. Field take + liveness. regions.rs unique_sets per-block liveness, record uniqueness, tests. Frozen: no. Claude Opus.
  3. Consumed parameters, unique returns, $own clones. regions.rs summaries, mono.rs clone emission, ir.rs verify. Frozen: no. Claude Opus.
  4. Copies report reasons + fixtures. regions.rs reporting, main.rs flags, diagnostics.md check-reports, zero-copy fixtures for tui and agent loops. Frozen: no. Claude Sonnet.
  5. Bench + spec. bench/unique-state/, LANGUAGE.md line, IDIOMS.md. Frozen: no. Gemini (agy Argon Low).

Open questions (resolved)

Resolved by maintainer on 2026-11-22: every question takes its stated default.

  1. Clone policy: one $own clone per function (default) / per ownership mask (more precise, more code)?
  2. Fallback syntax: none until the bench shows misses (default) / add copyfree fn now?
  3. Extend field take to vec elements (v.set(i, v[i] with {…}) with inner sets): later (default) / in slice 2?
  4. Bench gate: on-demand bench/ only, per AGENTS.md (default) / also a CI job?