Repository navigation
The JS lane loops a tail self call and calls plain defs directly (on top of #974) - #1040
Closed
nicolas-abril wants to merge 4 commits into
Closed
nicolas-abril wants to merge 4 commits into
nicolas-abril wants to merge 4 commits into
Conversation
JS-only OPERATIONS entries for bool_and, bool_not, bool_pick, u32_min, u32_max, f32_min, f32_max, nat_min and nat_max: each mirrors its base.bend body exactly (pick c a b = c ? a : b; min = a < b ? a : b; max = a < b ? b : a, so F32's NaN answers follow is_lt, and Nat's Succ-peeling recursion becomes one BigInt compare). Before, each call cost a run_jump trampoline into the def and, for min/max, a second one into Bool.pick; now the ternary emits in place. The C lane is untouched: intr_of without js sees no JS-only entry, so fuse, spins and the borrow inference read the same book as before. Verified through WSL on the JS lane: a scratch program exercising every entry through variable and constant arguments prints the same value interpreted and emitted, and 14 tests (num_kit, nat_ops, list_ops, list_fold, list_kit, string_ops, text_ops, float_roundtrip, u32_divmod, rand, tuple_suite, bool_algebra, fold, readback_sugars) pass run and emitted-JS alike. The cluster gates did not run (no ssh from this machine). Co-Authored-By: Claude Fable 5 <[email protected]>
A def is plain when its emitted JS returns no $JMP: a self call or a closure applied in tail position rides the trampoline, so its callers must run_loop; everything else returns through the machine stack, whose depth the def DAG bounds, since defs never call forward. plain_of reads that off the body with term_any's tail flag (a closure body counts as tail, which only errs toward the trampoline). js_call then calls a plain callee directly, tail or not, so the JIT can inline it; a self tail call keeps run_jump, and a tail call to a trampolined callee absorbs its $JMPs with run_loop in place, one bounded frame. The C lane and the device read none of this. Verified through WSL on the JS lane: 73 tests across base, eval and io pass run and emitted-JS alike (three tests/run fails are the @unsafe stderr note the local runner merges into stdout; they fail identically on the parent commit), and a 10M-iteration loop over a three-def chain runs 1.14-1.42 s before, 0.45-0.54 s after, same output and RSS. The cluster gates did not run (no ssh from this machine). Co-Authored-By: Claude Fable 5 <[email protected]>
…d off the same Src walk as C's flat On top of bendlang#974. A def's tail self call is a loop (for (;;) with per-turn const bindings, so a closure keeps its turn's values), as the C lane's flat helpers loop with WL_AGAIN. carb_book's one walk records each def's tail callees in its Src beside deps; plain_of reads them with the same in-progress false guard as flat_of, so a def is plain when every def it tail-calls is: a closure has no source and a call cycle (unsafe defs call forward) is not plain. A plain callee is called directly, every other one keeps main's run_jump in tail position and run_loop elsewhere, so a 10^6-deep mutual tail recursion runs on the JS lane again. The PLAINS map, plain_of's own walk and js_call's foreign branch go; the min/max selectors are one sel_ops row per type and bool_and joins tpl_ops. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Closed
Collaborator
Author
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Sep 25, 2026
…ess, no new Src or File fields, and only Nat.min and Nat.max become intrinsics On top of bendlang#1043, which carries bendlang#1040 and bendlang#974. The same tail loops, with less machinery: - One Tarjan pass over each def's tail callees, self calls included (tail_loops), fills a JS-lane LOOPS map. Src and File are main's again: carb_book records no tails, and plain_of, Src's plain and loop and File's loop go. The loop being emitted is read off fl.seg.def, as C's emit_jump spots a self jump; a closure's segment has no def, so its tail calls never continue the loop. A self loop is the group of one, so js_def's second walk for it goes. - Every tail call into a def is a direct JS call. Tail cycles loop, so a direct tail call can reach its caller again only through a closure, whose run_tail still bounces on the trampoline: run_jump and the plainness fixpoint go. A non-tail call keeps run_loop. - Only nat_min and nat_max stay intrinsics, on both lanes: Base's are unary and recurse min(a, b) deep. The other selectors are cheap direct calls now, and their C side measured nothing (clang inlines them), so they go with tests/base/selector_intrinsics; tests/run/ unsafe_law_tail_cycle repeated unsafe_tail_cycle. JS lane under bun, bendlang#1043 -> this, ms: cycle3 175 -> 176, mutual2 282 -> 282, listacc 148 -> 149, wrapper 137 -> 140, fib 152 -> 157 (a non-tail call keeps run_loop), bounce 263 -> 118 (a tail call into a def that bounces a closure skips the trampoline); node shows the same. bench/runtime/hashmap 5303 -> 5296. Verified: every runnable test, demo and runtime bench through the JS lane under bun prints what bendlang#1043 prints (the removed tests aside); emitted C equals main's but for tests/base/nat_ops and num_kit (Nat.min and Nat.max), which pass; 10^6-step CPS, def/closure ping-pong, List.foldl and IO loops run; gates/repo.ts 46/46 (comp.ts 63446 ttok); tsc reports its 4 existing errors, none in comp.ts. The cluster gates did not run. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
added a commit
that referenced
this pull request
Sep 25, 2026
A def on a tail cycle, or tail-calling itself, is one JS loop: loop_of finds its cycle (Tarjan over tail callees, cached in LOOPS with the other per-book caches), and js_def emits `let $pc = <its case>; for (;;) switch ($pc)` with each turn's parameters bound afresh, so a closure keeps its own. A tail call into the loop being emitted (fl.seg.def names it, as in C) sets $i and $pc and continues. Every other tail call into a def is a direct JS call: tail cycles loop, so it can reach its caller again only through a closure, whose run_tail still bounces; run_jump goes. Nat.min and Nat.max become one tpl_ops row on both lanes (Base's are unary and recurse min(a, b) deep). Src and File are unchanged. JS lane under bun against the trampoline: a three-def tail cycle 841 -> 176 ms, two-def mutual recursion 895 -> 282, a closure bounce 265 -> 118, fib 316 -> 157. comp.ts +627 ttok. Tests: nat_min_max, js_group_args, js_loop_capture, js_tail_closure, unsafe_tail_cycle. Builds on #974 and #1043 by Mathews Fernando; supersedes #1040 and #1043. The cluster gates did not run. Co-authored-by: Mathews Fernando <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on #974 by @MattCozendey: it contains its three commits unchanged, plus one commit that addresses the review there (changes requested). It's a draft so the two can be compared; whichever lands, the other closes.
What the extra commit does
WL_AGAIN. The def becomesfunction f($0, $1) { for (;;) { const a = $0; const b = $1; … } }, and the self call becomes$0 = …; $1 = …; continue;. Theconsts are fresh on each turn, so a closure created in the loop keeps its own turn's values. The loop target is cleared while a closure body is emitted, socontinuenever lands inside one. Only defs that tail-call themselves get the loop, so the emitted JS grows about 1.2% (app_slash_boss_3d: 390.5 → 395.3 KB).carb_bookalready walks each def once, recording itsdepsforflat_of. It now also records the def's tail callees in the sameSrcrecord.plain_ofreads them, and stores its answer on the record with the same in-progress-falseguardflat_ofuses. A def is plain when every def it tail-calls is plain:Src, so it is not plain;c64e021d).main's behaviour:run_jumpin tail position,run_loopelsewhere. feature/js-plains #974 ran a tail call into a non-plain def asrun_loopin place, which nests one frame per call on a cycle. With this, a 10^6-deep mutual tail recursion between two@unsafedefs printsTrueon the JS lane and throughbend file.bend. On feature/js-plains #974 both overflow the stack, whilemainand the C lane run it.PLAINSmap,plain_of's ownterm_anywalk, thefl.def = kand self-call cases injs_call, and its foreign branch (a foreign def has aSrcwith no tail callees, so it is plain). The min/max selectors are onesel_opsrow per type, withU32_BINfor U32 asBool.pickin Base returns the original word, andbool_andjoins thetpl_opsrow.comp.tsgrows by 579 tokens over #974's base, against 630 for #974 (measured withttok, asgates/repo.tsdoes).We also tried using
flat_ofitself for the JS lane (+444 tokens). It is slower on code with forks: C's flatness also excludes forks, GPU calls and non-tail calls, while the JS lane only cares about tail callees.Measurements
JS lane, bun, macOS arm64, medians of three with the builds alternating; 10M steps (3M for the loop wrapper):
mainBool.pick/and/not,Nat.minper stepVerification
mainwherevermainfinishes (and the right values where it overflows). It covers:Boolcombination;Bool.pickatStringandNat;Natnear 10^12.~) loop of 10^6, and a loop that builds closures over its counter (100 101 102 103 104) agree on all three lanes.main(b7ebee9):gates/repo.tspasses 46/46,tscreports only the four existing errors, and a local run ofgates/test.ts(this Mac, not the cluster) gives 1443/1445, failing onlygfx_clicksandgfx_window, which expect a host without a display.Not done yet
tests/base/nat_min_max.bend;🤖 Generated with Claude Code