Skip to content

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
bendlang:mainfrom
nicolas-abril:js-plain-loops
Closed

nicolas-abril wants to merge 4 commits into
bendlang:mainfrom
nicolas-abril:js-plain-loops

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

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

  1. A tail self call is a loop on the JS lane, as the C lane's flat helpers loop with WL_AGAIN. The def becomes function f($0, $1) { for (;;) { const a = $0; const b = $1; … } }, and the self call becomes $0 = …; $1 = …; continue;. The consts 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, so continue never 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).
  2. Plainness is read off the same walk as C's flatness. carb_book already walks each def once, recording its deps for flat_of. It now also records the def's tail callees in the same Src record. plain_of reads them, and stores its answer on the record with the same in-progress-false guard flat_of uses. A def is plain when every def it tail-calls is plain:
    • a self call loops;
    • a closure has no Src, so it is not plain;
    • a def on a call cycle is not plain (unsafe defs can call forward since c64e021d).
  3. Every non-plain callee keeps main's behaviour: run_jump in tail position, run_loop elsewhere. feature/js-plains #974 ran a tail call into a non-plain def as run_loop in place, which nests one frame per call on a cycle. With this, a 10^6-deep mutual tail recursion between two @unsafe defs prints True on the JS lane and through bend file.bend. On feature/js-plains #974 both overflow the stack, while main and the C lane run it.
  4. Removed: the PLAINS map, plain_of's own term_any walk, the fl.def = k and self-call cases in js_call, and its foreign branch (a foreign def has a Src with no tail callees, so it is plain). The min/max selectors are one sel_ops row per type, with U32_BIN for U32 as Bool.pick in Base returns the original word, and bool_and joins the tpl_ops row.

comp.ts grows by 579 tokens over #974's base, against 630 for #974 (measured with ttok, as gates/repo.ts does).

We also tried using flat_of itself 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):

workload main #974 this
chain of three small defs per step ~1.1 s 0.30 s 0.11 s
U32 min/max, Bool.pick/and/not, Nat.min per step ~58 s 0.42 s 0.23 s
a wrapper that tail-calls a self-recursive loop 0.45 s 0.36 s 0.13 s
a step that tail-calls a small forking helper — 0.30 s 0.14 s

Verification

  • A cross-lane program prints the same on the interpreter, JS and C as main wherever main finishes (and the right values where it overflows). It covers:
    • F32 NaN and ±0, the U32 extremes and every Bool combination;
    • Bool.pick at String and Nat;
    • Nat near 10^12.
  • The mutual recursion above, a template (~) loop of 10^6, and a loop that builds closures over its counter (100 101 102 103 104) agree on all three lanes.
  • Merged onto main (b7ebee9): gates/repo.ts passes 46/46, tsc reports only the four existing errors, and a local run of gates/test.ts (this Mac, not the cluster) gives 1443/1445, failing only gfx_clicks and gfx_window, which expect a host without a display.

Not done yet

🤖 Generated with Claude Code

MattCozendey and others added 4 commits September 24, 2026 08:01
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]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Superseded by #1061, which builds on #1043 (which includes this PR's commits).

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]>
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