Skip to content

Start-up sets up per-thread allocator rows, the fork list and the wake pipe when first needed - #46

Merged
nicolas-abril merged 5 commits into
opt/integrationfrom
opt/lazy-startup
Nov 27, 2026
Merged

nicolas-abril merged 5 commits into
opt/integrationfrom
opt/lazy-startup

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Owner

Merged into integration at 4561e97 (opt/lazy-startup 0efeead, after merging integration babd999; lane statuses equal to integration's; 242 of 244 benchmark programs print expected at 1 thread and the 98 parallel ports at 10, the same two as integration exceed the memory caps: kostya/json at 4 GB, perceus/deriv's emit at 5 GB).

Stacks on opt/integration (418a706). comp.ts +78 ttok (101,434 to 101,512). Runtime only: every program's C changes, in start-up code.

What it does

A Bend binary starts about 0.18 ms after a C or Koru one (OPTIMIZATIONS.md §44). This makes three pieces of start-up happen when they are first needed:

  • Allocator rows. corpus_setup set the counts of all 128 lanes' allocator rows (128 KB of ALC, 8 pages touched). Only the pool's threads use a row: each thread now sets its own when it starts (alc_open), the main thread in corpus_setup.
  • The main thread's fork list. With more than one thread (the default is the core count), io_loop mapped the main thread's 4 GB fork list at start-up. Its first use is in the pool, which opens only when a fork is dealt; pool_open now maps it.
  • The event loop's wake pipe. Only IO helper threads write it; it now opens with the first helper (io_work), and io_wait leaves it out of its select until then.

Nothing on a hot path changes (one io_size != 0 test per io_wait).

Where the 0.18 ms goes, and what is left

Measured on the empty program (one IO.print), M4:

piece cost
a bigger binary (load, before main) ~25-40 µs not addressed
allocator rows for 128 lanes 7 of the 19 page faults taken inside main; ~2 µs lazy here
wake pipe ~12 µs lazy here
4 GB fork-list map (≥ 2 threads) ~4-15 µs lazy here
each large mmaped region with touched pages (heap 8 GB, stack 2 GB) ~10-15 µs each, mostly paid at exit (a probe: 1 region +12 µs, 3 regions +40 µs over none) not addressed: the heap and the stack must exist; merging them into one region was inconclusive in a noisy probe
evaluating main and printing through the IO loop ~5-7 µs warm not changed: the IO loop's own work is small; the write is the same as C's

Instructions: 12.20M to 11.93M on the empty program (C: 10.14M).

Results

hyperfine, -N, 500 runs (200 for the kernels), both orders, median (ms):

program threads integration this
empty 10 1.094 1.071 -23 µs
empty 1 1.082 1.077 -6 µs
listops 10 1.171 1.146 -26 µs
listops 1 1.156 1.140 -16 µs
textstats 10 2.441 2.422 -19 µs
textstats 1 2.446 2.420 -25 µs
digitsum 10 6.593 6.572 -21 µs
digitsum 1 6.595 6.574 -21 µs
primes 10 5.469 5.469 0
primes 1 5.470 5.462 -8 µs
wordfreq 10 1.322 1.298 -23 µs
wordfreq 1 1.313 1.291 -21 µs

On the previous integration base the same runs gave 10-15 µs on the empty program and 10-60 µs on the kernels. This closes roughly a tenth of the 0.18 ms gap; the rest is below.

Instructions on all 244 bend-benchmarks programs (one run each, 98 parallel ports also checked at 10 threads, all print their expected output): 243 measured (kostya/json was killed by the 4 GB cap in this round; on the previous base it measured 5,549M to 5,548M). Instructions change by -0.23M at the median (quartiles -0.47M and -0.01M; -0.009% median relative); the four largest relative increases in the single runs (parallel-ml dedup-entangled +1.4%, linefit-opt +1.4%, linearrec +1.1%) are -0.3%, -0.1% and -0.3% over 5 runs each.

Loss over 5%, placement: osprey pascal runs +12% cycles at equal instructions (379.9M to 425.4M, median of 5; board 95.1 to 106.9 ms) and tak -10% the other way (board 31.0 to 27.9 ms). Nothing in their loops changes; the runtime's new text moves their code. Over 16 code positions pascal is 0.885x to 1.114x of integration (median 1.039x), so the +12% is one bad position, not a cost of the change.

Checks

  • Native lane (1,123 tests): every status equal to integration's (974 pass, 27 FAIL, 123 not built on either).
  • Memory (-DBEND_POISON, live words at 1 and 8 threads, all 1,123 tests since every C changes): all 1,001 that build give the same output, exit code and live words as integration at 1 and 8 threads (8 tests leave words on both: 5 network/heap IO tests and 3 array tests).
  • GPU (Metal; previous integration base): all 20 GPU programs and the 56 bang tests print their expected lines at --gpu on and --gpu off --threads 10; GPU wall 0.98x to 1.02x of integration, CPU 10 threads 0.95x to 1.04x.
  • Wrong versions: the pipe never opened is caught by the IO tests (file_read_line, random_u32, file_write_at). Not caught, as expected since they change speed and not results: pool threads not setting their allocator rows (their frees hand blocks to the bank at once), the main thread never mapping its fork list (it then never notes forks), and io_wait selecting on fd 0 before the pipe exists.
  • Not run: cluster gates, CUDA, Linux/x86.

Not done

"Print main's result directly when no IO loop is running": measured at 5-7 µs for the whole of main's evaluation and print, most of it work C does too; not worth a second print path.

🤖 Generated with Claude Code

nicolas-abril and others added 5 commits November 25, 2026 00:39
… wake pipe with the first IO helper

A run set the allocator counts of all 128 lanes (8 pages of 128 KB, though
only the pool's threads use a row), mapped the main thread's 4 GB fork list
at start-up when it had more than one thread (its first use is in the pool,
which opens only when a fork is dealt), and opened the event loop's wake
pipe, which only IO helper threads write. Now each thread sets its own
allocator row when it starts, the main thread maps its fork list when the
pool opens, and the pipe opens with the first helper thread; io_wait leaves
the pipe out of its select until then. Nothing on a hot path changes.

Empty program (one print), M4, hyperfine 500 runs x 2 orders, median:
1.036 to 1.025 ms at 10 threads, 1.032 to 1.020 ms at 1; 12.17M to 11.94M
instructions.

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