Skip to content

fix(compiler): isolate spare nodes across sum arms - #1279

Merged
nicolas-abril merged 3 commits into
bendlang:mainfrom
jasisz:fix-val-arms-spare-leak
Oct 5, 2026
Merged

nicolas-abril merged 3 commits into
bendlang:mainfrom
jasisz:fix-val-arms-spare-leak

Conversation

@jasisz

@jasisz jasisz commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

A specialized sum converted through an erased generic function can make one val_arms branch consume a parked constructor node while another branch leaves it untouched. Because the generated branches shared fl.spares, compile-time generation of the first branch removed the node for every runtime branch; the Skip path 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-tag Put control stays at 1,933,312 bytes.

This gives every multi-arm val_arms branch its own spare list and flushes branch-local leftovers, matching the branch isolation already used by val_box after #970. Spares parked before the branch remain available to code after the chain. With this patch, both 1,000,000 and 5,000,000 Skip passes 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:

  • New regression test: interpreter, emitted JS, and emitted native C all print 1.
  • Existing list_rebox_sum, array_borrow_generic_sum, and brw_audit2_fresh_box_root tests still print their expected output.
  • The original reproducer passes both --check-only and --verdict.
  • Full tsc reports the same pre-existing errors on clean origin/main and this branch, with no error in comp.ts.

@jasisz
jasisz marked this pull request as ready for review October 2, 2026 17:51
jasisz and others added 3 commits October 5, 2026 10:29
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]>
@nicolas-abril
nicolas-abril force-pushed the fix-val-arms-spare-leak branch from 209b828 to b5390f0 Compare October 5, 2026 08:53

@nicolas-abril nicolas-abril left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. val_arms: only the arm's val_to needs a branch-local file. The read: (al, k) signature change isn't needed: node_fields without tail never touches spares, and everything else it uses (seg, brwl, fresh, …) is shared between fl and the spread copy. spare_flush(al) is dead too. The only push onto spares is node_fields(…, tail=true), reached only from emit_match, which val_to can'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) => {
  2. val_box: its spare_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.spares is empty at val_box's flush never fired across the suite.
  • The leak reproducer stays flat at 1M and 5M rounds.
  • tsc reports no errors in comp.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants