Skip to content

perf(comp): parameters a cycle passes along unchanged go as one node - #36

Draft
nicolas-abril wants to merge 1 commit into
mainfrom
opt/wide-pin
Draft

nicolas-abril wants to merge 1 commit into
mainfrom
opt/wide-pin

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

On main (6180be5b)

This PR is a port onto main, on main: 1 commit of its own, with no integration history (head 068a264f).

Port notes

Checks

  • Cluster gate (every test, every lane): 1872 / 1873 pass. The one failure is io_tcp_text_residue on the interpreter lane, a TCP test that is flaky on the cluster; it passes locally, on this branch and on main.
  • Locally: no new tsc errors against main, no new comp.ts lines over 80, its tests pass on the C and JS lanes (plus 40 lane runs of existing tests; run/array_slab fails on main too).
  • comp.ts: +5181 tokens (ttok -m gpt-3.5-turbo) over main, which is at 65,989 of the 66,000 cap.

Performance was measured on integration when this change merged there (see the original description or the replaced PRs); it has not been re-measured on main.

Original description (written for integration)

Stacked on opt/integration (09d002f). One commit; comp.ts +4,909 ttok (99,999 to 104,908). Measured on one Mac (M4) under the shared lock; perf gate on the cluster.

What

Every flat value is one word per field, so a recursion that passes 20 words along unchanged copies them into every frame, register set and task. This passes such parameters as one word:

  • Which parameters. Parameters of words alone (scalars, enums, records without boxes) that every call inside a cycle of defs passes a parameter of the caller, unchanged, form a group (grp_slots). The cycle needs a call inside it that is not a tail call (a tail loop already keeps the words in registers). The group needs 8 words or more if the cycle forks inside itself, 20 if it doesn't: there, each read's extra load sits on the chain of calls, which is why a 16-word serial walk is left as words. A slot is the set of parameters that the calls join together, so two defs may pass the values in different positions.
  • Copies. Each def in the cycle that holds the group gets a copy, d~grp. In the copy, the first grouped parameter takes a heap node of all the words and the others take no word; reads load off the node. The node is a raw word, so refcounts and borrowing never see it. Calls inside the cycle go to the copies (grp_route), so frames, forks and tasks hold the group as one word.
  • Entry. The defs as written still take the words, so a call from outside costs nothing extra. A def's first call into the cycle builds the node. Variables it uses again read off that node from then on, and the binding that owns the node frees it once nothing reads it. A tail call that would leave the node becomes a cut.
  • Details. Closures copy the words out, since they can outlive the node. Words passed into a native loop go through GRP_KEEP (an empty asm on the host), because clang otherwise compiles the loop differently around loaded values (+1.5% instructions in quickhull). A spin called from one site in the source is now called from a def and its copy, so the call is marked HOT to keep clang's single-site inlining (kmeans lost 1.9% without it). A def and its copy share their lends (raytracer's BVH stays borrowed).

Effect

Microbenchmarks (16 separate U32s unless noted; a fork tree of 2^25 leaves; integration → this PR):

1 thread 10 threads GPU
fork tree, 12 words 203.5 → 145.1 ms 46.8 → 31.7 ms 81.6 → 76.0 ms
fork tree, 24 words 475.2 → 148.3 ms 88.6 → 31.8 ms 141.4 → 77.2 ms
fork tree, 16-word record 465.4 → 338.3 ms 110.2 → 65.2 ms 137.4 → 95.4 ms
serial walk (2^24 leaves), 20 / 32 words 0.91x / 0.39x cycles

Cost: a loop that hands 12 fresh words to a forked recursion 2M times runs 1.00x the cycles at depth 0, 1.14x at depth 1, 0.94x at depth 2 and 0.71x at depth 3.

Benchmarks. Of the 244 programs in bend-benchmarks, 5 change C. All print expected.txt (the parallel ports also at 10 threads). Cycles, median of 5:

program goes as one word 1 thread 10 threads GPU
bend-runtime/mandelbrot rcol's Nat and 8-word LUT 0.983x 0.98x
bend-runtime/kmeans pfold's 8 centroids 1.001x 0.98x
parallel-ml/quickhull flags' flag and two lines 0.999x 1.008x
parallel-ml/pure-quickhull the same 0.998x 1.006x
parallel-ml/raytracer render's 12-word camera 1.011x (equal instructions) 1.009x

Perf gate: this PR passed 111 and 113 of 124, against 113 on opt/integration, within the gate's run-to-run spread.

Checks

  • Native test lane, 1,123 tests: statuses equal opt/integration's, and no existing test's C changes.
  • tests/run/param_group.bend: serial and forked groups; two defs passing the words in another order; records, a record of records with a tag, a constant record, and a record stored in a list; a matched flag; closures that run inside the cycle and after it returns; a word or a record that one call changes (it stays words); entries at depths 0 and 1, from a tail call, from a fork and inside a walk; a loop handed grouped words; bang calls. It passes at 1 and 8 threads and with the GPU on and off, with 0 live words under -DBEND_POISON.
  • Deliberately wrong versions that the test catches: never freeing the node, freeing it early or while a call passes it, not reading before a binding goes, offsets by position rather than slot, a closure keeping the node, a join dropping the node's word, restoring an owner before its readers, and a lazy fork that builds a node. One is not caught: a later fork step's frame dropping its own node's owner. That path runs only when a drain runs tasks in sequence, which the test does not reach on this machine.

An earlier version also passed 12-word records through a borrowed box. It went, because groups do the same job without touching refcounts or borrowing. Its history is on opt/wide-pin-history.

Updates

  • 2026-12-01: merged opt/integration (d887682, then 3cd6ad0) into this branch: five conflicts, both behaviours kept (file_new's group fields beside integration's WeakMap caches; the HOT test takes integration's memoized big() and nest_of() terms and this PR's one-site term, then GRP_KEEP; the lazy fork keeps grp_fresh and the noted-frame comment, WL_FKDROP gone as on integration; perf(c): a spin that meets a shared cell borrows the rest instead of taking it apart #7's kept-copy exit in emit_body now reads c0 and leaves through grp_route). Merged into opt/integration. Against integration: 5 programs change C, all level in cycles (mandelbrot -1.8%; kmeans +21.6% instructions at equal cycles); 244 programs print expected; the 1 test with changed C passes the poison/live-word check; GPU level; gate 2020/2020.

🤖 Generated with Claude Code

@nicolas-abril nicolas-abril changed the title perf(comp): a wide record a cycle of calls passes along unchanged goes as one borrowed box perf(comp): wide records and groups of parameters that a cycle passes along unchanged go as one word Nov 25, 2026
@nicolas-abril nicolas-abril changed the title perf(comp): wide records and groups of parameters that a cycle passes along unchanged go as one word perf(comp): parameters a cycle passes along unchanged go as one node Nov 25, 2026
@nicolas-abril
nicolas-abril changed the base branch from opt/integration to main November 26, 2026 16:47
nicolas-abril added a commit that referenced this pull request Dec 1, 2026
The one conflict: #36's wide-pin and this branch both mark HOT a spin
whose single source site became several through copies (grp_defs' copies
there, the closure pass's here); both rules and their notes kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Parameters of words alone (scalars, enums, records without boxes) that
every call inside a cycle of defs passes a parameter of the caller as it
is are a group, when the cycle has a call inside it that is not a tail
and they hold 8 words or more where the cycle forks inside (20 where it
does not: there each read's load sits on the chain of calls). Each def of
the cycle that holds the group gets a copy, d~grp, whose first grouped
parameter takes a node of all their words and the others no word; reads
load off the node (GRP_KEEP holds words handed to a loop in place). The
defs as written take the words: a call from outside costs nothing, and
the def's first call into the cycle builds the node, which its owner
frees once nothing reads it. Calls inside the cycle go to the copies, so
frames, forks and tasks hold the group as one word.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 380dbad)
nicolas-abril added a commit that referenced this pull request Dec 1, 2026
…33, #34, #36, #39, #44, #61-#68): record their heads

Each is a port onto main of integration's own branch, its content already
here (integration keeps the original commits; the ports add type fixes,
line wraps, moved pieces and #68's book_passes, applied in the previous
commit). Groups: #61 (#9 #11 #15 #32 #41 #42), #62 (#26 #56), #63 (#48 #50
and compile-time), #64 (#19 #20 #45), #67 (#55 #58), #68 (#54); par-defer
(#43) is now part of #34, lazy-startup's parts are in #16 and #34. This
merge records their heads (-s ours: no content change).

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.

1 participant