Repository navigation
fix(compiler): isolate spare nodes across sum arms - #1279
Conversation
The arm's read and the trailing flush do not touch spares: node_fields without tail never parks one, and val_to never reaches a tail match, so only the val_to call needs a branch-local spare list. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A val_box arm only takes spares (ctr_build) and never parks one, so its branch-local list is always empty at the flush; it now builds in place like the val_arms arm. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
209b828 to
b5390f0
Compare
nicolas-abril
left a comment
There was a problem hiding this comment.
Thanks! The diagnosis is right: val_arms was the one emit_chain caller that built nodes without isolating fl.spares per runtime branch. The single-arm val_box fast path called ctr_build on the shared list, so the spare was taken at compile time for every branch while only one branch used it at runtime. I reproduced it on current main (17.5 MB → 81.5 MB max RSS from 1M → 5M rounds, 16 B/pass), and with the fix it stays at ~1.5 MB. Giving arms an empty list, rather than emit_match's copy-and-flush, is the right choice here: it keeps the parked Cons reusable after the chain.
I rebased onto main and pushed two simplifications on top:
val_arms: only the arm'sval_toneeds a branch-local file. Theread: (al, k)signature change isn't needed:node_fieldswithouttailnever touchesspares, and everything else it uses (seg,brwl,fresh, …) is shared betweenfland the spread copy.spare_flush(al)is dead too. The only push ontosparesisnode_fields(…, tail=true), reached only fromemit_match, whichval_tocan't reach, so the arm's list is always empty at the flush. The fix becomes:const ws = (al: File, k: Name) => read(k).flatMap((f, j) => val_to(al, f, lay.arms![k][j]).ws); ... ws({ ...fl, spares: [] }, k).forEach((w, n) => {
val_box: itsspare_flush(bl)is dead for the same reason, so its arm now builds in place the same way:file_push(fl, `${out} = ${build({ ...fl, spares: [] }, k)};`)
Verification:
- The emitted C is byte-identical to your original commit for all 702 compilable tests. Only the new test's C differs from main.
- A runtime assertion that
bl.sparesis empty atval_box's flush never fired across the suite. - The leak reproducer stays flat at 1M and 5M rounds.
tscreports no errors incomp.ts.
One note on the test: on main it also prints 1 (the leaking build prints 1 at 1M rounds too), so it pins the codegen shape but can't catch the leak coming back. Nothing in the suite measures memory today, so I left it as is.
A specialized sum converted through an erased generic function can make one
val_armsbranch consume a parked constructor node while another branch leaves it untouched. Because the generated branches sharedfl.spares, compile-time generation of the first branch removed the node for every runtime branch; theSkippath then allocated a replacement and leaked the original node on each pass.On macOS arm64 at
e1ed243502764d013471a7338bc7ab2c8e62ff98, the reproducer grows from 17,924,096 bytes maximum RSS at 1,000,000 passes to 81,920,000 bytes at 5,000,000 passes, exactly 16 bytes per extra pass. The opposite-tagPutcontrol stays at 1,933,312 bytes.This gives every multi-arm
val_armsbranch its own spare list and flushes branch-local leftovers, matching the branch isolation already used byval_boxafter #970. Spares parked before the branch remain available to code after the chain. With this patch, both 1,000,000 and 5,000,000Skippasses stay at about 1.9 MB maximum RSS.The regression test preserves the generic sum conversion and repeated list rebuild that trigger this compiler shape. The leak itself is not observable in Bend output, so the direct RSS measurement used the same program with 1,000,000 and 5,000,000 rounds.
Validation:
1.list_rebox_sum,array_borrow_generic_sum, andbrw_audit2_fresh_box_roottests still print their expected output.--check-onlyand--verdict.tscreports the same pre-existing errors on cleanorigin/mainand this branch, with no error incomp.ts.