VM copy-on-write design
Status: design only. No implementation.
Problem
Section titled “Problem”Every VM read of a non-scalar clones deeply:
- tree-walker
Env::get→Some(v.clone())(crates/zz_runtime/src/env.rs); Stmt::Decl→define(name, v.clone())and returns the original;- bytecode VM
LoadVar(env.get),LoadSlot(stack.clone()),DefineVar(clone-then-push); StoreVar/StoreSlot, call args, and returns move (no clone there).
Value::Object/Array/Dict/Str are Box-owned trees, so one
doc read copies all 16 arena arrays. The toml package appends one key
per push_node call and pays 3–5 full deep copies per key → the
~quadratic curve (2 KB ~1 s, 10 KB ~43 s on the VM).
The native engine does not have this problem: boxed structs travel as
zz_value handles (zz_clone = refcount bump, O(1)); per-step cost is
only the vec_push element-array dup.
(1) COW for arrays / objects / strings
Section titled “(1) COW for arrays / objects / strings”Wrap the backing stores, not Value:
Array(Box<Vec<Value>>)→Array(CowVec)whereCowVec = Arc<Vec<Value>>(same forDictentries,Objectfields,Strbytes).- Clone becomes an
Arcbump (O(1)). Every mutation path goes throughmake_mut:set_object_field,set_index/zz_array_set,vec.push/append/insert, dictset, string append/concat-into. make_mutonArc::get_mutfailure copies once (O(n)); the copy is exactly the copy the VM does today, so worst case equals status quo.
Rc vs Arc. The VM is single-threaded while interpreting, BUT Value
is unsafe impl Send + Sync and spawned tasks receive snapshotted/duped
values across threads (snapshot_env_cow, zz_value_dup at crossings).
Keeping Send + Sync is load-bearing for that story. The atomic bump
costs ~1 ns against deep clones costing ms: use Arc. (Rc would
force removing the Send/Sync impls and re-auditing every crossing —
rejected.)
Strings: Str(Box<String>) → Arc<String> (or Arc<str>); slicing
already shares via Bytes (Arc + window) and is the model to copy.
Concat produces a fresh buffer (no sharing to preserve).
(2) Minimal last-use move for self-reassignment
Section titled “(2) Minimal last-use move for self-reassignment”Syntactic rule, no dataflow analysis. In the tree-walker and the
bytecode compiler, when lowering x = <rhs> / x := <rhs>:
If the RHS mentions
xexactly once, that occurrence is a move (no clone), ANDxis a plain local (not a global, not captured by a closure in scope, not a path/index target), AND the single mention is in argument position of the outermost call or a direct operand — then evaluate the RHS by movingx’s slot instead of cloning it.
Covered shapes: x = f(x, ...), x = x + ..., x = vec.push(x, e),
x = m(x) (method call). The callee/operator receives owned x; the
final store overwrites the (moved-from) slot. Unit (()) left in the
slot during evaluation is unobservable: the statement completes before
any user code can read x.
MUST NOT apply when:
xoccurs 2+ times in the RHS (x = x + x,x = f(x, x)) — the second read needs the value;xis read inside a nested closure in the RHS (x = apply(|v| x)): the closure must capture a clone (capture outlives the call);xappears in a loop condition or is captured by a closure elsewhere in scope (conservative: captured names never move);- target is
obj.field,arr[i], or a global (write-back paths re-read the container — move only the value, never the container); - RHS can fail partway (
?, bounds-checked index, div-by-zero): on error the function aborts with an error value, so a moved-from slot is fine — BUT the error message path must not print the slot. Rule: moves are allowed only if the RHS is total or the error path never formats locals (auditEvalErrorconstruction); xismut-borrowed anywhere (no borrows exist today; revisit if&mutlands).
(3) Trace: doc = push_node(doc, x)
Section titled “(3) Trace: doc = push_node(doc, x)”push_node appends to ~5 of the 16 arrays and returns doc. Caller
shape (toml): doc = push_node(doc, kind, ...).
Today (refcounts are deep clones, shown as copies):
- eval arg
doc— copy #1 (wholeDoc, 16 arrays). - param bind — move.
- each
doc.<arr> = vec.push(doc.<arr>, v)— field read copies wholeDocagain (copies #2..#6),vec.pushdups that one array (O(n)), field write-back moves. return doc— move. CallerDecl/assign —Declclones (copy #7).
Per key: ~7 whole-Doc copies + 5 array dups → O(keys × nodes).
With (2) only (no COW):
- eval arg
doc— MOVE (single use indoc = push_node(doc, ...)). - param bind — move.
- field reads still clone whole
Doc(no rule fires inside — the rule is statement-level;doc.t_tblread insidevec.push(doc.t_tbl, …)mentionsdoconce… the inner statement is an expression, not an assignment todoc, so no move; each field read still deep-copies). - return/assign — moves.
Per key: ~5 whole-Doc copies (field reads) + 5 array dups. Better
constant, still O(keys × nodes). push does NOT become in-place:
vec.push receives a shared-or-not unknown array and must dup.
With (1) COW + (2):
- arg —
Arcbump. 2. field reads —Arcbumps; element arrays shared. vec.push(shared_arr, v)—make_mutcopies that ONE array once (O(n)), all other 15 arrays stay shared. 4. return/assign — bumps.
Per key: one O(n) array copy + O(1) bumps → O(keys × avg_len) total with
a tiny constant — the same complexity as the native engine. push
becomes in-place exactly when the array is uniquely owned
(Arc::get_mut succeeds), which is the common accumulator case.
(4) Semantic risks
Section titled “(4) Semantic risks”- Equality (
==,!=): deep comparison, sharing-invisible. Safe. - Aliasing through closures: captured envs clone at capture today;
with COW they would share until a write. Every write path already
routes through
make_mut(section 1 list) — audit must prove no directVec/Stringmutation exists outside it (grep mutonValueinternals). Highest-risk site:set_object_fieldwrite-back in the tree-walker, which re-reads and rewrites whole objects. - Dicts of arrays / nested sharing:
make_mutis per-buffer, so a write to a nested array copies only that array. Diagrams that print shared structure ({:?}) must not expose addresses — they don’t. strinterning: interned literals are immortal singletons; they never participate in COW (excluded like the nativeinternedflag).- Rollback hazard:
Arc::get_mut+ interior retry logic must never be reachable from two threads for the same buffer — thread crossings keep today’s deepdup, so no buffer is ever shared across threads while mutable. State this as an invariant with adebug_assert!.
(5) Expected effect
Section titled “(5) Expected effect”- toml append curve (VM): from ~quadratic (43 s @ 10 KB) to ~linear
with native-like constant (one array copy per key). Native path
unchanged (already handle-passing);
vec_pushstill copies the one array per push — that copy is required by value semantics unless the caller is proven unique owner (not knowable without (2)-style analysis in codegen; out of scope here). - String-heavy loops:
out = out + ...moves the accumulator (rule) and shares RHS temps (COW); one copy per stored value, same as the native heal.
(6) Incremental plan (gates + rollback per step)
Section titled “(6) Incremental plan (gates + rollback per step)”- Instrument (0.5 d): counters for deep-clone sites
(
env.get,DefineVar,LoadSlot) behind#[cfg(feature)]or an env flag; gate: toml-10KB run prints the copy histogram. Rollback: delete the counters. Arc<String>forStr(1 d): smallest blast radius, no nesting. Gate:cargo test+ fuzz 200 + clone-counter delta on string bench. Rollback: revert the type alias.Arc<Vec<Value>>for arrays +make_muton all write paths (2–3 d): enumerate write paths by test (grep -n "arr:"mutations); gate: full e2e + parity + fuzz 1000, zero new DIFFs; clone counters show O(1) per read. Rollback: revert (single-commit).- Dicts + objects (2 d): same gate. Rollback: revert.
- Last-use move rule (2 d): tree-walker first, then bytecode
compiler; gate: targeted tests for every MUST-NOT case in section 2
(each asserts old value preserved / error identical); fuzz 1000.
Rollback: flag-gate the rule (
ZZ_NO_MOVE=1env) before removing. - toml curve re-measure (0.5 d): accept if 10 KB < 2 s on VM.
Total ≈ 8–10 d. Each step is independently revertible; steps 2–4 are near-mechanical, step 5 carries the semantic risk.
&mut comparison (deferred)
Section titled “&mut comparison (deferred)”&mut params would give true zero-copy hot loops with caller-visible
mutation, beating COW (which still copies once per make_mut).
Deferred because it is a language feature, not an optimization: new
syntax, borrow rules in the checker, codegen for both engines, aliasing
semantics to specify, and a migration story for existing by-value code.
Do COW + move first; revisit &mut only if measured hot loops still
miss targets after step 6.