Wado

WEP: Borrowed Parts — Sharing a Copy Held by an Aggregate or Handed Out by a Call

Context

Value semantics copy a value wherever it gains a second owner. Lowering skips the copy where it can prove nothing observes the difference: a final read moves, a fresh value needs no copy, and a read-only binding shares its source while nothing writes either side (Resource Ownership). The specification already allows the result: two places may be stored once (Memory Model).

The share is decided per binding. A value read into anything else is copied whatever happens to it next:

Gale's generator spends 15% of its samples in $value_copy$ (a lower bound: copies inlined into their caller do not count), spread over many call sites with none above 1%. Read at those sites, the copies fall into three shapes:

  1. A body builds a temporary out of parts it borrowed and only reads it: elements_from(elements, i + 1) extended by a suffix, &[rep.element], RepeatElement { ..*rep, kind: Star } pushed into a list the walk reads.
  2. A function returns a value holding a part of its argument: predicate_site returns a fresh PredicateSite whose bindings field is the receiver's, and stamp_alternatives rebuilds each Alternative with ..*alt.
  3. A value moves in or out of long-lived heap storage: a cache read, a cache write, self.parser_rules[rid].

The other benchmarks spend under 0.1% in copies.

Decision

Copy elision stays static. A runtime copy-on-write representation was weighed and declined: it adds a check to every in-place write of every program to pay for copies most programs no longer make.

A part shares as a binding does

A value stored into a part of an aggregate is a binding whose holder is that aggregate. It shares its source under the rules a binding shares under, read against the holder:

Whether a write reaches the part is the question a binding's share already asks of a write, by type: a push replaces a slot of the holder's array and writes nothing inside an element, so it does not reach an element's storage.

A part can leave its holder without a copy in two ways, and either refuses the share:

A closure is such a holder: it shares the storage of what it captures. Lowering lets an immutable capture share its source for as long as whatever keeps the closure is live, and that includes the result of calling the closure, which may hand the captured storage back. A closure whose body mints another closure lends nothing: the inner closure captures the same storage and can carry it anywhere the body reaches, which no holder of the outer one bounds.

Where the decision is made

The optimizer decides it, in nir/copy_elision, on the body after inlining. There the readers of the aggregate are in the body, and the heap analysis tells the objects the body allocates from the ones it was handed.

The inliner keeps each $value_copy$T helper a call until the optimizer's loop first converges, so the pass finds every copy whole in the body it lands in. It drops the call where three things hold:

It runs ahead of nir/value_copy_demote, which would otherwise turn a copy the pass can drop whole into a copy of the list's spine.

The heap analysis keeps objects apart by depth: what a load from an object reads, a field, an element or a payload, is one depth below it, and a call's summary says how deep below each parameter it writes. So a push into a local List<Tree> writes the list and its array, not the Tree parts it holds, though a Tree reaches both types.

A call lends what it returns

A function whose returned value holds a copy of a part of a parameter lends that part rather than copying it. The caller decides the copy at the call, as it does for a binding of any other place it does not own. A function that returns its argument outright already works this way.

nir/copy_elision gives such a function a $lend clone without those copies. The clone records how deep below the result the lent parts sit, and how deep below each parameter they come from. A call moves to the clone where three things hold in the caller:

The caller may still grow the list it was handed, or extend it with a parameter's elements: those write the spine above the lent parts, and the store's direction is into the result, not out of it.

A copy whose source is local to the function and reaches the result only through the copy is lent too, not dropped in place. The function cannot tell whether one source object reaches the result through more than one copy, as in a loop pushing the same value each turn. Dropping those copies would make the result's parts alias each other, while the caller's check keeps them unwritten.

A $value_copy$T hands a unit case of a variant on as it is, since the case holds nothing. Its summary takes what it shares from T, so such a copy still reads as fresh.

Roadmap

Known gaps

A recursive function lends nothing

A function on a call cycle keeps its copies. Its $lend clone would still call the original, so the inliner splicing the clone into a caller uncovers a call that lending moves to the clone again, one level per round, and the optimizer never converges. A recursive walk that returns a copy of a part of its argument, such as Gale's ctx_cast_class, pays for the copy.

Long-lived heap storage

A value read from or written to storage that outlives the body, such as a cache held by self, is copied. Sharing it needs every write to that storage, by every function, to be known not to reach the part.