On this page

0219. Amortized driver carry reclamation

Context

Ordinary C loops retain replaced carry buffers until the function's arena is reset. The runner has a name-based exception that copies the full carry on every iteration. A driver that changes a few rows of a large transcript needs reclamation with a cost tied to new allocations.

Decision

Select by IR shape. A function's first qualifying top-level loop must write exactly one heap-bearing local declared outside that loop, a record, and placement must identify a fresh retained replacement or promotion into that record within the loop. Scalar counters are allowed. Functions reachable in parallel workers use ordinary placement because a worker trap can unwind without running the function's C epilogue. Also decline functions that can overwrite a unique vector of heap-bearing items, which could insert younger references into older storage. Other loops use ordinary placement. This needs neither source syntax nor manifest data.

Use the same private colour tables for the runner and ordinary drivers. Start in the ordinary arenas. When retained growth reaches 64 KiB and the process has page headroom, route subsequent allocations to a private colour table. Keep the ordinary prefix and first epoch alive, since older aliases can share their backing. Short calls keep ordinary placement and return.

At a private loop head, reset iteration scratch and measure retained growth since the last successful flip. Wait for growth to reach at least the last compacted size and the 64 KiB floor. Size the carry's private backing using at most that growth as a budget. Count the same aligned reservations as relocation. If sizing or page headroom fails, wait for twice the growth before trying again. If the copy fits, relocate private backing to the alternate table, then reset the old one.

Every successful flip's copy fits within the new bytes accumulated since the last flip, including adopted worker storage. The size pass stops when its budget is exhausted. Doubling spaces out failed passes. For bounded live carry and bounded per-iteration allocations, the trigger bounds arena growth independent of iteration count. Private aliases may expand during a flip. The size pass charges each such copy before it happens. Caller-owned and first-epoch backing remain borrowed. The page budget must fit live backing, iteration scratch, and the temporary relocation.

Lifetime contract

Only the record may acquire heap references across the loop back-edge. Read-only parameters and locals initialized before the loop still refer to the original arenas, caller-owned storage, or literals. Region placement promotes references out of younger scratch as before. Vector and map tip checks and the separate uniqueness analysis remain in force. The copy walks private backing, including views and adopted data. Older containers cannot acquire references into a private epoch. Vector and map tip updates check the destination arena, and unique heap vector writes are excluded. On a break, retain the active arena through the function suffix. Relocate each backing allocation to its original colour in the new table. A carry can include older result storage even when the record itself belongs to function scope. Preserving both colours keeps those lifetimes and keeps tip appends in their destination arenas. Keep spare tip capacity up to twice the visible length. Ordinary return placement keeps result references out of scratch. At return, move reachable private result buffers into the original result arena and keep older backing intact. If the measured copy exceeds the private result arena's size or cannot fit the page budget, transfer the private result arena into the caller's cleanup chain. The transfer preserves sharing and lets the caller's mark reclaim the arena. The interpreter retains its existing value representation.

Alternatives