diff --git a/.agents/a64/performance.md b/.agents/a64/performance.md index c3e6541..86261f2 100644 --- a/.agents/a64/performance.md +++ b/.agents/a64/performance.md @@ -52,7 +52,9 @@ and rejected designs are in kwemu.md. first (`uptime`): with about six other busy cores the same bundle differed from itself by 14-68% per row (2026-11-21), while its counters stayed within 3%; on a quiet host rows spread 3-5% (`node cpu.js` first - runs more). + runs more). Much of the spread that remains comes from the guest's + address randomisation, and effects of 1-2% need paired cycles: kwemu.md, + Measuring, Comparing two builds. - **Counters**: a `-Dkwemu-stats` kernel (`kernel/build.sh stats`, CLI `--stats`, or `kw_debug_stats` per window: kwemu.md, Measuring). The A64 tier adds `tier.Ax` in JitStats.hb_gen (interpreted instructions by @@ -227,8 +229,8 @@ SIMD host, "no SIMD" the same code as interpreter calls): A host without wasm SIMD is 2-13x slower than the integer paths would be; it could boot with `kw.isa=-sha512` or set `OPENSSL_armcap=0` (status.md). -**Tenured ring** (`ten_default` 128K ops beside a 320K-op nursery, -`KW_CPU_SIZE` 9 MiB). The 64K ring, which is FIFO, could not hold one +**Tenured ring** (`ten_default` 128K ops, measured beside a 320K-op +nursery, which is 285K now; `KW_CPU_SIZE` 9 MiB). The 64K ring, which is FIFO, could not hold one run's compiled set of a Bun-based program, so every run compiled again; non-PIE binaries reuse what the ring keeps (Where the time goes, Compiled code across processes). Taking the 128K ring out of a 384K-op arena (a @@ -383,8 +385,9 @@ evicts 78-706 compiled blocks per step after the first. instruction share from the guest profile: 2.5-3.6x). Compiling is 4.6% of a claude run (`jit.compileMs` 142 of 3113 ms). V8's worker threads take 36% of the process's cycles on `claude --help` and 13.5% on npm - with the JIT on (unsymbolized; likely tier-up of the modules), invisible - in wall time on an idle 16-thread host. The wasm64 harness's hot loops + with the JIT on (unsymbolized; tier-up of the modules: TurboFan CPU on + background threads fell 28-30% with 27% fewer module bytes, kwemu.md, + Numbers), invisible in wall time on an idle 16-thread host. The wasm64 harness's hot loops (17.6-21.3 cycles, 69-73 host instructions, 0.007-0.10 mispredicts per guest instruction, whole process) do not compare with these. - **apk.** Outside compiled code nearly all is blocks counting down @@ -408,7 +411,29 @@ evicts 78-706 compiled blocks per step after the first. chacha20-poly1305); exclusive pairs and CASP never ran in any workload. - One stats boot showed an ash loop at 82% compiled with 12% of its instructions in tails after data-TLB misses: the direct-mapped data TLB - aliases hot pages in some address layouts; other boots do not. + aliases hot pages in some address layouts; other boots do not. The + guest's address randomisation decides it per process, and a full + block-table set does the same to compiled blocks; frequencies and + costs per workload: kwemu.md, Numbers, Variance between runs. +- **Perl kernels** (2026-12-03, `work.pl 8`, the Debian perl 5.40.1, + UP bundle, 1 CPU, 1024 MiB, later runs, medians of 9 over 3 boots): + 39.85 s (37.57-41.65) in headless Chromium 158 on the page's main + thread and 37.57 s in Node 24, 3-6% faster; the first run of a boot + is 0-4% slower. Per kernel in Chromium (sieve, hash, sort, regex, + recursion, float): 7.09, 7.16, 7.98, 7.29, 3.75 and 6.03 s for 4446, + 3243, 3457, 2334, 1930 and 2441 M instructions, that is 627, 453, + 433, 320, 515 and 405 MIPS. Stats bundle, each kernel as its own + process, second run: compiled share 98.8, 99.6, 95.4, 97.7, 99.6 and + 99.5%; transfers between modules per 1000 instructions 18, 26, 35, + 41, 26 and 27; module KiB compiled 1206, 1367, 2039, 6096, 830 and + 1047 in 37-222 ms. Time per instruction follows the transfer rate + (regex slowest, sieve fastest); the regex kernel also compiles the + same code again and again (466 compiles in a second run). Compiling + is under 3% of each kernel's time. Per guest instruction the kernels + run 19.1, 20.1, 23.1, 24.0, 19.4 and 24.0 host instructions and 6.9, + 9.1, 9.9, 13.5, 7.9 and 10.0 cycles (Node 24, the kernel's thread). + What a transfer costs and where: kwemu.md, Numbers, Transfers between + modules and the entries after it. ## Next levers (ranked, 2026-11-20) @@ -444,8 +469,31 @@ Apk and shell rows: 2. **NZCV materialisation**: about 3%. Carrying lazy state across member transfers needs a runtime kind for the checked entry's stop path. -The shared-core levers (bigger regions, self-loop exits, threshold) are in -kwemu.md, Next steps. +The shared-core levers come first for long-running compiled code (the +Perl kernels, node and bun loops; kwemu.md, Next steps; 2026-12-04). +The host is front-end bound on it: perl-k2 and perl-k4 run 20-23 host +instructions and 9.4-13.2 cycles per guest instruction with 37-39% of +pipeline slots lost to the front end, from 246-400 KiB of hot code, so +a change pays only if it removes host instructions without adding hot +code. Register traffic by path (bounded at 1.8-6.7% of host +instructions), a hashed dispatch for indirect jumps inside a region, +regions formed once, bigger regions, registers as wasm parameters, +per-target links and module format v2 are all measured and rejected +(kwemu.md, Rejected designs): each cut transfers or instructions and +lost in cycles to the code it added. Open there: structured forward +branches for hops inside a region and a shorter path through the +chained exit (both in progress, unmeasured), the two variance +mechanisms. + +In this front end, `scvtf` and `fcvtzs` with the integer in a V +register run inline (2026-12-04): perl-k6 made 1.17M calls per run to +the generic FP helpers (`simd_fp.mapUn`, `miscFp`, `fparith.toFixed` +through `execGeneric` and `jitGeneric`, about 3.4% of its main thread), +all for these two forms; inline, perl-k6 is 6.2% faster in cycles (9 of +10 paired rounds) and no other row or the code size moves. Together +with the 16384-entry data TLBs the 16 rows of `test/work-bench.sh` are +3.8% faster in sum (kwemu.md, Numbers, The data TLB). +`decode.exec` is another 2.1% there. ## Rejected designs (A64) diff --git a/.agents/kwemu.md b/.agents/kwemu.md index 545cccc..fbac59e 100644 --- a/.agents/kwemu.md +++ b/.agents/kwemu.md @@ -53,9 +53,10 @@ block is shaped is the ISA's (a64/tier.md). the access's last byte, so page-straddling accesses take the slow path for free (still about 15 wasm instructions per access). Read and write TLBs are direct-mapped, 16384 entries each (512 KiB); a log of the last - 256 fills lets a full flush clear only those. Against 256 (riscv64 - guest, 2026-11-15): apk, sort, gzip and an ash loop 1.5-3% faster; 4096 - gained nothing (Rejected designs). + 256 fills lets a full flush clear only those (why this size and the + log: Numbers, The data TLB). Two hot pages whose page numbers are + equal modulo 16384 evict each other on every access (Numbers, Variance + between runs). - **Ops.** The op arena is an array of 16-byte slots (`kwemu.Slot`) the ISA views as its own op type (`Cpu.opArena`). One guest instruction is one op, so an op index is a count of retired instructions; what the ISA @@ -74,7 +75,8 @@ block is shaped is the ISA's (a64/tier.md). The exec TLB is 2-way (256 sets), and every fill gets a fresh 64-bit generation. Both the 2-way exec TLB and that hash are needed: a 1-way exec TLB or a `pc >> 16` hash made an ash loop vary between 3 and 5 s - (riscv64 guest). + (riscv64 guest). Three hot blocks in one set still thrash (Numbers, + Variance between runs). - **Coherence.** A block is trusted only under the generation it was validated with. Flushes, `kw_set_pgd` and instruction-cache invalidation (A64 IC IVAU, `Cpu.icInvalidate`) retire generations. A stale block is @@ -124,8 +126,8 @@ block is shaped is the ISA's (a64/tier.md). the survivor loses half its progress. Only kw_cpu_init, kw_set_isa and kw_host_reset drop everything. Why so: a64/tier.md, The op arena. The block table cannot grow past 64K entries (region members are u16 block - indexes); a nursery beyond 320K was no faster (a64/performance.md, - Tenured ring). + indexes); a larger nursery was no faster (measured beyond 320K ops; + a64/performance.md, Tenured ring). - **FP** (`fpu.zig`). All five rounding modes and flags exact. RNE uses host-float fast paths with exact inexact detection. f64 FMA uses Boldo & Melquiond's emulated FMA (Dekker product, TwoSum, round to odd), @@ -240,10 +242,14 @@ names the member a region left from. closed). Calls into libc and PLT stubs, functions spanning pages, blocks of the page compiled into other modules and self-loops entered from the region then stay in one module. `noteExit` tests the closed - bit inline: most exits leave closed regions. A grow recompiles the + bit inline: on app workloads most exits leave closed or full regions + (Numbers, Transfers between modules). A grow recompiles the whole region (a few hundred µs), so the threshold is high and per KiB makes big regions wait longer; a round trip through `run` costs about - 30 ns (A64, 7-Zip's LZMA, 2026-11-23). + 30 ns (A64, 7-Zip's LZMA, 2026-11-23). The threshold is close to + break-even: one recompile of a 14 KiB region costs about as much as + 190K transfers (estimated, 2026-12-02), and faster growth loses + (Rejected designs). - **Checked entry** (`checkedEntry`). Every branch into another member m goes through m's checked entry: `jl` covers all of m; `*irqp` is 0; and at the first entry into m in a run of the function, m's `op` is still @@ -462,6 +468,27 @@ same region, a self-loop module, another region that could grow, or a full one). The A64 counters and bench scripts: a64/performance.md, Measuring. +**Comparing two builds** (2026-12-03, one x64 host, V8). Judge by +main-thread cycles (`perf stat` of the kernel's thread), paired per round +against the reference boot of the same round, later runs, with +instructions alongside: changes that ran fewer host instructions have +lost in cycles more than once. A build against a byte-identical copy of +itself shows per-row medians of up to ±2% at 8 rounds and -6% to +2.4% +at 5, and single rounds from -29% to +26%, so a row that moves 1-2% is +not resolved. Most of that spread is guest address randomisation +(Numbers, Variance between runs): with `randomize_va_space` 0 the +hand-back counts of a row are the same in every run (`ASLR=0` in +`test/work-bench.sh` and `test/app-bench.mjs`), which should make small +effects measurable; a fixed layout may also sit in the slow mode in every +run, so look at the hand-back and stop counters of both sides. When +instructions fall by several percent on a row the change should not +touch, suspect the layout of those boots, not the change. Check that two +bundles that should differ are not byte-identical: `zig build` can +install a cached `libkwemu.a` with its old modification time, and the +kernel is then not linked again. The unit tests pass when a module is +invalid wasm (the block stays interpreted): read the `compile failures` +count in each variant's summary line. + **Guest pc profile** (any build; CLI `--profile FILE [--profile-hz N] [--profile-insns N] [--profile-root DIR]`, SDK `createKernel({ profile })`, `vm.profile()` and `symbolizeProfile()`, `js/core/profile.ts`; kernel @@ -543,7 +570,355 @@ JIT: no errors, and each CPU still collects what it frees. of `KW_CPU_SIZE` spare, 3,312 in a `-Dkwemu-stats` build (`jc.State` is 117,376 bytes, of them the 32 KiB emit buffer and `members` and `member_op` 64 KiB at 16 members; every block carries an 8-byte `jx`): -check the headroom before growing JIT tables. +check the headroom before growing JIT tables. A table can take its room +from the op nursery instead, with no change to `KW_CPU_SIZE` or the ABI +(a 32 KiB table costs 2048 ops; the data TLBs past 1024 entries, +their fill log and the reverse map take 35K ops this way). + +All of the following: 2026-12-02 to 12-04, i7-10700K, Node 24.22.1 (V8), UP +bundle, 1 CPU, the rows of `test/work-bench.sh` (Perl kernels `perl-k1` +to `k6`, `perl-start`, Python start and compile, a C build, a shell +process loop, dpkg, `claude --help`, `npm --version`, `bun cpu.js`, +`node cpu.js`); other engines, arm64 hosts and SMP were not measured, +except by the synthetic model at the end. + +**Transfers between modules.** A chained transfer costs about 65 +main-thread cycles more than a hop inside a module (64.5, fitted over +18 Perl points), and about 190 host instructions (measured by +injecting transfers: 190 instructions and 60-70 cycles each). The rows +make 2-39 transfers per 1000 guest +instructions (Perl kernels 26-39, node 27, python 25-28, claude 22, bun +and the C build 19-20, npm 12.5, the shell loop 2.3), which is an +estimated 16-25% of main-thread cycles on the Perl kernels, 9% on node +and bun, 7-8% on python, 5% on claude and the C build, 2-3% on npm and +`perl-start`, 0.4% on the shell loop. By the branch that leaves (Perl +kernels / the other rows): side exits 9-44% / 17-64%, BL 4-15% / +11-28%, BLR 2-27% / 0-16%, RET 19-41% / 11-32%, BR 0-9% / 0-15%. A +64-entry shadow call stack predicts 68-86% of the Perl kernels' RET +transfers at its top entry (apps 41-78%). 75-93% of transfers (apps +78-100%) leave exit sites that only ever had one target; the hottest +target module of a source module takes 44-63% (apps 50-77%), the top +four 82-95%. The region they leave is open in 41-69% of the Perl +kernels' transfers and 18-37% of most other rows' (node 60%), full (16 +members) in 16-49%, closed by bytes in 0-39%, a self-loop module in +0-14% (the shell loop 30%, `perl-start` 25%). Inside regions the Perl +kernels take 54-79 member hops per 1000 instructions (`loop` + +`br_table`), and 0.05-4.5 entries through `run`. + +Where the cost sits (host profile of perl-k1, k5 and k6, later runs, +share of main-thread samples): module bodies 52.9%; the chained exit +9.6% (the block-table lookup 5.3%, the compiled test and growth +counting 1.8%, the rest 2.5%); the prologue 9.0% (frame 2.7%, register +loads 3.3%, dispatch to the entered member 2.0%, moves 1.0%); the +epilogue 3.1%; V8's tail-call sequence 2.1%; exit edges 2.0%; loop head +and `br_table` 1.6%; `kw_run` 8.1%, modules still in Liftoff 4.3%. So +code tied to transfers is about 27% of the main thread. A +second profile over all six kernels (`work.pl 8 1`) agrees: module +bodies 60.1%, code tied to transfers 29.0% (the lookup 5.5%, prologue +loads 3.9%, the epilogue 3.8%, frame 2.8%, the tail call 2.3%, exit +edges 1.9%, loop head and `br_table` 1.8%, dispatch 1.7%, growth +counting 1.7%, the rest 3.6%), `kw_run` 4.1%, other interpreter +functions 4.3%, small or Liftoff modules 2.6%. + +**Host cost per guest instruction, and a loop that fits one region** +(2026-12-03, same host, the kernel's thread only). The Perl kernels k1-k6 run 19.1, +20.1, 23.1, 24.0, 19.4 and 24.0 host instructions and 6.9, 9.1, 9.9, +13.5, 7.9 and 10.0 cycles per guest instruction (9.29 weighted), at +1.8-2.8 instructions per cycle and 1.7-8.6 branch misses per 1000 guest +instructions; module bodies alone are 5.58 cycles and about 12-13 +host instructions per guest instruction (estimated from the profile +shares). A Perl-shaped dispatch loop in the JIT harness costs 21.4 +host instructions and 6.4-7.0 cycles per guest instruction with 16 +members; built with 32 members, so that the loop is one region and 8.7 +transfers per 1000 guest instructions go away, 18.5 and 5.3-5.4 (four +alternating rounds). That is about 330 host instructions and 150 cycles +per avoided transfer, but the same change turns 16 compiles into 1 and +alters the region's shape, so it overstates a transfer: use the 190 +instructions and 65 cycles above. Harness kernels that fit one region +run from a few KiB of code that stays in the L1 instruction cache, so +they also understate what module bodies cost on Perl (Front-end bound, +below). What a transfer carries: + +- *Register traffic.* The prologue and epilogue follow the union over + all members (Regions, Register file): 150 sampled Perl modules have + 7.6 members, 17.3 prologue loads and 17.3 register store sites on + average (21.7 loads with 8 or more members), whatever the entered + path uses. Loading and storing all 32 X registers instead added about + 63 host instructions per transfer for about 34 more moves, so the + union is about 60 host instructions per transfer on Perl (estimated + from that). The union grows with the region (20-25 registers each way + at 32 or 64 members). Little of it is recoverable: see Register + traffic by path below. +- *The lookup's instructions are nearly free.* Running the block-table + lookup twice in the chained exit changed neither instructions nor + cycles beyond noise (21.42 against 21.40); its profile share is data + misses on the block table and exec-TLB lines, or sample skid. This is + why a transfer cache gains little. +- *Everything else in the one exit path:* the compiled test, growth + counting, budget, poll, charge, the `cur` stores, the tail call, then + the callee's frame and its dispatch to the entered member. + +**Indirect jumps inside a region** compare the target against every +member in turn (`memberTail`, the ISA's region hook): the final module +of the dispatch loop above holds 260 `i64.eq`. Emitting each compare +twice added 1.15 host instructions and about 0.5 cycles per guest +instruction at 180 member hops per 1000 (22.5 against 21.4, four +rounds), so the chain costs on that order there. On real workloads +(2026-12-04, stats build) most indirect hops leave the region: 6-62% of +them land on a member, and the chain walks about 7-11 compares per hop. +Replacing the chain did not pass (Rejected designs). + +**Register traffic by path** (2026-12-04, a census in a stats build: +at every exit of a module function, the registers the path taken since +entry read before writing and wrote, against the union the prologue +loaded and the epilogue stored; X0-X30, SP, V0-V31, NZCV and TPIDR_EL0 +as one slot each). A module function runs 26-45 times per 1000 guest +instructions on the Perl kernels and moves 14.2-19.8 loads and +13.8-17.4 stores per run. A perfect mechanism (load at first use, store +only what the path wrote, the exiting member counted up to the op it +left at) would still move 7.3-9.9 loads and 8.3-11.1 stores: 0.44-0.75 +of today's traffic on Perl, 0.36-0.50 on the app rows (python and perl +start, the C build, dpkg, claude, node). Only runs that never leave the +entered member are small (3.2-5.3 loads, 4.5-9.1 stores), and 42-88% of +Perl runs hop on (2-3.3 members per run; 26-46% on the app rows); one +Perl block alone touches about 10 slots. At about 1.85 host +instructions per slot the perfect bound is 1.8-6.7% of host +instructions on the Perl kernels (estimated); the one cheap design +(the entered member's registers at entry, the rest of the union at the +first hop, that member's writes at exit) 0.4-3.8% before its own cost +of a dispatch per run and a test per hop (about 1-2%). So register +traffic is not where most of a transfer's 190 host instructions can be +recovered. + +**Growth by re-emission.** The dispatch loop took 15-16 compiles and +123-125 KB of wasm to end as one 15 KB module; with 32 members and no +need to grow, 1 compile and 20 KB. A compare tree of more than 16 +blocks took 7 compiles and 116 KB, at 16, 32 or 64 members: the limit +is not the member count. Its three self-loop blocks each get a module +of their own first (a self-loop is never a member of a first region) +and then join one exit grow at a time; a block that gets hot late takes +its page over and starts another round. Every later run of the same +Perl script compiles 52-244 modules and 0.76-3.1 MB again, and V8's +background threads cost 0.6-5 cycles per guest instruction. + +**What growth does and what stops regions** (2026-12-04, stats builds +without chaining, guest ASLR off unless said; perl-k2, perl-k4, python +compile, bun's cpu.js): + +- Growth removes about 30% of transfers (perl-k2 25.4 per 1000 guest + instructions in later runs against 37.8 with growth off, perl-k4 43.5 + against 51.1) and causes most recompiles of a later run: on perl-k2 + 57% and 88% of the members recompiled in runs 2 and 3 had been + compiled at the same pc and were freed by an exit grow; perl-k4 has + 81-104 grows and 48-57 exit grows per run. Perl sees no clock or ring + eviction. On python compile and bun the tenured ring is the main + cause (bun run 3: 1205 of 2154 same-pc recompiles were ring frees, + 603 grows, 302 reused block-table ways). +- With guest ASLR on a later run compiles about as much as the first + (perl-k2 42-61 compiles against 17-19 with it off): 63-81% of the + recompiled members are code that was compiled earlier in the boot at + another address. +- Transfers on Perl leave regions that are still open 42-50% of the + time, full ones 45-50% (perl-k2), ones closed by the byte limit + 20-25% (perl-k4). By exit kind: perl-k2 RET 32-41%, BLR 17-27%, side + exits 10-21%; perl-k4 side exits 43-47%, RET 16-20%, BL 15-18%, BR + about 11%. +- What the region search refuses is mostly "not decoded yet" (538 of + 620 refusals in perl-k2's first run), then "compiled elsewhere" and + self-loops. Exits of open regions are spread evenly over their growth + progress: a region reaches its shape one grow at a time. +- Per transfer the prologue and epilogue move 16-21 registers each way + at 16 members, 20-25 with 32 or 64. + +**Front-end bound** (2026-12-04, perl-k2 and perl-k4 at size 8, later +runs, the kernel's thread, host counters; per guest instruction unless +said): + +| | perl-k2 | perl-k4 | +| --- | --- | --- | +| Host instructions | 20.25 | 23.41 | +| Cycles | 9.37 | 13.21 | +| Pipeline slots lost to the front end | 36.7% | 39.2% | +| Stalled on L1 instruction-cache misses, share of cycles | 10.5% | 18.0% | +| Stalled on iTLB or tag misses, share of cycles | 5.9% | 9.0% | +| L2 code misses per 1000 host instructions | 15.5 | 36.1 | +| Branch mispredicts per 1000 host instructions | 0.22 | 0.37 | +| Stalled on memory, share of cycles | 12.6% | 20.3% | +| V8's other threads, cycles | 0.95 | 2.48 | + +It is the steady state, not warm-up: size 24 minus size 8 gives 18.6 +instructions and 8.54 cycles on perl-k2, 22.1 and 12.8 on perl-k4. +Mispredicts and completed iTLB walks (0.008-0.023 per 1000 host +instructions) are not a cost. The code lines that take 90% of the +cycle samples are 246 KiB on 234 pages in 41 functions (perl-k2) and +400 KiB on 457 pages in 99 functions (perl-k4), against a 32 KiB L1 +instruction cache; V8 holds 1621 TurboFan functions of 21.4 KB on +average, spread over 97 MiB with Liftoff copies between them. Inside a +hot module 4-9 KiB of lines carry 90% of its samples, but they span the +whole function, because the epilogue and the chained exit sit at its +end: the first 1 KiB of a module function (frame, register loads, the +dispatch to the entered member) takes 16% of main-thread cycles and the +last 3 KiB 14-20%. Fetch stalls are spread like the cycle samples, not +concentrated in one place. + +So a change pays only if it removes host instructions without adding +hot code bytes, functions or indirect-branch sites. Every change that +cut transfers or host instructions by adding code lost in cycles on +this host: regions formed from recorded edges (transfers -27 to -32%, +host instructions -2.2%, cycles +3.3%; on the Perl rows alone with +guest ASLR off it gained, cycles -1 to -5.6%, and lost on the start-up +and app rows), bigger regions, registers as parameters with per-target +links (instructions +3.2%, cycles +14.8%), module format v2 (Rejected +designs). What gained removed instructions or bytes and added none: +fewer module bytes, the transfer cache, the inline FP conversions, the +larger data TLB. Branch hints added to the modules by a post-processor +(V8's branch-hinting flag on, 4 paired rounds): compiled bytes -9% and +-22%, tag stalls -23% and -12% (4 of 4), cycles +0.6% on perl-k2 and +-4.0% on perl-k4 (3 of 4, one noisy base round): not decided. + +**The data TLB** (2026-12-04, 16 rows, main-thread cycles). 16384 +entries per data TLB with the fill log and the reverse map, together +with the inline FP conversions (a64/jit.md), against 1024 entries with +a scan of the write TLB and generic calls: sum -3.8% cycles and -3.7% +host instructions over 4 paired rounds, lower in 4 of 4, every row at +or below zero. Over 10 paired rounds pooled over guest ASLR on and off: +sum -2.7% (lower in 9 of 10; instructions -2.7%, 10 of 10), dpkg -8.4%, +perl-k6 -6.5% (the conversions), python start -5.3%, claude -4.4%, bun +-2.6%, npm -1.7%; `perl-start` +1.7% there (4 of 10) and -1.2% (9 of +10) in 10 rounds of its own. What decides the shape: + +- *No scan of the write TLB.* A page gets its first marked chunk again + after every nursery reset (about 820 pages per reset in npm), so + `markCode` runs 36-40 thousand times per `npm --version` run. A scan + per call visits 648M entries per run at 16384 entries: npm +18% (and + claude, the C build, bun and node slower), +4.3% at 4096, slower in + every round. Through the reverse map the run visits 6.8K entries, and + about 100 of the 40K calls find an entry to flag. At 1024 entries the + map takes 3.2% off npm's host instructions and nothing off its cycles + (the scan is a streaming loop). A log of filled write entries instead + of the map does not help: it overflows inside one long node process. +- *The fill log.* Without it the shell process loop is 3.9% slower (4 + rounds): every context switch clears 2 x 16384 tags. With it the loop + is 1.9-2.2% faster than at 1024 entries. +- *16384 rather than 4096.* 4096 entries gain less (sum -2.3% against + -3.8%, 4 rounds) and left perl-k5 at +4.8%. +- *Memory.* 544.5 KiB per CPU (the TLBs past 1024 entries, the log and + the 64 KiB map) come out of the op nursery: 35K of 320K ops, with + `KW_CPU_SIZE` and the ABI unchanged. npm resets the nursery 51 times + per run instead of 44 and is still faster; the same shrink alone + costs npm about 1%. +- Nothing is added to the store fast path, the read path or compiled + code. A check of a per-page code bit on the store fast path would add + an instruction there and was not built. Keeping marks across nursery + resets would send stores to stale chunks down the slow path and fill + the mark sets; with the map a re-mark is one bucket walk. + +**Outside compiled code** (shares of the main thread, 2026-12-03, with +1024-entry data TLBs): perl-k3 `kw_run` 6.2%, the data-TLB slow paths +(`Cpu.loadSlow`) 4.2%, `Cpu.translate` 1.4%; perl-k4 `kw_run` 4.8%; perl-k6 `decode.exec` 2.1%, `kw_run` 1.7% +and about 3.4% in generic FP helpers (`simd_fp.mapUn`, `miscFp`, +`fparith.toFixed`, `execGeneric`, `jitGeneric`). Those generic calls of +perl-k6 (1.17M per run) are all `scvtf d,d` and `fcvtzs d,d`, which the +a64 JIT emits inline (a64/jit.md): perl-k6 6.2% faster in cycles (9 of +10 rounds, 6.5% fewer host instructions), no other row and not the code +size moving (2026-12-04). + +**Engines and hosts, synthetic** (2026-12-03). A hand-built model of +the boundary (units of one function, 16 registers, 256 live units) +reproduces its cost on the x64 host (62-66 cycles above a hop inside a +module). The same model on an M4, ns per hop above a no-boundary +control: + +| | Chrome 158 | Safari 27.2 | +| --- | --- | --- | +| Today's boundary, one module per unit | 40.0 | 15.4 | +| The same, each module's code start moved off the page boundary | 14.7 | no effect | +| A direct tail call with the registers as parameters | 2.2 | 4.5 | + +V8 starts each module's code at a page boundary, so the entries of +one-function modules share instruction-cache sets: about 2.5x per +transfer in the model on Apple silicon (the same with 12 KiB units, +42.4 against 14.7), small on the x64 host. Whether real modules can be +moved is open: emitting a varying number of empty functions before the +region function moved Liftoff code on x64, but V8 still placed 30 of 79 +TurboFan modules at page offset 0, and cycles were +2.0% (16 rows, 2 +rounds); not yet timed on Apple silicon. The parameter count of a tail +call is free in V8 (flat about 2.3 ns from 5 to 64 i64 on x64, 4 to 31 +on the M4) but not in JavaScriptCore (5.7 ns at 4 registers, 7.1 at 16, +11.0 at 31). A guest call and return as wasm `call`/`return` is slower +than a tail call on both engines once returns miss (4.1-5.9 ns against +2.2 at depth 8 in Chrome; on x64 about 200 cycles per unmatched return +at depth 8). The model has tiny bodies, no entries from `run` and no +misses, and it overestimated what a real emitter gains (Rejected +designs, registers as parameters). + +**Compiled volume.** V8 compiles a module of 12.1 KiB (the mean) in 334 +µs with Liftoff (64 µs + 0.022 µs per byte) on the kernel's thread and +5.52 ms with TurboFan (0.47 µs per byte) on a background thread. +Main-thread compiling is about 4% of a later run. Bytes as such barely +cost main-thread time: padding every module with code that never runs +(+34% bytes) changed main-thread cycles by +0.1% (compile time +5%, +background threads +7%). What costs is compiling the same code again +and running it in Liftoff until TurboFan delivers: where regions grew +more (Rejected designs, 32 members), each further compiled KiB went +with about 0.88 M main-thread cycles (fitted, R² 0.72; it mixes +recompiles, Liftoff time and the entry cost of larger modules), and a +profile showed TurboFan module code, `kw_run` and stubs 19% cheaper but +Liftoff code 4.6x and compiling +44%. Regions compiled again by grows +are an estimated 3-4% of main-thread cycles on most Perl kernels (k4 +9.4%), 1-5% on the other rows, 10.8% on `bun cpu.js`, 0% on the shell +loop. V8 does not inline a call from a function past its inlining +budget (`--wasm-inlining-budget`, 5000 TurboFan nodes; region functions +are larger), and a helper function inside each module is called too +rarely to leave Liftoff, so code moved from the fast path into a wasm +function gets slower (Rejected designs). `--wasm-tiering-budget` changes +nothing here. + +**Module bytes.** 1601 bytes per member over all rows (6653 modules, +12.1 KiB each). Shares, which overlap: data-TLB checks 40.2% (their +miss arms 19.3%), NZCV syncs at exits and member transfers 20.6%, exits +and hand-backs 11.8%, checked entries 7.2%, member branches 6.3%, op +bodies and the skeleton 29.1%. Shared sync stubs on leave arms, no pc +store on hand-backs, one stop arm per checked entry, a shared lookup +for indirect jumps and leave arms that carry their code as a block +value took 27% off (1175 bytes per member) with the member policy held +fixed: main-thread compile time -20 to -23%, background TurboFan CPU +-28 to -30%, main-thread cycles about -1% (within the ±2% a row +resolves; at -20% bytes a module compiled only 8% faster in Liftoff and +12% in TurboFan, since the bytes removed are cheap ones). Not built into the tree; its place would be a +host with few cores, where background compiles compete with the +kernel's thread (not measured). + +**Variance between runs.** The same bundle runs the same command with +very different counters from one process to the next, through two +mechanisms that the guest's address randomisation decides per process +(with `randomize_va_space` 0 the counts repeat). The counts below are +from 2026-12-03 with 1024-entry data TLBs; the data-TLB ones are not +measured again at 16384 entries, where fewer pages share an index: + +- *Data-TLB conflict.* Two hot data pages with the same index in the + direct-mapped data TLB (for example perl's data page and its stack + page) evict each other, and compiled code hands every such access back + to `run`: 1-3 M hand-backs in a run against a few thousand (they equal + the conflict fills of one set). A hand-back costs about 900 cycles + (fitted). Such runs are 2-10 of 30 on the Perl kernels and cost +4% + to +47% (perl-k5: 3766 M cycles against a median of 2565 M); on node, + bun, npm, python, the C build and dpkg 12-30 of 30 runs have a set + with more than 50K conflict fills (node +22%, bun +9%); dpkg has + about 2.2 M hand-backs in every run, spread over many sets (capacity, + not one pair). +- *Block-table thrash.* A third hot block in a full 2-way block-table + set replaces a region member; the member is decoded again with a new + op number, so the region's checked entry fails on every branch into + it (1 M+ stops) while the block is interpreted and counts down again, + is compiled into another region, and is dropped again. A failed + checked entry costs about 570 cycles (fitted). 0-9 of 30 runs on the + Perl kernels (+1% to +13%); on node and bun 22-23 of 30, with 0.4-3.9 + M stale entries per run, an estimated 7-8% of their cycles (the C + build, python and claude 2.5-4.5%). + +Neither fix tried in compiled code or the block table is free on every +row (Rejected designs: victim way, taking back the tenured copy). ## Rejected designs @@ -577,22 +952,98 @@ retry without a new reason. | SMP: making the host's first call of a new module trap instead of the `cpu == 0` test | a wasm trap costs 33-43 µs in a CPU's thread, against one compare per entry | | SMP: compiling asynchronously (`WebAssembly.compile`, a helper worker) | a finished Module reaches a CPU's thread only through its event loop, at the 20 ms yields; V8's first-call compile stays on the CPU anyway | | SMP: returning to the kernel between `new Module`, `new Instance` and the first call | the first-call compile alone is most of it and cannot be split | -| Data TLBs of 4096 entries each (2026-11-20, A64, the profile disk of a64/performance.md, 6 boots) | `claude --help` first runs 0.0%, later -2.5%, npm -0.5% and +2.9%, bun cpu.js -0.8% and -1.0%: noise, for 4x the TLB memory | +| Data TLBs of 4096 entries each (2026-11-20, A64, the profile disk of a64/performance.md, 6 boots) | `claude --help` first runs 0.0%, later -2.5%, npm -0.5% and +2.9%, bun cpu.js -0.8% and -1.0%: noise, for 4x the TLB memory (with `markCode` scanning the write TLB; see the next row) | +| Data TLBs of 4096 or 16384 entries each while `markCode` scans the write TLB at a page's first marked chunk (2026-12-03 and 12-04, paired cycles) | 4096: dpkg -4.2 to -4.5%, python start -4.5%, perl-k3 -4.6%, but npm +4.1 to +4.3%, slower in every round, and the shell process loop +3.2%; 16384: dpkg up to -10%, npm +18%. The scan recurs after every nursery reset (Numbers, The data TLB); with the reverse map and the fill log the larger TLB is in the tree. Conflicts between two hot pages get rarer but stay (perl-k1 up to 6462 M cycles against a reference maximum of 4746 M at 4096) | +| A second (victim) way per data-TLB set, checked in compiled code (2026-12-03) | hand-backs on the Perl kernels fall from 100K-2M to about 2K, but compiled code is 7-15% slower in cycles on runs without any conflict (4 Perl kernels, 4 boots: sum +9.9%, lower in 0 of 4): the code added at every memory access. Not the module size, a phi on the hot path or the struct layout (each removed, still slow); with only the host side built it is not slower. The check out of line through the function table: +23.5%, 12% more instructions | +| A re-decoded region member takes back its identical tenured copy, O(1) (2026-12-03, 8 boots) | stale checked entries -30 to -60% on app rows, but the shell process loop +2.3% in 8 of 8 rounds (untraced), sum +0.4%; a member evicted again at once still ping-pongs (three hot blocks in one set). With the 4096-entry TLB: dpkg -5.0% (8 of 8), python start -5.4%, the shell loop +4.0%, npm +3.3% | +| 32-member regions with a 60 KiB emit buffer (2026-12-02, with chaining; 5 rounds Perl, 3 rounds apps, paired cycles) | transfers -4 to -23%, compiled bytes 1.3-3.5x: Perl sum +2.0%, apps +4.0% (python, the C build, dpkg, bun and node +4 to +14%, lower in 0 of 3). With growth at 4096 exits per KiB: transfers -25 to -47%, compiles doubled, Liftoff execution 4.6x, V8 background cycles 2.5-4x: +8.6% on both. With no growth: Perl +5.1% (perl-k1 transfers +40%: cross-page calls and returns only growth merges); no growth at 16 members: +0.4%. Fewer transfers bought with more compiled code lose (Numbers, Compiled volume) | +| The data-TLB lookup, or the first-entry check of a member, as a wasm function of the module (2026-12-03) | the lookup: perl-k1 1303 against 983 ms (one boot; V8 does not inline it into a function past its inlining budget). The entry check: 29 bytes per member less, but about 1.3% of main-thread time, the helper running mostly in Liftoff | +| The indirect-jump lookup moved to the top of the region's dispatch loop (2026-12-03) | cycles +3.0%, instructions +3.2%, lower in 0 of 5 rounds | +| A chained exit whose lookup miss calls the kernel instead of carrying the inline lookup, with a transfer cache in front (2026-12-03) | chain bytes per module -30%, instructions down, but app cycles up on the rows with 8-12% misses: python compile +4.2%, python start +2.7%, app sum +1.5% (lower in 0 of 5) | +| The entered member's position passed in `jl` instead of read through `cpu.jit.cur`; a branchier block-table lookup in the chained exit (2026-12-03, Perl, 5 rounds) | Perl sum +2.7% and +4.2% (perl-k6 +13%) | +| Registers as wasm parameters: a module exports a second function that takes the X registers, NZCV and pc as parameters, entered from a small loader function and tail-called across modules; registers stored only on return to `run`, hand-backs and faults (2026-12-03, 16 rows, 8 paired rounds, cycles) | with 16 registers passed: +4.3% (guest ASLR off) and +3.9% (on), higher in 8 of 8 rounds; with all 32 worse. Instructions +1.7%, icache misses +6.8%: every entry from `run` (4-12 per 1000 instructions on app rows) and every hand-back now moves the whole set, and each module carries a bigger prologue and epilogue | +| The same plus per-target links: static exits tail-call a slot of a funcref table (a miss stub until the host links it), the callee checks pc and validity; with one inline call per exit site, a leaner entry, and links for indirect branches (2026-12-03, same) | +14.8% / +10.6% (indirect branches too: +16.9%), higher in 8 of 8. Instructions only +3.2%; the rest is front end: icache misses +24%, 21.5% more bytes per member, on perl-k4 branch mispredicts +45% and iTLB walks +133% (many distinct indirect-call sites instead of one). Link misses 2.3-3.6 per 1000 instructions on app rows. A table holding funcrefs keeps freed modules alive until the host clears the slot (bun's first run +87% before that) | +| Register traffic by path: each member loads at first use, exits store only what the path wrote (2026-12-04, census only, nothing built) | the perfect bound is 1.8-6.7% of host instructions on the Perl kernels, because 42-88% of Perl runs hop to further members and a block touches about 10 slots; a cheap form (the entered member's set, then the union at the first hop) is 0.4-3.8% before its own 1-2%, so perl-k1, k2, k5 and k6 would not gain. An exact form needs fixed register states per member with fix-up code on every edge, or a store chain per exit site: more code per member (Numbers, Register traffic by path) | +| Indirect jumps inside a region through a pc hash into a `br_table` with a pc check, RET first against the return site of a call the region contains (2026-12-04, 10 paired rounds) | perl-k1, k2 and k4 -2 to -4%, every Perl row 1.4-2.3% fewer instructions, but python compile +2.9% (lower in 1 of 10) and python start +2.6% (2 of 10) at equal instructions (the host's jump table predicting worse than the compare chain is the suspect, unverified). Only a filter in front of the chain: no slower row, sum -0.2%. Most indirect hops leave the region anyway (hits 6-62%) | +| Regions formed from recorded edges: `run` records the block that follows each interpreted pass while a block counts towards the threshold, the region search follows those edges and takes already-compiled blocks as guests (2026-12-04, 16 rows, 6 paired rounds each, cycles) | transfers -27 to -32%, same-pc recompiles of later Perl runs from 47-165 to 0-17, host instructions -2.2%, but cycles +3.3% (guest ASLR off) and +4.2% (on), higher in 6 of 6, 10 of 16 rows slower in both (start-up, python, node and build rows +2 to +7%; perl-k4 +13% with ASLR on), module bytes +10 to +80%, V8 background cycles +38 to +46%. Recorded edges without guests: region shapes barely change, +0.7%, instructions +1.3% for the recording | +| 32 and 64 members again (2026-12-04, 4 rows, 3 rounds, with and without growth) | cycles +10.3% and +17.5%, instructions +8.1% and +13.6%, background cycles +221% and +374%; transfers -25% on perl-k4 but only -5 to -12% elsewhere, and at 64 members python compile and bun thrash the tenured ring (3000 or more ring frees per run). No growth at 16 members: +0.2% (instructions +1.3%, background -3.9%) | +| Module format v2 switched on (`KW_JIT_V2=1`; 2026-12-04, perl-k2 and perl-k4, 3 paired rounds) | cycles +20.4% and +34.4% (lower in 0 of 3), instructions +8.4% and +10.6%, L1 instruction-cache stalls +92% and +85%, iTLB and tag stalls +109% and +63%; chained transfers do not fall (25.7 to 26.0 and 36.9 to 42.0 per 1000 guest instructions) and 7.5 and 14.4 transfers between clusters come on top, each storing and reloading the registers (an estimated 170-230 host instructions more than the hop it replaces): every cluster function carries a full prologue and epilogue | +| Guest BL/RET as wasm `call`/`return` (2026-12-03, synthetic only) | saves about 41 cycles on a matched return, costs about 200 on an unmatched one at depth 8 (2200 at depth 64) | | No halving of the surviving way's progress in `jc.victim` (2026-11-20, same) | 4-23% slower: `claude --help` +4.2% / +8.2% (first / later runs), npm +5.9% / +23.2%, bun cpu.js +6.1% / +8.4%; compiles per later claude run 453 to 855 | ## Next steps Shared-core levers (A64's ranked list: a64/performance.md, Next levers). -Bigger regions: most exits that remain leave full or closed regions. On -7-Zip's LZMA (A64, 2026-11-23) 421 M of 629 M entries follow an exit -from a closed region: its match finder's regions close at 25-28 KiB with -7-12 members (about 3.5 KiB per member, much of it data-TLB checks and -NZCV packing on exit paths), so fewer bytes per member (shared exit -stubs) would let them grow; more members also need a smaller -`members`/`member_op` encoding (or a bigger `KW_CPU_SIZE`, an ABI -change). Exits into self-loop modules and back are the other large group -(176 M of those entries): self-loops join open regions as guests, but -not closed ones, and a self-loop module, closed itself, never grows -towards the region it returns to (a self-loop module that dissolves into -a home member once its exits are hot would). Cheaper `enter` and region -entries (a smaller register prologue). +The evidence is in Numbers (2026-12-02 to 12-04, one x64 host, V8). + +1. **Fewer host instructions on the hot path, with no code added** + (Numbers, Front-end bound). The host is front-end bound on this + code, a transfer is about 190 host instructions, and nothing that + bought fewer transfers with more code has paid. In progress, + unmeasured: + - forward member-to-member edges as structured `br` instead of the + `loop` + `br_table` hop (54-79 hops per 1000 guest instructions on + Perl; 0.4 against 0.8 ns in the synthetic model on an M4). First a + census of the hops that are static, single-target and forward in + member order: not worth building if under a third are; + - a shorter path through the one shared chained exit when the target + is compiled and unchanged since the last transfer from that exit: + an estimated 130 of the 190 instructions are protocol (validation, + growth counting, budget and bookkeeping, the `cur` stores, the + callee's dispatch), not register traffic. One exit per module + stays; one inline indirect call per exit site cost more front end + than it saved. + + After those, and only if at least half of the chained transfers go + to a target that is static and already compiled when the source + region is compiled: regions formed once without growth re-emission, + whose static exits to a region in the same module are a direct + `return_call` carrying only live registers. Module format v2 as it + stands is not that (Rejected designs). Also open: growth that does + not free and recompile the region (most later-run recompiles with + guest ASLR off), code that survives a move to another address (most + of them with ASLR on), and branch hints from the emitter (fewer + bytes, cycles not decided). + + Known constraints: one wasm function per member with registers + through memory cost the hop-heavy harness guests 23-30%; + `call_indirect` to the next block gained nothing while each hop wrote + back and reloaded registers; a self-loop as a region member was much + slower, which is what keeps compare trees and string loops split; + register traffic by path is bounded at 1.8-6.7% of instructions and + the lookup's instructions are free. +2. **Module code placement in V8 on Apple silicon** (Numbers, Engines + and hosts): a possible 2.5x per transfer there if TurboFan code can + be moved off the page boundary; needs a device timing of real + modules first. +3. **A transfer cache** in the chained exit (measured, not in the tree): + 1024 direct-mapped entries keyed by target pc, each with its own + epoch (an entry is valid while the CPU's generation has not moved + since its fill; every event that can change what it names skips a + generation), a miss falling back to the inline lookup. It hits on + 86-100% of chained transfers. Pooled over 10-18 rounds, cycles: the + Perl kernels -2.4% to -5.5% (k4 0.0%), dpkg -5.9%, node -3.9%, python + start -2.6%, bun -2.1%, but the three rows with few transfers lean + slower at equal instructions (the shell loop +2.2%, claude +1.3%, npm + +0.7%), inside what a build shows against itself; unexplained (not + the nursery it takes room from). A cache per exit site hits 69-100%, + one per module 24-62%; one global epoch would flush too often on npm + (10 invalidating events per 1000 transfers). One run of it under Node + took 3x as long in every Perl kernel, once, unexplained. +4. **The two variance mechanisms** (Numbers, Variance between runs) are + a steady cost on node and bun (stale checked entries an estimated + 7-8% of cycles) and were on dpkg at 1024 data-TLB entries (hand-backs + an estimated 14%; not measured again). Open: a block-table + replacement rule that does not push out a region member, or does not + make the returning block count down from the start. +5. **Growth without recompiling the region** is bounded by the cycles + grows spend recompiling (Numbers, Compiled volume) minus the + transfers that not growing leaves; a higher grow threshold on top of + the transfer cache gained nothing. + +Not worth building on their bounds: self-loop modules linking to their +neighbours (at most 2% on the Perl kernels; 25% of `perl-start`'s +transfers, which are 3% of its cycles). Fewer bytes per member do not +let regions grow usefully; they cut background compile time (Numbers, +Module bytes). A smaller register prologue is bounded too (Numbers, +Register traffic by path); a cheaper `enter` from `run` remains. diff --git a/.agents/status.md b/.agents/status.md index ba95bf7..a7ae25c 100644 --- a/.agents/status.md +++ b/.agents/status.md @@ -161,9 +161,16 @@ Open: each 64K ops cost 1 MiB of guest RAM per CPU) only if long sessions show the same; per-CPU code caches on SMP; a cheaper decoder only if decode stays above about 8% of npm's - time. Apk - and shell: bigger regions (most remaining exits leave full ones), exits - into self-loop modules, `run`'s entry path and region prologues, NZCV + time. Long-running compiled code (Perl, node and bun loops): the + host is front-end bound on it, so only changes that remove host + instructions without adding code pay (register traffic by path, + hashed indirect jumps, regions formed once, bigger regions, + registers as parameters, per-target links and module format v2 are + all measured and rejected): structured forward branches for hops + inside a region and a shorter path through the chained exit (in + progress, unmeasured), then the two causes of run-to-run variance + (kwemu.md, Next steps). Apk + and shell: `run`'s entry path and region prologues, NZCV materialisation. Not worth revisiting: the compile threshold and heat policy, the block-table size, the interpreter's dispatch. 2. **A64 features**: ARMv8.0-A with AES, PMULL, SHA1, SHA2, CRC32, LSE,