Skip to content

comp.ts back under its cap by removing mechanisms at their root: a global File, one way to read a shared cell, and effects that park themselves - #1281

Merged
nicolas-abril merged 21 commits into
bendlang:mainfrom
nicolas-abril:comp-simplify
Oct 5, 2026

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Context

comp.ts sits just under its permanent 64,000 cap: main is at 63,971 ttok. This PR brings it to 62,978.

The cuts are simplifications at the root rather than trimming: each removes a mechanism that existed only because another part behaved a certain way, or a second way of doing one job. Each was traced to the commit that added it, to check the reason no longer holds.

Changes (one commit each)

  • The compiler's File is global and holds every cache. The caches were module globals cleared per book, so the compiler already held one book at a time, yet 40 functions took book or fl only to reach it. FL is now the book being compiled and everything memoized about it; FL.memo holds what one def's walk memoizes. type File lists every field, FL.memo's too. show_main now reads this book's caches, not the previous book's (it ran before the caches were cleared): this restores Initialize compiler caches before building printer layouts #961's fix, which main has lost. Two books compiled in one process now each emit what a fresh compile emits; on main the second either gets the first's stale layout or throws.
  • Intr.call goes: an intrinsic that arr_op lays out has C: null; the flag's third use (blocking a fold rebuild) was redundant.
  • A foreign def's segment opens and returns through emit_open/emit_put, like any def's.
  • No scan for the word undefined in the emitted C: a leaked one is an undeclared name, which clang refuses.
  • corpus_eval is one loop with one post-condition, and three guards for states that cannot occur go.
  • A redirect cell has one read, term_peek (one relaxed 64-bit load, as A shared Array's redirect is read without a race and without an atomic on the device, once per spin (#975) #1155 made blk_loc do), and rfc_view goes; ctr_take acquires only as the last holder, before it owns the node. Shared Array redirect lookup races with atomic reference-count updates #975's reproducer under ThreadSanitizer: 0 reports in 10 runs on both trees (and 6 with the read made plain, as a control).
  • A JS foreign op is a node tagged with its def's name, as in C, and an IO.OP match keeps its default arm in both lanes, so the JS-only $FFI goes. A user's case _ in an IO.OP match takes only an Emit or a Halt: a request reaching it fail-stops in every lane.
  • An effect that waits parks itself: io_eff takes no need, and the loop runs every request one way. IO.sleep parks on its deadline; TCP.accept and UDP.recv_from park until their socket is readable before their first try, as the need made them, so a backlog cannot starve the timers. The C effect table is not static, so it is zero fill rather than 512 KB of every binary.
  • The book and the emitter's scope are two things: FL is the File being compiled, and a Scope (segment, local names, bindings, spare nodes, def) is where the emitter writes, so a nested arm, closure or fork copies only its scope.

The series also tried opening a constructor's own binders as DUMMY in place of facts_hot's local flag, and reading Nat literals past 256 without lit_call's U32.to_nat shim. Review found a C miscompile from the first and a compile-time blow-up from the second (7.3 s and 4.4 GB against 0.3 s and 324 MB on main), so both are main's again in later commits; a squash leaves neither.

Breaking

  • The effect API: io_eff(cid, run) takes no need. A third argument is a compile error in C, and in JS io_eff throws ("<k> takes no need: an effect that waits parks itself").
  • IO.OP matches: a request reaching a user's case _ in an IO.OP match fail-stops in C too. Main already refused it in JS and bend run, but ran the user's arm on it in C. Every waiting effect already had the self-parking form (io_wait_on / io_park_on), so nothing becomes impossible; guide/EFFECTS.md says so and shows how to wait on time alone. All 63 C registrations in bend2/effs and tests are updated.
  • JS registry check: a foreign def without a registered JS effect fails when its request runs, naming the def ("bend: no effect registers <def>"; C says "an alien request"), not at load.

Checks

On main 1a2e5cd (after the review fixes and rebases), on an Apple M4:

  • Emitted output: for all 1633 .bend files under tests/, bench/ and demos/, each commit that says its output is byte-identical emits byte-identical C, JS and .mjs against its parent. Each review fix changes only what it says. Every rebase was checked file by file: an output main's new commits leave alone is unchanged, and the ones they change hold both main's change and this PR's.
  • New tests: udp_backlog_timer (a 0 ms timer fires before 40 queued datagrams drain), tcp_backlog_timer (the same before 20 queued connections are accepted) and request_wildcard (an IO.OP case _ takes a Halt but refuses a request) fail without their fixes and pass with them, in C and JS; effect_unregistered pins the named refusal.
  • Local full suite (gates/test.ts's shard script on this Mac): 1548/1550. The two failures are gfx_clicks and gfx_window, which need a display.
  • Cluster gates (gates/_run.ts): repo 50/50, test 1550/1550, perf 121/124 and 124/124 on reruns, against main's 88/124 to 124/124 on the same minis. The cells over 1.15x were PAR-GPU times under 0.1 s, on both trees. ping was skipped because the site repo is not checked out here.
  • Review: two independent passes over the changed compiler and runtime code (generated C and JS against main across the corpus, a redirect-cell stress run at 8 threads, the IO.OP match cases in every lane) found no further bugs.
  • Not re-run: the JS-lane timings and the commit audit below.

The original series, before the rebases and the review fixes, on an Apple M4 against main (fb6f32a):

  • Emitted output: the compiler-only commits keep the emitted C, JS and .mjs byte-identical for all 1608 .bend files under tests/, bench/ and demos/; the rest change it as described in their messages.
  • Local full suite (gates/test.ts's shard script run on this Mac; every lane, Metal for the ! tests): all pass but gfx_clicks and gfx_window, which need no display and fail on main too.
  • Cluster gates: repo 49/49, test 1526/1526, perf 115/117 against main's 117/117 back to back (geomeans: compiler 1.032, SEQ 0.993, PAR-CPU 1.015, PAR-GPU 1.035). The two cells over 1.15 are Metal times (nbody 1.17, terrain 1.19), which swing that much between runs on main too; on this M4, interleaved best of 3, Metal is 0.997 of main (nbody 1.018, terrain 0.991).
  • JS lane (no gate covers it): the 16 benches compiled to JS, 2-3 interleaved runs each, geomean 0.983, every bench within ±2%; within_busy 0.75.
  • Demos: all 15 build in C and JS.
  • Audit of every commit since 2.0.0 (453 commits, a7e0600 to fb6f32a): for each, what it did, whether it aimed at compile or runtime performance or an edge case, its linked issues and PRs, and whether comp.ts back under its cap by removing mechanisms at their root: a global File, one way to read a shared cell, and effects that park themselves #1281 touches the same mechanism; where it does, the commit's or issue's reproducer was run on both trees. 123 preserved (verified), 329 no interaction, 1 regression, fixed: removing FID_EXIT's continuation fast path (87a1e9c) cost 28% more instructions at 8 threads in code that applies a closure which may fork (9.03G -> 11.54G; no bench takes that path), so the fast path stays. The 34 commits that aimed at performance are all preserved or untouched.
  • Tried and dropped, with measurements: removing FID_EXIT's fast path (above), element-wise block copy (10% slower on a block-heavy micro-benchmark), a shared device/host drain loop (Metal compiled the work loop twice), JS Nat matches through == (a host's raw BigInt), dropping fl.consts (b84b004's 8 s compile guard), and two emitter shortcuts that relied on term identity.
  • Not run: CUDA (the device read path and the CUDA gpu_make change are untested) and Linux.

🤖 Generated with Claude Code

@nicolas-abril nicolas-abril changed the title comp.ts back under its cap by removing mechanisms at their root: a global File, one way to enter a task, one way to read a shared cell, and effects that park themselves comp.ts back under its cap by removing mechanisms at their root: a global File, one way to read a shared cell, and effects that park themselves Oct 2, 2026
@nicolas-abril
nicolas-abril force-pushed the comp-simplify branch 5 times, most recently from 391d842 to 47d055a Compare October 3, 2026 20:27
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Review of 47d055a

Summary: the gates pass, but there is one silent miscompile in C, a compile-time and memory blow-up, and some changes to IO behaviour the PR doesn't mention. I'd hold the merge until the first five items below are fixed.

Gates (cluster, PR merged onto main)

Tree repo ping test perf safe
PR merged onto 2bfc83f 50/50 46/46 1543/1543 120–124/124 same as main except the 2 renamed tests
PR merged onto 698b2e8 (#1304 ported by hand) 50/50 46/46 1543/1543 120–124/124 same as main
  • Perf: in three alternating rounds against main, every column's geomean is between 0.99 and 1.01. The exception is bfs compile time, which is 1.12x every time (see item 2). The cells over 1.15x are the sub-0.1 s GPU cells, which miss on main too.
  • Token cap: comp.ts is 62,796 tokens after the merge.

What holds up

  • Byte-identical output: checked for C, JS and .mjs on all 1,626 .bend files, on main and at five of the PR's commits. Every commit that claims identical output has it, and lit_call changes exactly the 32 C files it lists.
  • Two books in one process: all 737 compilable books, compiled in one process in both orders, match a fresh per-process compile. Main still fails the Initialize compiler caches before building printer layouts #961 repro, so that fix is real.
  • JS lane: every test, bench and demo output under node, and every bench and demo under bun, matches main.
  • term_peek / ctr_take: native stress runs at 14 threads found no mismatches. That was the sharing tests 20–25 times each, 255 other tests 8 times, and every bench 6 times on CPU and 3 times on Metal. ThreadSanitizer reported nothing on either tree.
  • Not tested: CUDA and Linux, as the PR body also says.

Must fix

1. C miscompile from removing the local flag (5fb5852, facts_hot).

  • What goes wrong: under a constructor with its own erased binder, facts_hot matches a type Var against the def's erased parameter by name and index. It records the narrow fact def~i where main recorded "*". In the repro below, the existential Var("A",1) is taken for f's parameter 1, so AuditNode stays cold. audit_head then reads a shared (rfc-sealed) cell through term_loc, and a 1-word cell is freed as a 2-word node, which corrupts the heap.
  • Result: main prints 9 in every lane. The PR's C binary prints 67108867 at 1 and at 8 threads, while the interpreter and JS print 9.
  • What fixes it: renaming f's parameter to X gives 9, and so does restoring only the local flag while keeping DUMMY.
c9.bend
import Base

type AuditTree is Data:
  AuditEnd{}
  AuditNode{n: U32, tail: AuditTree}

type Hidden<P: Data> is Data:
  Hide{-B: Data, value: P}
  More{h: Hidden<P>}

type Ex is Data:
  Ex{tag: U32, -A: Data, v: A, eq: {A == AuditTree : Type}}

def audit_head(t: AuditTree) -> U32:
  match t:
    case AuditEnd{}:
      0
    case AuditNode{n, tail}:
      n

def count(k: Nat, acc: U32) -> U32:
  match k:
    case 0n:
      acc
    case 1n+p:
      count(p, (acc + 1 : U32))

def build(k: Nat, +n: U32) -> AuditTree:
  match k:
    case 0n:
      AuditEnd{}
    case 1n+p:
      AuditNode{n, build(p, (n + 1 : U32))}

def cast(-A: Data, v: A, eq: {A == AuditTree : Type}) -> AuditTree:
  %eq : _
  v

def unhide(-P: Data, x: Hidden<P>) -> P:
  match x:
    case Hide{B, value}:
      value
    case More{h}:
      unhide(P, h)

def twice(-P: Data, +x: Hidden<P>) -> P & P:
  a = unhide(P, x)
  b = unhide(P, x)
  (a, b)

def bsum(p: Bool & Bool) -> U32:
  match p:
    case Tuple{a, b}:
      2

def f(e: Ex, -A: Data) -> U32:
  match e:
    case Ex{tag, A, v, eq}:
      audit_head(cast(A, unhide(A, More{Hide{Bool, v}}), eq))

def main() -> IO(Unit):
  t = build(3n, count(7n, 0))
  n = f(Ex{1, AuditTree, t, {==}}, Bool)
  m = bsum(twice(Bool, More{Hide{U32, True{}}}))
  IO.print(U32.show((n + m : U32)))

2. Compile-time and memory blow-up from removing lit_call (b009765).

  • What goes wrong: every Nat literal past 256 is now a constant (term_const, term_force). emit_unfold unrolls recursion bounded by such a literal about 1,365 levels per call site, per spin and per fixpoint pass, then throws the work away when FOLD_FUEL runs out. The cost is linear in the number of call sites, so the compiler can run out of memory.
  • Numbers: the repro below compiles in 0.30 s and 324 MB on main, and in 7.3 s and 4.4 GB on the PR. 40 call sites of count(1000n+i) take 12 ms on main and 532 ms on the PR. bfs is the 1.12x compile time in the perf gate (emit goes from 21 to 40 ms, instructions from 2.45G to 3.48G). JS is unaffected.
  • Second-order effect: a failed unroll leaves FUEL negative for the rest of the def, so folds that main performs are refused.
    • step(x) = inner(300n, x) + len(mk(60n, Nil{}), 0) stops folding mk(60n, Nil{}) into a static image, and runs slower (0.65 s vs 0.74 s for 3M calls).
    • tests/run/array_map_loop loses its Array.map fold, although the commit message lists it as a new fold.
    • loop(300n, x) now unrolls into 300 straight-line statements in its caller.
t6_k10_d300_small.bend (bend -o x.c)
import Base

def f(n: Nat) -> Nat:
  match n:
    case 0n:
      0n
    case 300n:
      1n
    case 1n+p:
      f(p)

def g0(x: Nat) -> Nat:
  Nat.add(f(100000n), x)

# ... g1..g9 the same with 100001n..100009n

def main() -> List<Nat>:
  [g0(0n), g1(1n), g2(2n), g3(3n), g4(4n), g5(5n), g6(6n), g7(7n), g8(8n), g9(9n)]

3. TCP.accept and UDP.recv_from can starve timers and other fibers (dde6439).

  • What goes wrong: these effects no longer park until their socket is readable before they run. A fiber that keeps finding queued datagrams or connections never leaves io_step's loop, so timers, IO.within deadlines and other fibers starve.
  • Repro: queue 300 datagrams, spawn a fiber that sleeps 5 ms, then loop on recv_from. Main prints timer fired 5 ms before receiver done. The PR prints receiver done first, and the timer fires after 101 ms in C and 603 ms in JS. An accept loop with a backlog of connections behaves the same way.

4. A wildcard arm in an IO.OP match now takes requests silently (e52ce32, mat_ctrs).

  • What goes wrong: keeping IO.OP's default arm in JS, in place of $FFI, means a request that reaches a user's case _ arm takes it in every lane. On main, the run and JS lanes refuse it with bend: runtime fail-stop and exit 1.
  • Result: on the PR, the repro below prints wildcard arm taken then 42, and exits 0. With case other: other, the effect actually runs.
  • Coverage gap: request_out_of_band says a request is "a refusal in EVERY lane", but no test has a wildcard arm.
t1_wild.bend
import Base

law intercept.at:
  for op: IO.OP<U32>
  IO(U32)

def intercept.at(op):
  match op:
    case Emit{v}:
      do IO<U32>:
        u : Unit <- IO.print("emitted")
        return v
    case _:
      do IO<U32>:
        u : Unit <- IO.print("wildcard arm taken")
        return 42

law intercept:
  IO(U32)

def intercept():
  intercept.at(IO.print("SECRET")(U32, x => Emit{7}))

law main:
  IO(Unit)

def main():
  do IO<Unit>:
    u : Unit <- IO.print("before")
    r : U32 <- intercept()
    IO.print(U32.show(r))

5. The JS effect API change is silent.

  • A third io_eff argument is ignored: JS io_eff(k, run) drops it without an error, while C refuses to compile. An effect written to the 3-argument API from CHANGELOG 2.0.28 stops waiting. For example, io_eff(CID(My.wait), my_wait, () => ({time: true})) returns at once instead of waiting 300 ms. A {read: true} effect reads before its fd is ready, which can block the whole JS loop. Main's own effect_need_collision test prints 99 22 on the PR instead of 11 22.
  • A missing registration fails late: a foreign def whose .js never calls io_eff is no longer refused at load. It fails only when its request runs, after any earlier side effects, and the message bend: an alien request doesn't name the def. On main the error is no effect registers tick, raised before any output.

Should fix

  1. Binary size (io_eff_rows): the table is static Effect io_eff_rows[1 << 16] and is only written by constructor-time io_eff calls. clang -O3 evaluates those calls and stores the table as 512 KB of initialized __DATA_CONST data, where main's table stayed in zero-filled BSS. Every native binary grows by about 528 KB: io_hello_world goes from 70 KB to 582 KB. Dropping static brings binaries back to about 71 KB.
  2. Test coverage: big-literal tests no longer exercise their C path. literal_rows_cubic now emits 13 else if rows instead of 512, so the clang bracket-nesting and cubic-flatten bugs it pins would no longer be caught.
  3. guide/EFFECTS.md: it shows a time-only wait only for C. In JS, fd 0 means stdin, so copying the C form as io_park_on(0, false, k, ..., deadline) returns after 1 ms when stdin is at EOF, as in CI. The JS paragraph should say to pass undefined.
  4. Stale docs: guide/GUIDE.md:635 and tests/compile/literal_reads.bend:3 still say a Nat literal past 256n is U32.to_nat(n) underneath. bend.ts:1078-1079 says the same; that one is Taelin's to change.

Nits

  • The new term_peek comment says "a shared node's fields never change". term_peek also serves shared blocks through blk_loc, and their cells do change while shared (Array.set on a forked array, the array atomics, blk_keep's in-place write). ctr_take's copy-before-acquire depends on that invariant, so the comment should limit it to constructor nodes.
  • emit_open's live[i]?.[1] ?? "k" silently names any extra parameter of any def k, where main threw. Nothing reaches it today, but a future mismatch between lays and live would emit _k_N instead of failing.
  • type File and type Scope repeat every field that file_new and scope_new already set. Deriving the types (ReturnType<typeof file_new>) would save about 180–205 tokens. Merging ctr_take's two copy loops saves about 38, and calling scope_new() in compile_book about 25.

Rebase

This branch conflicts with main a3b0dfe: 5 hunks in comp.ts, from #1304, #1298, #1303 and #1279. #1279 isolates spare nodes across sum arms, and spare nodes are what this PR moves into Scope, so that hunk needs care. The effect changes merge cleanly with #1280. The measurements in the PR body predate all of this, so they need redoing after the rebase.

@nicolas-abril
nicolas-abril force-pushed the comp-simplify branch 2 times, most recently from 9c408f5 to 4699313 Compare October 5, 2026 10:59
@nicolas-abril
nicolas-abril marked this pull request as ready for review October 5, 2026 11:40

@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.

Re-review: 4699313 plus a merge with main and two small fixes (now 2a811ba)

Thanks for the fixes. The five must-fix items and the binary size from the first review are all resolved, and the gates are green.

Merge with #1324 (472bfc5)

#1324 conflicted in 7 hunks of comp.ts. It resets the module-global layout caches before show_main. This PR removes those globals and builds a fresh FL first, so I kept this branch's side throughout. #1324's own case (T = C{a, b}, then C{a}, then C{a, b} again, in one process) emits B's C byte-identical before and after A on this branch. Main before #1324 throws on it. That case isn't in the suite, so it would be worth adding as a test.

Two small fixes (2a811ba)

  1. JS io_eff(k, run) with a third argument now throws. An effect still written for the old need API fails at load. Before, it loaded and stopped waiting; C already fails to compile it.
  2. A request that no effect registers now names its def: "bend: no effect registers ", the old load-time check's words, instead of an unnamed "an alien request". tests/io/effect_unregistered pins it. It still fails when the request runs, as this PR intends.

Gates (cluster)

  • 2a811ba: repo 50/50, ping 46/46, test 1549/1549, perf 124/124.
  • 472bfc5: test 1548/1548, perf 121/124, and safe matches main file for file, with the 2 new tests agreeing.

Perf also ran in three alternating full-table rounds against main 1159f0b. No measured cell went over 1.15x, and best-of-3 geomeans match main in every column. bfs compile time is back to main's level.

Fixes confirmed with the first review's repros

  • c9.bend prints 9 at 1 and 8 threads.
  • The big-literal program compiles in 0.32 s with 273 MB, against 7.3 s and 4.4 GB before.
  • The 5 ms timer fires before a UDP backlog drains.
  • A request reaching a user's case _ fail-stops.
  • io_hello_world is 70 KB again.

Notes, not blocking

  • mat_ops hard-codes IO.OP's constructors as ["Emit", "Halt"], so a third constructor would need updating there too (and in io_step and io_run).
  • On the device, term_peek is now a plain 64-bit load where it was volatile 32-bit loads. CUDA hasn't been run, so it should get a run before release.

nicolas-abril and others added 15 commits October 5, 2026 14:50
…ds the book and no cache is cleared by hand

The caches were module globals cleared per book, so the compiler already
held one book at a time; 40 functions still took `book` or `fl` only to
reach it. Now FL is the book being compiled and all that is memoized
about it: compile_book and js_lib make a new File first, so show_main
reads this book's caches rather than the last one's, and FL.memo holds
what one def's walk memoizes (memo_gc clears it as a whole). type File
lists every field, FL.memo's too. The root segment mints no C id: no
name is empty, so its FID_ never collided with anything. Emitted C, JS
and .mjs are byte-identical for all 1626 .bend files under tests/,
bench/ and demos/. comp.ts: 63980 -> 63502 ttok.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Intr.call meant three things: an array op stays intrinsic in C with no C
text, emit_intr and js_expr send it to arr_op, and emit_fold does not
rebuild a call with folded arguments. The last was redundant (emit_intr
folds each argument as it emits it), so the flag only said 'arr_op lays
this out', which C: null now says. Emitted C, JS and .mjs are
byte-identical for all 1626 files.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…t_open and emit_put

compile_book named a foreign def's parameters, opened its segment and
wrote its return by hand, which is what emit_open and emit_put do; the
one difference is the continuation, a parameter past the live ones, which
emit_open now names k. Emitted C, JS and .mjs are byte-identical for all
1626 files.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…l flag

facts_hot matches an erased Var against the current def's telescope by
name and index; a constructor's own erased binder (AuditHide{-A, value:
A}, 6200daa) unbound as Var(A, i) could pass for the def's parameter, so
a local flag ran through facts_hot and facts_ctr to switch the
match off under such a constructor. ctr_tail now opens each binder as
DUMMY, named ~, which no parameter can match: the collision cannot arise,
for erased and live dependent fields alike, and the flag goes. Emitted C,
JS and .mjs are byte-identical for all 1626 files (seal_var_scope and
seal_clone_heat among them). A constructor's own telescope, which the
teles memo served through tele_unbind, is memoized in FL.ctrs, so a
1000-arm Nat literal match emits in the same time as on main.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…_nat shim goes

942573f made a literal one Lit node and kept the old parser's desugar of
a Nat past NAT_LITERAL_MAX, U32.to_nat of its word, so the emitted C and
JS stayed the same. Nothing needs it: ctr_adt reads a Nat literal in one
step, as it reads U32 and F32 ones, and FOLD_FUEL caps any unrolling. A
big literal now counts as constant: 32 C files change, most only from
((u64)300ull) to 300ull; a constant list of big Nats becomes a static
image (walkers_test), and calls on big literals fold
(literal_rows_cubic, array_map_loop). nat_pattern_deep, which pinned the
default arm's one checked sum and reached it through the shim, takes its
300 from U32.to_nat, so down still runs its match: its C again holds
nat_chk(e, _305_0 + 300). The 22 changed tests pass interpreted, in JS
and in C; the 5 changed benches print their pins in C and JS. JS and
.mjs are byte-identical everywhere.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ses a leaked one

A JS undefined leaked into the generated C is an undeclared name in a
segment, a spin or a table, all of which clang compiles on the host, so
the build fails there; the scan only reworded that failure, and had a
false positive of its own (effect_undefined_word, whose comment now says
so). Emitted C, JS and .mjs are byte-identical for all 1626 files.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… impossible states go

- corpus_eval: work_loop returning 0, a seam delivery returning 0 and a
  pool turn all end when the root is done, so the three exits and their
  two 'delivery lost' messages are one loop and one check after it.
- gpu_make (CUDA) took a NULL path no caller passes; ring_rewind skipped
  a ring holding more than RING_LEN, which a host push's err_fail and the
  device's error check after its pass both rule out; run_tail wrapped its
  one argument in an array that run_loop spread back.

Full local suite (gates/test.ts's shards on this Mac, every lane, Metal
for the bang tests): every test passes but the two display-dependent gfx
tests, as on main.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
- term_peek reads a redirect cell with one relaxed 64-bit load (plain on
  the device), as bendlang#1155 made blk_loc do: the target bits never change, so
  a torn read still finds them, and a shared node's fields never change
  either (span_fade only bumps its children). rfc_view's two atomic
  halves and its acquire go; ctr_take reads the count itself after
  copying and acquires only when it is the last holder, before it frees
  the cell and owns the node. blk_loc is term_peek for a shared block.

FID_EXIT's continuation fast path stays: without it, code that applies a
closure which may fork retires 28% more instructions at 8 threads.

Full local suite on every lane: all pass but the two display-dependent
gfx tests, as on main.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… IO.OP match keeps its default arm in both lanes, so $FFI goes

C refuses a request reaching a Bend match over IO.OP through the match's
kept default arm (ERR_TAGS); JS threw the request object and io_run's
catch turned it into the fail-stop. mat_ctrs now keeps the default arm
for IO.OP itself, so JS takes the same arm, and io_run's catch only maps
a stack overflow. A foreign op is { $: name, args, kont }, built like
C's constructor; the loop looks its effect up by tag and refuses an
alien request at run time, as C does, so js_lib's load-time registry
check goes with $FFI. request_out_of_band's header says so; its lines are
unchanged. Full local suite on every lane: all pass but the two
display-dependent gfx tests, as on main.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… has one way to run a request

The loop could park a request before its effect ran, from a need given at
registration (IO_READ, IO_TIME in C; a need function in JS), and only
IO.sleep, TCP.accept and UDP.recv_from had one. Each effect already
parks itself the other way (io_wait_on in C, io_park_on in JS): accept
and recv_from on EAGAIN, so they just try first; sleep now parks on its
deadline. So io_eff takes (cid, run), IoEff and io_exec go, and io_step
takes a request's fields once for Halt and for an effect. Nothing an
effect could do is lost: guide/EFFECTS.md now says an effect that waits
parks itself, and how to wait on time alone.

Breaking for effects outside Base: a C io_eff with a third argument no
longer compiles, and JS ignores one. All 63 C registrations in
bend2/effs and tests drop their third argument. effect_need_collision
keeps its point (two effect files define the same function name) with
the helper as a plain function. All 136 io tests pass on every lane.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…he helper tests stop saying need

- file_book takes the root file and returns nothing; compile_book and
  js_lib bind it once (const fl = FL = file_new(...)) and say fl, where
  they mixed fl and FL for the same object.
- js_lib's comment registered effects with io_eff(CID(k), run, need?),
  which takes no need now: it says io_eff(CID(k), run).
- effect_need_collision(_reverse) and their two .js files become
  effect_helper_*: with needs gone they pin that two effect sources may
  define the same function name, and the name says so.

Emitted C, JS and .mjs are byte-identical for every file but the renamed
tests, which pass on every lane.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… compiled, a Scope is where the emitter writes

File held both what lives for a book (its facts, its output, its caches)
and what lives for a scope (the segment written, the locals' names, the
bindings and spare nodes held, the def), so every { ...fl } copy for a
match arm, closure or fork carried the book's dozen caches, and fl.hot
and FL.hot named the same set. Now file_new builds the File (FL) and
scope_new a Scope, which a nested scope copies; functions read the book
through FL and take a scope (sc) only to write. Six that took the file
only to reach the book (arr_q, brw_of, c_ids, effect_srcs, js_host,
js_marshal) take none. Emitted C, JS and .mjs are byte-identical
for all 1626 files.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…nrolls recursion bounded by one

Reading a big literal as a constant let the folder try to unroll any
recursion bounded by it: a 43-line program took 7.3 s and 4.4 GB to
compile against 0.3 s and 324 MB on main, and a failed unroll blocked
the later folds in its def (array_map_loop lost one). lit_call, term_const,
ctr_adt and nat_pattern_deep are main's again.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…nder is not the def's parameter

Opening a constructor's binders as DUMMY did not do what the flag did: on
a type whose constructor binds its own erased type, the C binary printed
67108867 where every other lane printed 9, and corrupted the heap.
facts_hot, facts_ctr and ctr_tail are main's again, over Scope and FL.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…g cannot starve the timers

With the need gone, accept and recv_from tried their socket first, and
while datagrams or connections were queued every request answered at once:
the loop never polled, so a 5 ms timer fired after 101 ms in C and 603 ms
in JS. Each now parks until its socket is readable, as the IO_READ need
made it, and re-parks on EAGAIN as before. udp_backlog_timer pins it: its
0 ms tick prints before 40 queued datagrams drain (after them before the
fix, in C and in JS). guide/EFFECTS.md says when an effect parks first,
and that io_wait_on keeps its fd in w->word.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril and others added 6 commits October 5, 2026 14:50
…reaching it fail-stops in every lane

A user's case _ in an IO.OP match took a request in every lane once $FFI
went (main refused it in JS and bend run, and took it in C). mat_ctrs
now splits such a default arm: the user's arm runs when the scrutinee is
an Emit or a Halt, and anything else, a request, fail-stops as the kept
default arm already did in a match without one. request_wildcard pins it:
a purely built Halt still reaches case _, and the request is refused in
C, JS and bend run.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…every binary

clang placed the static io_eff_rows in __DATA_CONST, so every native
binary carried its 65536 null pointers: io_hello_world was 598 KB on
this Mac (main's IoEff table, twice as wide, made it 1.1 MB). A
non-static table is zero fill: 70 KB.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…mments say what holds

- compile_book opens each def in scope_new(), which is what it built by
  hand; mat_ctrs names its IO.OP test once.
- FL.memo's consts and lits are ground and steps, apart from FL.consts
  and FL.lits, which hold other things and clear at other times;
  js_marshal keeps its names in FL.marsh, not in the C lane's FL.spun.
- File's comment says brwl is the scope's; term_peek's says the host's
  load cannot tear (only the device's can), and why ctr_take may copy a
  shared node's fields before it acquires.

JS and .mjs are byte-identical for all 1628 files, and C differs only in
term_peek's comment, which the C runtime carries.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…es its def

With the need gone, a JS io_eff(k, run, need) loaded and ran its effect
at once, so an effect written for the old API stopped waiting with no
error, where C refuses to compile one. io_eff now throws when it is given
a third argument. And with js_lib's load-time check gone, a foreign def
whose source registers no effect failed with "an alien request", naming
nothing; the loop now says "no effect registers <def>", the old check's
words. effect_unregistered pins it in the interpreter and the JS lane.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…arve the timers

udp_backlog_timer pins it for UDP.recv_from; TCP.accept had the same fix
and no test. Twenty connections wait in the listener's queue while a 0 ms
timer is due: the tick prints before the last accept, in C, JS and bend
run. With the accept trying its listener before it parks, it printed after
them, in C and JS.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril merged commit bf96827 into bendlang:main Oct 5, 2026
1 check passed
@nicolas-abril
nicolas-abril deleted the comp-simplify branch October 5, 2026 13:01
jnadeau207-collab pushed a commit to jnadeau207-collab/bend that referenced this pull request Oct 5, 2026
…end is rebuilt

FORK.md states the rules this fork follows: main is upstream plus U64,
F64 and the F64 min/max fix, replayed when upstream moves, with each old
main kept as a tag archive/main-*; BendCAD pins are tags numeric/<date>
on the pin branch numeric-on-upstream, whose conformance/ receipts name
the tree each pin tested; numeric is the earlier pin branch. AGENTS.md
points to it and warns that `bend update` drops U64 and F64.

This main replays U64, F64 and the min/max fix onto upstream bf96827.
Upstream's bendlang#1281 replaced the threaded File with a global FL and a
per-scope Scope and dropped the intrinsics' call flag; the port keeps
every behavior: erased-argument layouts in fun_of, two-word arguments
and results in intr_c, soft natives in place behind
#ifndef __METAL_VERSION__, and the U64 and F64 show and read natives.

Checked on WSL2 (Ryzen 7 7800X3D, RTX 4070 SUPER): repo gate 50/50;
test gate 1561/1564, where import_Word and io_process_run fail the same
way on upstream bf96827 (1548/1551) and the two failures that differ
between the runs pass 3/3 in isolation on both trees; conformance/run.sh
at D=20 on this bend2 tree (1a3b0a4), harness from numeric-on-upstream
with the defs lanes' edit matching the new file handle: 0 mismatches on
js, c, cuda, cuda-defs, c-defs and the interpreter, and no line a Metal
compile keeps uses a double.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jnadeau207-collab pushed a commit to jnadeau207-collab/bend that referenced this pull request Oct 5, 2026
… either file handle

cuda.sh's FLIP edit looked for file_push(fl, "#ifndef __METAL_VERSION__").
Upstream bf96827 (bendlang#1281) names the handle sc, so on fork main the edit
did not apply and both defs lanes failed to build. It now matches any
handle name; on this tree it changes the same line as before.

Co-Authored-By: Claude Opus 5.5 <[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