Repository navigation
perf(rt): a hungry drain splits the oldest fork a sequential task has open - #34
Draft
nicolas-abril wants to merge 16 commits into
Draft
nicolas-abril wants to merge 16 commits into
nicolas-abril wants to merge 16 commits into
Conversation
Owner
Author
|
Pushed
|
… open
A drain runs each task in sequence, so a task that holds most of a drain's
work (one corner of an unbalanced fork tree) runs on one thread while the
others wait for the next turn. Now a two-call fork run in sequence notes its
first frame on the thread's fork list (a seq bit, so a thread without one
tests a register). While threads that ran out of claims hunt and fewer tasks
wait than hunt, a note turns the thread's oldest unsplit frame into a task:
the fork's s segment reads the second call's arguments off that frame, as
the first continuation would, into a task whose answer goes to a future in
a slot of the thread's box, and puts the fork's w segment in the frame's id
word. Returning there, w takes the task back and runs it, or waits for the
thread that took it, running others' tasks meanwhile. The oldest frame holds
the largest part left. A drain still ends when its claims do: a claim waits
for every task split off it. An idle hunter sleeps after a while, woken by
a boxed task or the drain's end.
The micro benchmark with an unbalanced fork tree (item i of 1024 holds a
share 1/(i + 1)) goes from 1053 to 385 ms at 10 threads (2.86x to 7.81x of
its 3010 ms on one thread).
Tests: tests/run/lazy_split_{deep,unbal,results,shared}.bend.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 6938a7d)
…rame with 4 notes above it At 16 threads on 10 cores (the perf gate's PAR-CPU), tree-bitonic took 1.63 s against 1.16 before lazy forks: past-claim threads spun and yielded (980k sched_yield, 3.6 s sys) on cores the remaining claims needed, and split 260k frames near the top of their stacks, 177k of them stolen and waited on. A thread past its claims now hunts only while busy threads and hunters are fewer than the cores (fk_busy), and fk_poll splits the oldest frame only with FK_DEEP = 4 notes above it. tree-bitonic, wall s median of 5 (before lazy forks / this PR / now): 16 thr 1.157 / 1.632 / 1.250; 10 thr 1.187 / 1.257 / 1.239. The unbalanced micro keeps its gain: 16 thr 1.012 / 0.855 / 0.850, 10 thr 1.028 / 0.851 / 0.859. nbody RSS at 16 thr 2.36 / 3.18 / 2.97 MB. The lazy_split tests and fork_exit_turns print right at 1, 2, 4, 10 and 16 threads (3 runs each); the 51 forking tests print right with 0 live words (-DBEND_POISON) at 1, 8 and 16 threads. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 6274bc8)
…lane does not normalize them A main of type U32 is printed from the checker's normal form: each of the four took over 100 s there (past the gate's 30 s, some past its 4 GB). main now prints run() (the old main) through IO.print: the interpreter answers in under a second, and C (1, 2, 4, 10 and 16 threads, 3 runs) and JS print the same as before. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 60f7605)
…a noted frame returns through a d segment A note at every two-call fork (and its drop in the first continuation) cost osprey fib's bend-par port (a fork every ~38 instructions) +48% instructions at 2+ threads and the first continuation's test +15% cycles at 1 thread; at 10 threads it ran 7.8% slower than main. A fork now notes its frame only while threads hunt or when the frame is within FK_LEVELS = 8 levels (by the fork's frame size) of its thread's stack base; stacks are 16 MB-aligned, so the test is a mask of sp. A noted frame's id word is the fork's new d segment, which drops the note and jumps to the first continuation; a first call answered in the fork's segment (an inlined base arm) drops it there (WL_FKARM), so the first continuation tests nothing. A split needs the polling fork 16 words above the frame (FK_DEEP, was 4 notes). The tests are laid out as unlikely and the note itself is out of line (fk_noted). New test lazy_split_arms: forks whose first call is answered in place (own and another def's tree), right at 1, 2, 4 and 10 threads. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 3587c88)
… not 4096 empty rounds fk_help put a hunter to sleep after 4096 empty rounds. Those took 25 us on idle cores and 170 us with 10 threads yielding, so whether a hunter slept turned on how many others yielded, and a sleeper costs the drain's end its wake. On ocaml-nbody-imm at 10 threads, 3587c88's slower E-core leaf (below) put the drain tails right at that length: 730-875 1 ms sleeps a run (mostly none before it). The spin is now timed (read every 64 rounds) and adapts per thread like POOL_WAIT's: a hunt whose drain ended within a spin of its sleep doubles it (25 us up to 1 ms), a longer sleep halves it. nbody-imm now sleeps 30-45 times a run, 140.2 -> 136.5 ms. A fixed 200 us got nbody-imm the same but cost the BFS ports up to 9% more CPU time; kept in a _Thread_local, the spin cost the fork-every- level micro 7% at 2 and 4 threads (it lives in the thread's alc row). The rest of nbody-imm's loss is clang's, not the scheduler's: the leaf loop inlined in vss's fork segment compiles to the same instructions with other registers since the note went out of line, and that runs 9% slower on the M4's E-cores (P-cores alike: 5.5% slower at 1 thread under taskpolicy -b, equal at full speed). Calling F32 loops out of line in fork segments fixed it but cost ocaml-nbody 13% and lu-decomp 6% at 1 thread (and msort 27% for all loops), so it is not done. New test lazy_split_leafloop: an F32 loop inlined in a lazily forking segment, right at 1, 2, 4 and 10 threads. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit a31ac97)
…et folds into the load ocaml-nbody-imm's leaf loop (inlined in vss's fork segment) built each field's u32 index with a mov and a bfi. Since 3587c88 clang put those in odd registers, and the M4's E-cores run fewer register writes per cycle when most go to registers of one parity: 9% slower per leaf on E-cores, P-cores alike. An index is below 2^31 (5-bit class), so index + offset never wraps in a u64 and the loads take the offset as an immediate. The device keeps u32. New test array_field_offset_wrap. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 3108eef)
Its length moved two of the gate's shard scripts on opt/integration into a window (about 8,065 to 8,120 bytes of command) where OpenSSH's mux client on macOS fails to pass the session's fds (sendmsg: Message too long). Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 58397c8)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…it whole The 8-leaf psum ports (Osprey bend-par) grow on 2 rows; each grow thread ran a whole leaf (a long spin) before its deadline while 8 threads waited, then the drain ran the other 6 leaves: at most 6 of 10 threads busy. Where a def that forks lazily calls an outer spin whose step runs a loop, and only matches and arithmetic on the segment's words precede the call, the segment in a grow (seq == 0) returns a task that reruns it in sequence (fk_defer), up to 4 a thread per turn, and only in turns with fewer rows than threads. The drain deals those tasks like any other. The spin and the code in sequence are unchanged, so nothing changes at one thread. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit d3fbe48)
leaf_defer asked, for every spin, whether each line of every segment
contains its name and "(": a string built per spin and line, and a scan of
all the program's lines per spin (twice for inner, once per spin it was
asked for). On app_slash_boss_3d that was 0.4-0.56 s and 500 MB of garbage,
the emit's peak (1.33 GB RSS). Each segment's called names (every name a
call's name ends in, as the substring test reads them) are now collected
once per segment. app_slash_boss_3d's emit: 2.17 s, 1.33 GB -> 1.80 s,
0.78 GB RSS; the emitted C is byte-identical.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit a9e7b8c)
…eter lane does not normalize them A main of type Nat is printed from the checker's normal form, and on 16 leaves of 60,000 gcd loops the checkup lane never finished (the cluster gate requeued those shards without end). As in the lazy_split tests, main now prints run() (the old main) through IO.print: the checkup answers all three in 0.2 s, at the same sizes, and the C is the same apart from main. Without the purity rule, fork_leaf_defer_drop still crashes at 8 threads under the poison build (6/6); the correct build leaves 0 live words at 1 and 8 threads on all three. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 5a66f04)
…names it The leaves were found for every spin before the segments were scanned for the pattern, and each segment's called spins were read with two regexes over every line. A spin is now tested when a candidate names it, and a line with no spin_ or lane_ in it is skipped. Same C on the 244 benchmarks, the tests and the real programs; leaf_defer was 1.4 s of the 244's emit and 0.48 s of bendquest's. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 1782aad)
…a leaf primes' and mutual's bend-par leaves are go(n, i, 0, isPrime(i)) and go(n, i, 0, eo(i % 1000, True)): a spin call precedes the leaf call, and the rerun may not repeat it, so neither port was deferred. The grow now defers before a run of calls and declarations that leads straight to the leaf. The rerun in sequence makes those calls once, so they need not be pure: a loop that frees, reads or writes the segment's values before the leaf is still exact. What precedes the deferral is held to the old rule (matches and arithmetic). A segment with no call in that run keeps its deferral where it was, so only primes and mutual change C among the 244 benchmarks. Tests: fork_leaf_defer_free (the loop frees the segment's list), fork_leaf_defer_shared (a list both halves and the caller share), fork_leaf_defer_write (the loop writes the segment's array in place). Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 81e3937)
…rst call The walk back from the leaf moved the deferral only to the start of a call block, so declarations above the first call (fork_leaf_defer_write's array reads, blk_loc / blk_read) stayed before it and failed the purity rule. Once the run holds a call, the deferral now goes to the run's top; a run with no call still leaves it at the leaf, so msort's and skyline's merge segments are byte-identical. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 3e25d4a)
A run mapped the main thread's 4 GB fork list at start-up when it had more than one thread, though its first use is in the pool, which opens only when a fork is dealt. Now the main thread maps it when the pool opens. Nothing on a hot path changes. (The fork-list part of opt/lazy-startup's cfcf168.) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Dec 1, 2026
nicolas-abril
force-pushed
the
opt/par-split
branch
from
December 1, 2026 21:55
58397c8 to
cb7f45a
Compare
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]>
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.
On main (
6180be5b)This PR is a port onto main, on
main: 16 commits of its own, with no integration history (headcb7f45a9).Port notes
segs_basehunks (another branch's base arms) are left out, and with them the unusedWL_FKARMmacros.WL_FKNOTE), and Start-up sets up per-thread allocator rows, the fork list and the wake pipe when first needed #46's fork-list part (the main thread maps its fork list when the pool opens).FID_EXITkeepsseq & 1: a heap continuation runs in sequence with no notes (safe).Checks
run/array_slabfails on main too).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)
2026-11-30 (
3108eef5, test comment58397c87; integration8e737f36): a block's cell index is a u64 on the CPU, so a field's offset folds into the load. Mechanism of the E-core register effect left aftera31ac97b: the M4's E-cores run fewer register writes per cycle when most of them go to registers of one parity (P-cores do not care). Hand edits of vss's leaf loop (a.sper variant, E-pinned 1-thread cycles, 3 nodes): renaming the loop's one temporary (themov/bfipair that builds each field's u32 index) from w19 to any even register (w2, w6, w20, w22, w24, w26) gives 0.948, to any odd one (w3, w5, w7) 1.000; moving the base register x9 to x10 changes nothing; the pair's own instructions don't matter (anlsl/orrin w19 is as slow); moving one more ALU result to an odd register costs +4%, one to an even register saves 4%. Counting FP destinations too, the slow loop writes about 18 odd and 10 even registers per iteration, the fast one about 14 and 14. A microbenchmark agrees (E-cores: 12 independent adds to one parity 4.3 cycles per iteration, alternating 3.1; integer adds next to FP adds of the same parity 5.5-6.0, of the other parity 2.2-4.3; P-cores level). So any change that moves the allocation can flip it. Fix:blk_ptr/blk_read/blk_writetake a u64 index on the CPU (BlkIx; the device keeps u32). A cell index is below 2^31 (5-bit class), soat + jnever wraps, and clang folds the field offset into the load: oneaddper element instead of amovand abfiper field, and the loop no longer depends on which registers it gets (the same edit by hand is 0.953 with any register).3587c883: 131.2), E-pinned 1 thread 0.929 cycles, 1/2/4 threads 0.997. msort, ocaml-nbody, ocaml-lu-decomp, osprey fib and pascal (bend-par), parallel-ml fib, bfs, delaunay, seam-carve, mcss, primes, tree-bitonic: level at 1/2/4/10 threads (within 1.5%; osprey fib 10 threads 0.95). All 244 programs print expected at 1 and 10 threads; 1-thread cycles geomean 0.998 (215, instructions 0.999), 10-thread wall 1.001 (98). Outliers rerun (3 x 9, then 5 x 15): interval-tree-old, mutual, reverb level; gcdsum, pfor and pfor/helpers at 10 threads within noise (their assembly is identical to integration's).preserve_most, it stays); a caller-saved clobber before the call removes half of it.array_field_offset_wrap: three-word elements set and got at indices past 2^32 - 8, in a loop and once; a version whoseblk_atdoes not mask segfaults on it). Merged into opt/integration: 1855/1980, the 125 failures exactly the three shards whose minis have no/usr/local/bun/bin/bunright now (integration alone: 1938/1979, the same cause). Gate note: a shard whose command is about 8,065-8,120 bytes long never starts (the macOS OpenSSH mux client fails withmm_send_fd: sendmsg: Message too long); the test's first comment put two shards there on integration, so58397c87shortens it. The 6 lazy_split tests and the new one: 0 live words (-DBEND_POISON) at 1 and 8 threads. comp.ts +143 ttok (92,959 to 93,102).2026-11-30 (
a31ac97b): an idle hunter's spin before it sleeps is timed and adapts (25 µs to 1 ms per thread), not 4096 empty rounds. Cause of3587c883's +7.4% on parallel-ml/ocaml-nbody-imm at 10 threads: (1) the leaf loop inlined in vss's fork segment compiles to the same instructions with other registers since the note went out of line, and that runs 9% slower on the M4's E-cores (P-cores alike; 5.5% slower at 1 thread undertaskpolicy -b); (2) the longer tails crossedfk_help's 4096-round sleep threshold (25 µs on idle cores, 170 µs with 10 threads yielding): 730-875 1 ms sleeps a run, whose wakes the drain's end waits for. The spin is now timed and adapts like POOL_WAIT's (a hunt whose drain ended within a spin of its sleep doubles it, a longer sleep halves it): 30-45 sleeps a run.3587c883: 131.2), 1/2/4 threads level. subset-sum level (its 1-thread +3.5% since3587c883is the same register effect, on the clear loop inlined in pfor's fork segment; not fixed). osprey fib and pascal (bend-par), parallel-ml fib, the BFS family, msort, delaunay, seam-carve, mcss, primes, tree-bitonic, the unbalanced and fork-every-level micros: wall and CPU time level at 1/2/4/10 threads (within 3%). All 244 programs print expected at 1 and 10 threads; 1-thread cycles geomean 0.999 (215), 10-thread wall 1.002 (98); the outliers (fib-par 1 thread, tak and collatz 10 threads) were level on rerun.[[clang::noinline]]) brought nbody-imm to 130 ms but cost ocaml-nbody 13% and lu-decomp 6% at 1 thread; for every loop it cost msort 27% (the merge took a branch for its select). A fixed 200 µs spin got nbody-imm the same 3% but cost the BFS ports up to 9% more CPU time; the spin in a_Thread_localcost the fork-every-level micro 7% at 2 and 4 threads.-DBEND_POISON) at 1 and 8 threads; new testlazy_split_leafloop(an F32 loop inlined in a lazily forking segment). comp.ts +307 ttok (92,652 to 92,959).2026-11-30 (
3587c883): forks note only shallow frames or while threads hunt; a noted frame returns through a d segment. The par-split merge made osprey fib bend-par 7.8% slower than main at 10 threads (it is 19% faster at 1 thread). Not the 20 µs limit or the 4-notes rule (turning either off changes nothing): a note at every two-call fork and its drop in the first continuation cost that port (a fork every ~38 instructions) +48% instructions at 2+ threads, and the drop's test +15% cycles at 1 thread; with notes off it ran 0.85x at 10 threads.WL_FKARM). The first continuation tests nothing.-DBEND_POISON) at 1 and 8 threads; new testlazy_split_arms. comp.ts +892 ttok (91,760 to 92,652).Stacked on #33, which is stacked on
opt/integration(1bae016): this PR's own commit is6938a7d6.comp.ts+3,465 ttok over #33 (87,877 to 91,342). Measured on one Mac (M4, 4 P + 6 E cores), GPU off, under the shared lock; other processes held at most 2.0 cores during the A/B below. The cluster gates have not been run.What
A drain runs each of its tasks in sequence on the thread that claimed it, and a task never moves. When one task holds most of a drain's work (one corner of an unbalanced fork tree), it runs on one thread while the others wait for the next turn. This is lazy task creation for the drain:
seq, a register, so one thread pays a test and a branch per fork and its first continuation).ssegment reads the second call's arguments off that frame (as its first continuation would) into a task whose answer goes to a future, in a slot of the thread's box, and writes the fork'swsegment into the frame's id word.wtakes the task back and runs it, or waits for the thread that took it, running others' tasks meanwhile; then it continues as the first continuation would.Without the 20 µs limit,
bend-runtime/tree-bitonicsplit 3.3 million small frames at 10 threads and took 2,682 ms against 1,292; with it, 1,315.Effect
Wall ms, median of 5, #33 and this PR interleaved:
The unbalanced micro now scales 8.3x on 10 threads (2.9x before). The parallel-ml programs these were found in (primes, primes-simple-bool) do not gain: they run the same instructions at 10 threads as at 1 but 2.7x the user time, memory traffic (lanes of different primes unmark the same lines of a 400 MB array), not the scheduler.
Cost. Fine-grained forks pay the note and its drop: the fib micro (a fork every ~38 instructions) +31% at one thread (+4.7% instructions; the rest is the drop test in the first continuation, measured by removing it by hand), +39% at 10. Over the 244 bend-benchmarks programs at one thread, instructions against #33: geomean 1.0016 over the 215 outside the set apart (the harness-hoisted 28, rbtree-ck), within this machine's run-to-run noise of up to 2.7%; above it:
osprey/fib(bend-par) +6.5%,polybench/atax+5.1%,osprey/pascal(bend-par) +4.3%,polybench/bicg+3.9%. At 10 threads, wall geomean 0.980 over 157 programs above 0.1 s (one run each).Checks
expected.txtat 1 and 10 threads.-DBEND_POISON, live words) at 1 and 8 threads on the 117 tests that fork and the new tests: 0 live words butio/heap_large_reuse(65,536, as on integration).!tests built withbend -o, run with--gpu on, print the same as on integration; the notes are empty on the device.tests/run/:lazy_split_deep(a fork chain 20,000 frames deep beside a filler tree, both sides),lazy_split_unbal(item 0 holds half the work),lazy_split_results(second calls returning a tree, a list and a pair),lazy_split_shared(one array forked to every leaf). Right at 1, 2, 4 and 10 threads, 3 runs each.🤖 Generated with Claude Code