Skip to content

A List Cons spare survives val_box over a sum (#970) - #987

Merged
nicolas-abril merged 1 commit into
bendlang:mainfrom
YidaWeng:fix/970-list-rebox-spare
Sep 25, 2026
Merged

nicolas-abril merged 1 commit into
bendlang:mainfrom
YidaWeng:fix/970-list-rebox-spare

Conversation

@YidaWeng

Copy link
Copy Markdown
Contributor

Summary

  • val_box shared one fl.spares list across exclusive sum arms, so boxing Pair could splice a parked List Cons cell that the Scalar arm never saw; rebuilding Con then always allocated and the Scalar path leaked 16 bytes per call (C backend: list reconstruction leaks 16 bytes per call in 2.0.24 #970).
  • Each arm now starts with an empty spare list, flushes leftovers, and restores the outer list so Con keeps the spare; Pair allocates its own node.
  • Adds tests/run/list_rebox_sum.bend (Scalar + Pair rebox).

Test plan

  • bun bend2/main.ts tests/run/list_rebox_sum.bend — prints 7 / 3
  • Same via -o ….js (bun) and native C
  • tests/run/spare_reuse.bend still passes (interp + C)
  • Generated C for the repro: Pair heap_allocs; Cons reuses _sp_0
  • bun gates/repo.ts — PASS 46 / 46
  • Cluster gates/test.ts (no minis here)

Made with Cursor

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thank you, the fix is right: with a list built at runtime, main's generated C leaves _sp_0 (the Cons cell) neither reused nor freed on the Scalar path, and this branch heap_allocs the Pair and hands _sp_0 to the Cons rebuild, so no path orphans the cell. Two things before it can merge.

  1. The test does not reach val_box. Both calls in main take literal lists, the compiler folds them, and the C emitted for tests/run/list_rebox_sum.bend is byte-identical on main and on this branch (no rebox, head_u32 or head_nat code at all; it passes on main). The list has to come from a runtime value. Building the Value from List.length of IO.args() does it, for example:

    def wrap(n: Nat) -> Value:
      match n:
        case 0n:
          Scalar{7}
        case 1n+p:
          Pair{p, 4n}
    
    def main() -> IO(Unit):
      do IO<Unit>:
        xs : List<String> <- IO.args()
        +n : Nat = List.length(&1, String, xs)
        Unit <- IO.print(U32.show(head_u32(rebox(wrap(n) <> Nil{}))))
        IO.print(Nat.show(head_nat(rebox(wrap(Nat.add(n, 4n)) <> Nil{}))))
    

    With that shape the two emitted C files differ in exactly the two lines the fix changes. The harness cannot observe a leak by output, so please say so in the header instead of "Regression for C backend: list reconstruction leaks 16 bytes per call in 2.0.24 #970": it is a smoke test that keeps the shape compiling and running.

  2. The commit message should name the trade-off: hiding the outer spares from the arms means an arm that reused a same-size spare on main now heap_allocs, and the spare is recovered only if a same-size build follows; otherwise it is freed at the next flush. Right for the Cons shape, but a maintainer reading the log should see it.

Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

…it from Scalar (bendlang#970)

When val_box branches over a sum, each arm now starts with empty spares, flushes leftovers, and restores the outer spares after the chain. This keeps a runtime List Cons spare available for the enclosing rebuild, so the Scalar path no longer orphans it while the Pair arm allocates its own node.

Trade-off: arms no longer see outer spares. An arm that reused a same-size spare on main now heap_allocs; the saved spare is recovered only if a same-size build follows, otherwise a later flush frees it.

Co-authored-by: Cursor <[email protected]>
@YidaWeng
YidaWeng force-pushed the fix/970-list-rebox-spare branch from d84d540 to f7acd44 Compare September 23, 2026 03:47
@YidaWeng

Copy link
Copy Markdown
Contributor Author

Both points are in f7acd44 (test and commit message amended in place).

  1. The test now builds the list at runtime exactly as you sketched (wrap from List.length of IO.args). Emitting C for it on main vs this branch differs in only the two lines the fix moves:
// main                                // this branch
u64 _nd_2 = _sp_0;                     u64 _nd_2 = heap_alloc(e, cls_fit(2));
u64 _nd_3 = heap_alloc(e, cls_fit(2)); u64 _nd_3 = _sp_0;

On main _sp_0 goes into the Pair node and the Cons rebuild allocates, so the Scalar path orphans the cell; here the Pair allocates and the rebuild keeps _sp_0. The header no longer says "Regression" — it is a smoke test, notes a leak is not observable in the output, and that a literal list folds away on both. Interp, C and JS all print 7 / 3; tests/run/spare_reuse.bend still prints 10n (interp + C); bun gates/repo.ts PASS 46 / 46.

  1. The commit message now carries the trade-off verbatim: arms no longer see outer spares, so an arm that reused a same-size spare on main now heap_allocs, and the spare comes back only if a same-size build follows, otherwise the next flush frees it.

@nicolas-abril nicolas-abril self-assigned this Sep 24, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks @YidaWeng! The test now reaches val_box, and its emitted C differs from main in exactly the two lines the fix moves. The trade-off is in the log, and across every test and runtime bench no other program's C changes. Merging 🙏

@nicolas-abril
nicolas-abril merged commit de63a6f into bendlang:main Sep 25, 2026
@Lorenzobattistela Lorenzobattistela mentioned this pull request Sep 25, 2026
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