Repository navigation
Make Array-wrapped JS marshalling stack-safe - #1370
Conversation
|
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 |
…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
left a comment
There was a problem hiding this comment.
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
.cis identical to main
Approving.
|
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. |
|
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. |
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:
bend: memory fault (machine stack overflow?).ok.ok.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.