Skip to content

Make Array-wrapped JS marshalling stack-safe - #1370

Merged
Lorenzobattistela merged 3 commits into
bendlang:mainfrom
oxura:fix/array-marshal-depth
Oct 7, 2026
Merged

Lorenzobattistela merged 3 commits into
bendlang:mainfrom
oxura:fix/array-marshal-depth

Conversation

@oxura

@oxura oxura commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Closes #1346.

Memoize Array converters alongside ADT converters and give composite converters one shared explicit work stack. Nested arrays and mutually referenced converters no longer invoke each other recursively. Work items use a flat stack rather than allocating a tuple per field; scheduling visits fields/elements left-to-right. Existing ownership stays unchanged: arrays are converted in place, ADT nodes are copied, and absent array elements are skipped.

The permanent regression traverses 20,000 nodes through singleton Arrays. The foreign effect checks each Nat and changes the leaf; Bend checks the entire returned tree. Both the JS twin and the real C twin perform that transformation.

Verification on Linux:

  • Corrected regression on unchanged main: bend: memory fault (machine stack overflow?).
  • Source IO CLI and emitted JS under Bun 1.3.13 / Node 22.23.1: ok.
  • Native binary with 1 and 4 CPU threads: ok.
  • Exported host API: 20,001 nodes and two branches, BigInt round trip and existing array identity preserved, on Bun and Node.
  • Existing Nat, closure and 50,000-value marshalling fixtures exercised.
  • Repository gate: PASS: 50 / 50, including the staged new fixtures.

No changes to bend2/bend.ts, native runtime/emitter, gates/caps, GUIDE or release changelog. No macOS/GPU/mini-cluster verification claimed.

@oxura

oxura commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the repo-gate cap failure from merging with the newer upstream compiler. Array and ADT converters now share the generated function/work-stack boilerplate instead of duplicating it. On fa4ba634 plus all four new fixes, the unchanged repository gate reports PASS: 50 / 50. Re-exercised the 20,000-node IO round trip and the branching exported API on Bun/Node. No cap changes or unrelated comment removal.

…own converter

The shared work stack made flat Array<Nat> and wide trees 1.5-2x slower
in JS: every element was pushed and popped. Leaves are converted where
they sit, so only composite parts go on the stack.

@Lorenzobattistela Lorenzobattistela 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 @oxura, this is the right fix: the Array converters now use the same memoized, explicit-stack path as the ADT ones, so deep data under Arrays no longer uses host recursion. I confirmed #1346 overflows on main and passes here in the interpreter, bun and node. It also handles deep non-last children, mutual types and .mjs up to 1M nodes.

I pushed one commit on top (7ba953c). With the shared work stack, every element went through a push/pop, so flat data got slower in JS. On 1M Array<Nat> it was 1.6x slower on bun and 2x on node. Nat and function parts are now converted in place, and only parts that have their own converter go on the stack. Flat arrays and lists are back to main speed. A 1M-leaf tree is still about 1.2x slower on bun and 1.4x on node, which is what any explicit stack costs per node.

Checked against main:

  • repo 50/50 (comp.ts 63,931 < 64,000), ping 47/47
  • test 1583/1583 (main 1582 + the new test), safe agrees with main +1
  • perf matches main (only the usual noise)
  • the generated .c is identical to main

Approving.

@Lorenzobattistela
Lorenzobattistela merged commit 022ff6e into bendlang:main Oct 7, 2026
1 check passed
@oxura

oxura commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the leaf-conversion fast path and the measured flat-array comparison. Keeping direct Nat/function conversion in place while queueing only recursive converters preserves the stack-safety fix without taxing every scalar element. I will use the merged version as the next campaign baseline.

@oxura

oxura commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the in-place Nat/function fast path. Keeping your follow-up commit intact; the explicit-stack regression coverage and restored flat-array performance are both important.

@Lorenzobattistela Lorenzobattistela mentioned this pull request Oct 13, 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.

JS effect marshaller overflows the host stack when recursion is nested under Array

2 participants