wado-lang/wado / .claude/skills/wado-performance
wado-performance
VerifiedAnalyze and improve the runtime speed of a Wado program's compiled guest Wasm — profile hot functions, read the generated WIR for allocations and copies, reason about the WasmGC cost model, and A/B-measure a fix. Use for any guest-side speed question, whatever the program does. For host-side native compiler profiling see profiling-wado-compiler; for wrong code out of an optimizer pass see optimizer-debug.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 34.2 KB |
| dead-ends.md | markdown | 45.3 KB |
| scripts/analyze_guest_profile.tsexecutable | script | 3.1 KB |
| trade-offs.md | markdown | 6.3 KB |
dead-ends.md
View on GitHub# Performance Dead Ends
Optimizations that were measured and did **not** pay off. Read before starting
performance work; add an entry whenever one lands here.
An entry lands here when a change clears none of the three bars in the skill's
§5: the benchmark did not move, the WIR A/B diff shows no fewer instructions, and
it does not even win the qualitative-tie case on wasm size. A change that clears
one of them is kept, and belongs in its commit instead. Note what is _not_ a bar:
code quantity. Neither wasm bytes nor dump lines correlate with speed, so only a
WIR diff establishes "fewer instructions". Profile shares only rank
candidates — two profiles have different sample counts, so a shifted percentage
proves nothing.
```sh
wado run -O2 --profile guest,prof.json,1 benchmark/json_catalog/json_catalog.wado
wado dump -O2 benchmark/json_catalog/json_catalog.wado # before/after: diff the hot function
for i in 1 2 3; do mise run json-catalog; done # before and after
```
## `array.new_fixed` of zeros in place of a short `array.new_default` (2026-10-28)
wasmtime zero-fills every `array.new_default` through the `memory_fill`
libcall, whatever the length. fts allocates a 16-byte `String` per conversion,
and `wasmtime_builtin_memory_fill` plus `memset` took 3.6% of a perf profile.
Emitting a constant-length packed `array.new_default` as `array.new_fixed` of
that many zeros skips the libcall.
It measured slower, three alternating pairs of `mise run benchmark-fts`:
| `array.new_default` 16 bytes | fts |
| ----------------------------- | --------- |
| as is (libcall) | baseline |
| `array.new_fixed` of 16 zeros | **−3.2%** |
wasmtime initializes `array.new_fixed` one element at a time, each through its
own element address. Sixteen byte stores cost more than one libcall.
Generalizes: the fill libcall is cheaper than it reads in a profile. Price the
replacement in instructions per element before trading it for stores.
## Keeping pool memory resident for `wado serve` (wasmtime 50, 2026-10-22)
`create_serve_engine` sets no `PoolingAllocationConfig::linear_memory_keep_resident`,
so it is 0. A freed slot is then decommitted at once, and
`Engine::release_idle_pool_memory` (new in wasmtime 50) finds nothing to
release: it measured 0 bytes. A GC heap sits in a memory slot of the pool, so
raising the budget keeps the GC heap of each recycled worker resident. The idea
was to keep it resident for speed and release it once the server goes idle.
It is a pure loss. One worker, `GET /user`, `oha -z 10s -c 200`, release after
10 s with no request:
| keep_resident | req/s | RSS after load | RSS after idle | Released |
| ------------- | ------ | -------------- | -------------- | --------- |
| 0 | 33,388 | 246 MiB | 248 MiB | 0 MiB |
| 16 MiB | 24,706 | 798 MiB | 258 MiB | 544 MiB |
| 64 MiB | 14,975 | 2,479 MiB | 307 MiB | 2,176 MiB |
| 256 MiB | 2,239 | 9,196 MiB | 496 MiB | 8,704 MiB |
A worker recycles every 200 requests (`DEFAULT_RECYCLE_REQUESTS`), so each slot
it frees is reset again and again. The released bytes show warm slots piling up
to about 34 times the budget. The release itself works, but it only undoes a
budget that should stay at 0. The cause of the slowdown was not profiled.
```sh
OHA_CORE_COUNT=2 mise run -C benchmark http-routing-wado
```
## Fusing `write_plain_key`'s appends across the key name (2026-10-03)
`nir/string_push` opens a new group at a run-time-length piece, because the
source may be the buffer. So `,"` + `name` + `":` pays two capacity checks. The
fusion was made alias-safe in one group: read the source's length up front and
add the prefix's length when `ref_eq(source, buffer)`. Each key then paid one
check. json-twitter ser −3.1%, json-catalog ser +1.6%, four rounds, ranges
overlapping.
The check was never the cost. Each key write is about 120 machine instructions
and a `wasmtime_builtin_memory_copy` libcall, for the `array.copy` whose length
is only known at run time. The 2-byte constant copies around it compile to a
load and a store. What would pay is a constant name at the site, which a
spliced or specialized key writer gets.
Two costs came with it. The `ref_eq` blocks the half-price fold that splices a
small helper on a constant string argument, so `dynamic` in
`opt_string_append_fuse.wado` stays a call. And `const_object_globalization`
refuses a literal handed to any instruction taking a struct, `ref_eq`
included, so every key literal became an allocation per call: twitter ser
−15.9% until its gate read `#[retain]` instead of the argument's type.
## Two inliner tweaks for the JSON writer's integer path (2026-10-03)
`serialize_i64` is a forwarder to `write_json_i64`. Bottom-up, it receives
that callee first and grows to 42 lines, so a site created in a later round
keeps the call and the `JsonSerializer` it builds for the receiver. Two changes
to `classify_callee` in `optimize/inline.rs`, against the same release base,
alternating rounds:
- **Holding a forwarder** (`net_cost == 0`) so it receives no splices. It
removed the `serialize_i64` hop, but json-twitter ser lost 3.7% (six rounds,
ranges apart). The hold also covered `push_str`, which splices into every
site, so `internal_push_string` lost its sole caller and stayed a call at 17
sites writing `"null"`. A sole callee is spliced only because its caller is
one function, and a hold breaks that.
- **Counting safepoints per path** instead of `has_safepoint`'s any-call rule:
an allocation always adds one, a single call per path does not, since the call
it replaces already was one. json-catalog de +3.5%, but catalog ser −2.4%,
canada de −2.0% and twitter ser −1.3%; four rounds. `Formatter::pad` and
`prepare_int_write` doubled in size.
## SWAR blocks in the JSON reader's scans (2026-10-03)
Testing four bytes packed into a `u32` with bit tricks pays in the writer's
escape scan: json-twitter ser +18.5%. There each byte took three compares,
which cost twice the bounds-checked read. Two readers took the same shape and
did not pay:
- **`scan_string_run`**, a block of four plain-ASCII bytes per test, retried at
the top of each iteration so it resumes after a multibyte character.
json-twitter de −4%, json-catalog de −1.5%, three rotating rounds. The
plain-byte test there is already one subtract and compare. twitter's
Japanese text fails every block, and citm's strings are short, so the
partial block is wasted on nearly every token.
- **`peek_after_whitespace_run`**, settling a block whose four bytes are all
`0x20` with one compare. json-catalog de between +1.7% and +5%, ranges
overlapping across sessions; json-twitter de flat. Indentation is spaces on
citm, but that is a property of the corpus, not of JSON, and the evidence did
not clear the noise.
Generalizes: SWAR pays where the per-byte predicate is the cost, not the read.
Read the loop's machine code before choosing: `wasmtime compile` with the
features `wado` enables, then `wasmtime objdump --filter <name>`.
## Rejecting control bytes in `core:json` without paying per byte (2026-09-29)
RFC 8259 forbids an unescaped byte below 0x20 in a string, so
`scan_string_run` has to stop on one. Every plain byte then pays for a third
test. Two shapes, each against the same release base, four alternating
rounds:
| ASCII branch of `scan_string_run` | json-twitter de |
| -------------------------------------------------------------------- | --------------- |
| `b < 0x80`, then `json_plain_byte(b as u8)` | -4.8% |
| `((b - 0x20) as u32) < 0x60`, then `"` or `\`; controls after (kept) | -1.5% |
`json_plain_byte` compiles to a mask, three compares and two `and`s per byte,
where the old test was two compares. The range shape adds one subtract to a
plain byte and one compare to a multibyte lead. Twelve back-to-back
twitter-only runs put it about 1% behind, losing all six pairs. twitter.json
holds 271K ASCII string bytes, 32K multibyte leads and 1.2K escapes, so the
per-byte test is the whole cost. json-catalog and json-canada deserialize
measured flat.
Separately, `scan_number_into` lost 4.4% on json-canada deserialize when the
integer digit loop moved into an `else` arm and an error builder took `&self`,
keeping the input reference live across the function. Rejecting a leading zero
before the unchanged loop, with a free function building the error, measured
flat. Both changes went into one build, so this does not say which of them
cost what.
Generalizes: a new check in a scan loop goes where the rare bytes branch, not
into the test every byte takes. Read the loop's WAT for what a plain byte
executes before and after.
## Breaking `adler32`'s dependency chain (2026-09-29)
`adler32` in `core:zlib` adds each byte into `s1` and then `s1` into `s2`, so
both sums form a serial chain. Rewriting the 16-byte block as
`s2 += 16 * s1 + Σ (16 - k) * b_k` and `s1 += Σ b_k`, over four independent
partial sums, shortens that chain. It measured **11% slower** over twitter.json
(631 KB), four alternating rounds with the order swapped, on a release compiler.
The arms' ranges do not overlap:
| block body | best | ms/iter range |
| ----------------- | --------- | ------------- |
| serial (today) | 1.82 GB/s | 0.347-0.356 |
| four partial sums | 1.62 GB/s | 0.390-0.399 |
The chain was never the bound. At 1.82 GB/s the loop already costs about as
much as its 16 gets, and the weighted sums only add multiplies. SIMD does not
apply either: `v128_load` wants a linear-memory address, and a `ByteList` is a
GC `Array<u8>`.
Generalizes: the same lesson as reading digits eight at a time. A loop that
reads one byte and accumulates it is at its floor on a GC array. Measure the
serial version in isolation before trying to beat it.
## Reading digits eight at a time in `scan_number_into` (2026-09-12)
The whitespace scan pays by reading several bytes per bounds check, so the
digit loops look like the same shape. They are not. Packing eight bytes into a
`u64`, testing them with the SWAR digit mask and folding them with three
multiplies measured **11% slower** on json-canada deserialize than the
byte-at-a-time loop; consuming the partial chunk's digits out of the bytes
already read, so no chunk is ever wasted, still lost by **4%**, 4 of 5 rounds.
Isolated on a 2.16 MB buffer of 17-digit numbers, both loops in one session:
| digit loop | throughput |
| ---------------------- | ---------- |
| byte at a time | 953 MB/s |
| eight per bounds check | 709 MB/s |
953 MB/s is about 1 ns per byte, a handful of cycles for a get, a compare, a
branch and a multiply-add. Whatever the loop still pays per get, it is already
small enough that the packing shifts cost more than batching saves. The "~20
machine instructions" figure for `array.get` is a straight-line one, and a tight
scan loop over a single array does not pay anything like it.
Batching the whitespace scan still pays, and the two results agree. What
differs is the body. A loop that only tests a byte has the get as its whole
cost, so amortising the get is the win. A loop that also accumulates the byte is
near its floor already, and widening it only adds the pack.
Measure the loop you mean to beat, in isolation, before widening it. A per-get
instruction count is not enough to reason from.
## Sharing `core:json`'s three digit-accumulating loops (2026-09-12)
Folding them into one `fold_digits` helper costs json-canada deserialize 1.5%:
9.738 ms/iter with the helper against 9.594 without, four alternating rounds.
The body is a loop, so the inliner declines the call and every digit pays one.
It first measured flat because it was measured on json-catalog, whose numbers
are integers where the change was to the float scanner.
Generalizes: a loop body is not a candidate for extraction, whatever the
duplication looks like. Run a scanner change on the corpus whose values reach
that scanner.
## Making `json_ws_end` cost nothing on minified input (2026-09-12)
The predicate tests `b != b' '` first, settling a skipped byte in one compare
and a stopping byte in two. citm_catalog is 71% whitespace and wins; canada.json
is minified, 24 whitespace bytes in 2.25 MB, and only pays. Five alternating
rounds against `origin/main`'s `core:json`, one dev compiler:
| `core:json` | catalog de | canada de |
| ------------------------------------- | ---------- | --------- |
| main's `b > b' ' \|\| !is_json_ws(b)` | +12.1% | **-1.0%** |
| space-first (today) | **+19.3%** | -2.8% |
The ordering is worth 7.2 points on the pretty corpus and 1.8 on the minified
one. Four attempts to keep both failed:
- **Reorder to `b > b' ' || (b != b' ' && !is_json_ws(b))`.** Costs what it
recovers: catalog de 3.9 → 4.2 ms.
- **Two predicates, one per call site**, structural-first at the scan's entry
and space-first inside the run. Neutral on both corpora.
- **Inline the body at all six sites.** Also neutral, which rules the call out:
the ordering is the cost, not the call.
- **Branchless**: `((b' ' - b) >> 31 | (ws_set >> b & 1) ^ 1) != 0`, correct for
all 256 bytes, the sign term rescuing the shift aliasing `assert b < 64`
guards. Ten canada rounds put it at main −4.0% against head's −4.2%, beating
head 6 of 10. The branch it removes is well-predicted on both corpora, so
eight unconditional ops lose to two behind a taken guess.
One compare cannot decide it either: a compare against a constant splits the
byte range into two intervals, and `{0x09, 0x0A, 0x0D, 0x20}` is neither an
interval nor the complement of one. SIMD does not apply, `v128_load` wanting a
linear-memory address where a `String` is a GC `Array<u8>`. All four arms sit
within two points of each other, and main's own canada median moved
9.376 → 9.056 between clean sessions, so this size of effect needs a better rig.
Generalizes: a byte-frequency assumption belongs to the corpus, not the format.
Check a scan predicate on a minified corpus and an indented one; they move in
opposite directions here.
## Reading more than four bytes per bounds check (2026-09-12)
citm_catalog's whitespace runs average 24 bytes, well over the 16-byte floor the
entry on short runs sets for batching, so widening `peek_after_whitespace_run`'s
block past four should pay. It does not. json-catalog deserialize, three
alternating rounds against the four-wide arm at 4.21 ms/iter:
| block | ms/iter |
| --------------------- | ---------------- |
| 4 (today) | **4.21** |
| 6 | 4.31, 4.36, 4.31 |
| 8 | 4.40, 4.52, 4.50 |
| 8, then 4, then 1 | 4.89, 4.60, 4.62 |
| 16, then 8, 4, then 1 | 4.88, 4.86, 4.89 |
Monotonic in the width, on the input whose runs are longest. wasmtime shares the
base, length and null check across about four gets and no further, so every get
past that is a lone one the block issues whether the run needs it or not.
Generalizes: four is the number, not a starting point. The run length decides
whether to batch at all; it does not buy a wider block.
## Two digits at a time in `write_decimal_digits` (2026-09-12)
The twin of the `fpfmt` loop in the entry on `write_digits_at`, coming out the
same way on a corpus of 8.8-digit integers. json-catalog serialize, three
alternating rounds against 0.822 ms/iter:
- **`t / 100`, then the pair's two digits off the remainder**: 0.956, 0.956,
1.045. Same store count, half the divisions and half the loop trips.
- **`array.copy` of the pair out of a 200-byte `DIGIT_PAIRS` global**, which
trades the two `array.set`s for one copy: 0.888, 0.978, 0.999, 1.002.
A two-byte `array.copy` does beat two `array.set`s elsewhere: retiring
`string_push`'s per-byte expansion, so a constant key's `":` stays one copy, is
worth 3-4% on this row. The difference is the index. A copy from a constant
offset in a global is not a copy whose source offset was just computed.
Generalizes: the digit loop is store-bound, and no digit-generation scheme has
yet removed a store from it. Three attempts now, two functions, two corpora.
## Splitting a hot leaf so its fast path fits the inline budget (2026-09-12)
Two tries on the theory that a call per token is worth removing, both flat:
- **`Formatter::prepare_int_write`.** Holding the padded half in its own
function leaves the fast half about ten instructions — a sign and a
reservation. json-catalog serialize measured 0.823 against 0.817 over four
alternating rounds, its best round behind the baseline's.
- **`JsonDeserializer::peek_after_whitespace`.** Reduced to a length check, one
`array.get` and `b > b' '`, the rest behind `peek_after_whitespace_cold`.
`wado dump -O2` shows the split worked and the 21 call sites still call: a
tail call is priced like any other operand, so the fast path is over budget
with it. `#[inline(never)]` on the cold half changed nothing; `#[inline]` on
the fast half measured 4.26–4.44 against 4.26.
Generalizes: shrinking a leaf is not the same as getting it inlined. The dump
tells the two apart, so read it before spending a measurement. The entry on
`HighlightVisitor::classify` says the same thing from the other end.
## A shared helper that hands back a value most callers discard (2026-09-10)
`core:json`'s three container accesses open an entry the same way, and
`entry_head` covers five of the six call sites at no cost: the helper call, its
`Result`, the `?` at the consumer and a `cold_path` arm inside it all fold away.
The optimizer is not what prices this shape.
What prices it is the sixth site. `next_field` reads the byte the whitespace
scan stopped on, so covering it too means returning that byte, and then every
seq element and map key re-reads one after the comma to produce a byte neither
wants. That costs 2.0% on json-catalog deserialize over six alternating rounds,
losing every one with no range overlap, where covering five measures flat.
Generalizes: before widening a helper's return type to serve one caller, count
what the other callers then compute and drop. A duplicated preamble at the one
site that consumes more than the others is cheaper than a return value the rest
pay for.
## Batching `array.get` where the runs are short (2026-09-10)
Reading four bytes per bounds check is worth 12.6% on json-catalog's
whitespace scan (`peek_after_whitespace_run`), whose runs average 16 bytes. The
same rewrite loses on two neighbouring scans, and the reason is the run length:
- **`scan_string_run`**, four plain-ASCII bytes per block: json-catalog de
**-2%** on the pretty input and **-4.5%** on the minified one, three
alternating rounds. citm's string tokens are 6-16 bytes, so the block that
straddles the closing `"` is wasted on nearly every string; a 6-byte key
goes from 7 reads to 11. A refinement that walks the four bytes already
read instead of re-reading them recovered half the loss and was still
behind.
- **`scan_number_into`**, four digits per block: json-canada de **-9.6%**
with both digit runs batched, **-5%** with only the fraction run (15
digits on this input) batched. The integer run is 1-2 digits on every JSON
corpus there is, so its block is pure waste. Even a 15-digit fraction did
not pay for growing the function.
Generalizes: the batching in the skill's §3 is priced by the run it covers
against the one partial block it always wastes. Below roughly 16 bytes per
run it is a loss, and the run length is a property of the _input_, so count
the runs over the corpus before rewriting the loop.
## Where json-canada's time actually is (2026-09-04)
Not a dead end — the map the entries below were measured against, since every
share here was established by ablation rather than read off a profile. Each row
is the whole benchmark minus one thing, dev build (which matches release on this
benchmark to under 2%, so the stdlib can be edited and re-run in 8 seconds with
no rebuild).
Serialize, 7.65 ms/iter:
| remove | ms/iter | cost |
| ----------------------------------------- | ------- | ---- |
| — (baseline) | 7.65 | — |
| every division in the digit loop | 7.44 | 0.24 |
| the decimal-point `array_copy` shift | 7.28 | 0.37 |
| `short()`, replaced by a constant | 5.98 | 1.68 |
| the digit loop, replaced by `array_fill` | 5.77 | 1.90 |
| all float formatting (19-byte `push_str`) | 3.09 | 4.56 |
| the float bytes entirely (traversal only) | 1.58 | 6.07 |
Deserialize, 10.96 ms/iter: `scanned_to_f64` → 0.0 costs 1.55 ms, and deleting
`digits = digits * 10 + d` on top of that is free — the scan is bound by the
bounds-checked `array.get` and its branches, not the arithmetic.
Two things follow. The serde traversal alone (1.58 ms, no float bytes written)
is already 65% of serde_json's entire 2.43 ms serialize, so the structural cost
is the standing gap, not the codec. And `short()` at 1.68 ms is ~15 ns per
conversion, which is ryu-class — there is no algorithmic slack left in it.
## Four ways to keep `short` small and still drop its `uscale` calls (2026-09-04)
Inlining `uscale` into `short` buys json-canada ser +2.4% and costs json-twitter
ser 2.3% — placement, on a hot path that never calls `short` (see the entry
below). So: get the win without growing `short`. Four shapes, all worse, and the
reason is the same each time.
| arm | `short` | canada ser | twitter ser |
| ------------------------------------------------ | ------- | ---------- | ----------- |
| base | 87 | 7.59 | 0.821 |
| inline `uscale` into `short` (what landed) | 122 | **7.39** | 0.837 |
| share the mask, keep the three calls | 89 | 7.70 | 0.835 |
| one call returning all three scalings | 86 | 7.81 | **0.814** |
| one call returning the two bounds | 88 | 7.88 | **0.818** |
| pin the rare arm out of line (shrinks the above) | 113 | 7.46 | 0.833 |
Every arm but the first makes fewer calls and fewer mask computations than base,
and every one of them is **slower than base**. So "fewer instructions" does not
predict the outcome here at all. The arm that wins is the only one with _no call
left on the hot path_. What it saves is the call boundary itself — argument
setup, the multivalue return, the registers that cannot stay live across it — and
not the two mask computations, which are worth nothing. The three-scaling arm
also does a third wide multiply `short` does not need: a shortest representation
at full width has `dmin == dmax`, so canada only ever needs two.
Two traps worth naming. A call is charged the ABI edge unless the callee carries
a loop this pass will splice, and what the threshold judges is that price **less
the call site it replaces**, so a helper that reads as "a mask and three calls"
comes in under budget and is pulled into `short` together with all three bodies.
`#[inline(never)]` is the only way to hold such a helper out of line, and
reaching for it there is not a claim against the cost model.
A size-matched control does not falsify a placement story either. Padding `short`
with a dead branch grew the _wasm_ by 196 bytes against the real change's 205 and
left twitter at exactly 0.820 all three rounds, because a compare-and-jump is
nothing in machine code next to three inlined `uscale` bodies. Wasm bytes are not
the unit the effect is measured in.
Generalizes: canada's gain and twitter's loss are one phenomenon, not two. The
only lever that moves either is whether `short` carries the scaling inline, and
it moves them in opposite directions.
## Three ways to get `uscale` inlined (2026-09-04)
`uscale` grosses 21 against the -O2 budget of 16, and its four parameters make
the call site it replaces worth 6, so `net_cost` puts it at 15 and `short`
carries all three scalings inline. That buys json-canada ser 7%, the whole of the
-O3 gain on this row. Raising the threshold to 22 buys the same 7%, along with
the global bloat the 2026-08-27 entry rules out.
The three arms below were measured while `short` still paid three calls per
conversion, each recomputing the same `(1 << (s & 63)) - 1`. Each fails for a
reason that does not turn on how `uscale` is admitted:
- **`builtin::cold_path()` in `uscale`'s exactness arm.** Prices the function by
its fast path and gets it inlined everywhere. json-canada ser +3.4%, de +2.7%
— and **fts -4.5%**, three rounds, non-overlapping. `fixed_width_for_prec`
scales once, so it gains nothing from the mask and only pays the growth; it is
the same size-sensitive function the skill's `cold_outline` note names.
- **Merging `check_special`'s two zero tests** into `bits << 1 == 0` (cost 18 →
under budget). json-canada de **-2.4%** on its own and nothing measurable on
ser, on a function the de path never calls. Pure placement.
- **Returning the two bracketing scalings together** (`uscale_bounds`), to shrink
`short` from three calls to two rather than grow it. The threshold admits it on
its own price, and the growth budget, where a single-site candidate costs
nothing, never gets to veto it. `short` grew from 87 to 113 dump lines
regardless.
Generalizes: at this scale module layout outweighs the instruction saving, in
both directions. Growing `short` moved json-twitter serialize 2–5% on a hot path
that is byte-identical bar block-label numbering. So hash the wasm of every row
first and only measure the ones that differ. Three of the five float-touching
benchmarks came out byte-identical, which retires their readings outright and is
the only reason the surviving numbers mean anything.
## Two digits at a time in `write_digits_at` (2026-09-04)
`write_decimal` is 27.7% self on json-canada, whose shortest-round-trip
mantissas are 17 digits: `v % 10` / `v / 10` per digit is a 17-long serial
chain on `v`, which reads as latency-bound. Stepping `v / 100` and peeling the
pair's two digits off `r` halves the chain and moves those two divisions off it.
**5% slower**, three alternating pairs, ser 282.5/286.9/288.3 → 261.6/272.7/271.7
MB/s. The chain was never the cost. Two ablations on the same loop say so:
| `write_digits_at` variant | ser ms/iter |
| ------------------------------------------ | ----------- |
| today | 7.65 |
| `v & 7` / `v >> 1` (every division gone) | 7.44 |
| `array_fill` (whole loop gone, same bytes) | 5.77 |
Deleting **every** division buys 0.24 ms; deleting the per-byte `array.set`
loop buys 1.9 ms. The loop is store-bound at ~2.8 cycles/byte, and a bulk fill
of the same span is near-free because it lowers to a vectorized memset. So two
digits per step only added a third multiply to a loop the multiplies were never
pacing.
Generalizes: on a GC-array-backed `String` the digit loop's cost is the one
`array.set` per byte, which no digit-generation scheme removes. Ablate the
arithmetic before optimizing it. This is the same finding as the 2026-08-16 entry
below, reached from the opposite direction — a 17-digit chain rather than a short
one — so the digit count is not what makes it come out differently.
## i64 divisions in integer formatting (2026-08-16)
Decimal formatting cost three i64 divisions per digit: `count_digits_i64` ran
`t / 10` per digit just to count, `write_decimal_digits` ran both `% 10` and
`/ 10`. Cut to one (binary search over powers of ten; remainder recovered as
`temp - q * 10`).
json-catalog ser, best of 3: **1.532 ms → 1.530 ms**. Within noise — the spread
inside each group (before: 1.532–1.601) exceeded the difference between them.
Cranelift strength-reduces division by a constant into multiply-high + shift,
so no division was ever issued. The ~28% in these frames is the per-byte
`array.set` into the GC buffer and the surrounding buffer management
(`String::grow`, `push_str`), not arithmetic.
Reverted; the digit-boundary tests were kept. Generalizes to any "replace a
constant divide/modulo" idea in guest code.
## Sharing list elements instead of deep-copying them (2026-08-20)
Lifting `value_copy_demote`'s variant-deep-copy gate is a no-op: the candidate
set grows from 26 helpers to 44 and demotes the same 3, for byte-identical Wasm
(`WADO_TRACE=demote`). It retargets only a `let x = $value_copy$T(arg)` binding,
and gale's `List<Element>` copies — 9.6% of the profile — are struct fields
(`SllConfig { ..*c, pos }`).
Extending the pass to expression position is the obvious next step and the wrong
one. Forcing every element clone shallow is the upper bound of any sharing
scheme:
| | `copying` | `null` (no GC) |
| ------------------- | --------- | -------------- |
| deep copy (default) | 171 KB/s | 208 KB/s |
| shared elements | 60 KB/s | 260 KB/s |
A deep-copied element dies in the nursery and costs a copying collector nothing;
a shared one is live from everything that borrowed it. GC goes from 18% of the
run to 77%.
Generalizes: price a "share instead of copy" idea against the live set it
creates, not the allocations it removes.
The motivating copy is gone as of 2026-08-25: `SllConfig.elements` is a
`&List<Element>`, which shares a list that is already a root rather than its
elements, and gale-gen gained 32% (`package-gale/perf.md`).
## Making an expression-position labeled block a break target (2026-08-20)
`Analyzer::walk_expr` pushes no exit entry for a value-producing labeled block,
so every `break` it holds resolves to "every local live". Pushing one is more
precise and does unlock moves — `build_sll_node`'s loop binding among them.
A precise live set also admits place moves the coarse one refused, and a place
move marks its root moved, retiring every immutable share of that root:
`Rebuild::rebuild` trades three shares of a member tuple for one element move.
gale-gen, best of four alternating pairs, **182.6 KB/s without it vs 168.0**.
Pricing the two elisions against each other needs the share analysis keyed on
liveness, a standing item in
[WEP: Ownership Analysis](../../../docs/wep-2026-05-21-resource-ownership.md).
## Raising the `-O2` inline budget (2026-08-27)
The 13-instruction budget leaves 313 functions out of line in gale-gen, and
raising it to 20 pulls them in: gale-gen 98.9 → 95.9 ms/iter, cbor-twitter
serialize +7.8%, syntax-highlight +3.7%, sqlite-parse +3.4%, sieve +2.2%.
It is still a loss. json-twitter **serialize drops 17%** (638 → 530 MB/s),
mandelbrot goes slightly backwards, and every program pays for it in bytes:
`-Os` output grows 41% on gale-gen (1.29 → 1.81 MB), 43% on json-twitter, 16%
on syntax-highlight.
So 20 is not a better setting — a serializer's hot loop is exactly what growing
it that far damages. What the sweep says instead is that the threshold is doing
two jobs, admitting a callee and pricing a loop body, and a gale-gen-shaped win
needs the second priced apart from the first. 16 did land later, on the same
rows; the budget this entry measures against is that, not the 13 above.
## Splicing a callee's only site in a loop at any price (2026-11-13)
A callee with one site moves rather than being copied, and in a loop the call
it replaces runs every iteration. Exempting such a callee from the payback
bound, when it reaches no safepoint, spliced zlib's `Matcher::longest_match`
(price 132, one call per input position).
It also spliced the JSON writer's helpers into the derived serializers:
`write_plain_key` (80–95) once per field, `write_decimal`,
`String::utf8_sequence_len` and `peek_after_whitespace_run`. Release A/B,
three alternating rounds against the same compiler without the rule:
| Row | Change |
| ---------------------------- | --------- |
| zlib compress | +1.8% |
| json-catalog ser (no-pgo) | **−9.0%** |
| every other json-catalog row | noise |
So the bound guards more than code size: a serializer's loop body grown by a
helper per field loses more than the calls it saves. A sole site needs a price
for what it adds to the loop it lands in, not a pass from the bound.
## A jump table in place of a compare cascade (gale, 2026-07/08)
Two independent rewrites, both reverted. `_kind_set_*` — a
`k matches { TK_… | … }` over the large SQLite keyword set, ~3% of the profile —
already lowers to a Wasm `br_table`, and its self-time was **unchanged** from the
compare cascade it replaced. `classify_keyword`'s per-length first-char dispatch,
rewritten from an `eq_ignore_ascii_case` else-if chain to a fold-once
`to_ascii_lowercase()` feeding a `match`, measured a consistent slight **loss**
(sqlite-parse 3.309 → 3.357 ms/iter, best of 3).
Cranelift lowers a short compare chain competitively and the chain predicts well,
while the jump table adds an indirect branch. Both frames were
call-frequency-bound, not dispatch-bound.
Generalizes: a hot `match`-vs-cascade frame is telling you how often it is
called. Cut the calls.
Both cascades here were short and already early-exit. A run of independent `if`s
that tests every key whatever matched is not that shape, and turning one into a
`br_table` gained 10.7% on cbor-twitter deserialize (`nir/if_chain_to_match`).
## Index loops in place of `for x of &List<i32>` (gale, 2026-07)
Iterating by reference boxes every element (WasmGC has no interior references),
so rewriting `follow_yields`'s membership scan and `classify`'s `rule_stack` scan
as index loops removes every box. It measured **within noise** — one of three
paired rounds.
The boxes die before the next collection, so the copying collector never traces
them. Same shape as a compiler pass that removed thousands of per-token
`Box<i32>` allocations for −0.7 ms/iter under `--collector null` and nothing at
all under `copying`.
Generalizes: an allocation you can prove short-lived is not a cost. Price a
de-allocation idea against the live set, not the allocation count.
## Pre-sizing a `String` past its one growth (gale, 2026-07)
`highlight_html`'s output grows once past its `source.len() * 5` reserve (HTML is
~6× source for keyword-dense SQL), and `String::grow` was 9% of the **dev**
profile. Bumping the reserve to `* 7` removes the growth; a release A/B
(best of 5) was **identical**, 3.90 vs 3.91 MB/s.
`String::grow` is allocation and zero-fill, which the dev host inflates ~4–5×;
its release share is ~1–2%, under the benchmark's noise. Contrast the CST column
pre-size, worth a clear ~6% because `sqlite_parse` is build-only and the arrays
are the work.
Generalizes: this is the dev-vs-release rule with a number on it. A grow-removal
sized from a dev profile is sized wrong.
## Working around `array.copy` (2026-08-28)
`array.copy` has a fast path that does not call out to the runtime, and it beats
anything hand-written. Two attempts to route around it, from opposite
directions, both lost:
- **Replacing a short copy with a byte loop.** `String::push_str` moves a dozen
bytes at a time, so a length-gated loop looked like it would skip a call.
json-twitter serialize, best of 3: **677 -> 479 MB/s** with the loop taken up
to 32 bytes. A GC array has no unchecked accessor, so the loop pays a bounds
check on both the `array.get` and the `array.set` of every byte.
- **Restructuring so the copy is not needed.** `fpfmt` inserts a float's decimal
point by writing the digits flush and shifting the fraction one byte right,
five sites over. Rewriting them into one right-to-left pass that writes both
digit runs around the point removes every copy at the same division and store
count — and measured flat: json-canada **198.60 -> 199.94 MB/s**, inside the
baseline arm's own 195.2-198.6 spread, `fts` unchanged, `-Os` output 33-36
bytes larger. Reverted.
Generalizes: reach for `array.copy` and leave it alone. It is not what your
profile is pointing at, so neither hand-rolling it nor contorting the algorithm
to avoid it is worth spending a measurement on.
## Widening `String::grow`'s growth factor (2026-08-28)
`String::grow` doubles, so a serializer reaching a megabyte reallocates ~14
times from `SERIALIZE_BUFFER_CAPACITY`'s 128 bytes. Widening the factor is worth
a lot on one benchmark and costs on several others, and no model of _why_ has
survived contact with the numbers. Serialize throughput, best of 3-4 alternating
on an idle host:
| factor | canada | twitter | catalog | cbor-tw ser | fts | twitter de |
| ----------------------- | --------- | ------- | ------- | ----------- | ----- | ---------- |
| 2 (today) | 215.1 | 710.5 | 1.27 GB | 1.18 GB | 19.73 | 161.3 |
| 4 | **288.8** | 736.1 | 1.31 GB | 1.14 GB | 19.25 | 154.3 |
| 16 | 237.3 | 817.9 | 1.31 GB | — | — | — |
| 2 below 64 KiB, 4 above | 251.1 | 632.3 | — | 1.16 GB | — | — |
Nothing explains the shape. It is not bytes zeroed: at factor 4 canada allocates
_more_ in total (~11.2 MB against ~8.4 MB, since the final capacity can reach
4x the content) and runs 34% faster. It is not the allocation count either:
factor 16 makes 5 allocations to factor 4's 9 and is **slower**. And the hybrid
is worse than both flat factors on twitter while making fewer allocations than
factor 2 and ending at the same capacity — twitter's response to the factor is
not even monotonic (+15.8% at 16, +3.6% at 4, -10.5% at the hybrid).
Reverted, all of it. The lead is real and unclaimed: **canada has ~30% sitting
in buffer growth policy**, and the live-set rule is the obvious suspect — its
parse tree stays reachable across the whole serialize loop, so every collection
re-traces 55K `List`s and anything changing collection frequency moves the
benchmark. Next attempt should instrument collections first, not guess a factor.
Measure a candidate on cbor-twitter, fts and a deserialize phase too; those are
where a wider factor takes its cost, and a factor tuned on canada alone looks
free.
## Three short ones (2026-07/08)
Each measured flat or negative, each with an obvious-sounding motivation:
- **A GC-array digit table.** Indexing a table of digit characters adds a
bounds-checked load per digit; plain arithmetic already had none.
- **Two-digit-at-a-time formatting.** The divides it was meant to halve were
already fused into magic multiplies, so it bought only extra multiplies.
- **Forcing inlining.** Raising the threshold bloats the hot loop; wasmtime
calls a small Wasm function cheaply enough that the call was never the cost.
The generalization: stop when the floor is the representation. A store-bound
loop over an `Array<T>`-backed `String` is near-optimal short of leaving GC
arrays, and no scheme that keeps the array beats it.
## SROA-ing the derived deserializer's slot tuple (2026-09-01)
`ReflectStruct::empty_slots` returns `[Option<F_0>, …]` as a tuple literal, but
out of line, so the caller's `let slots = empty_slots()` is bound to a _call_ and
`sroa`'s direct-literal matcher never sees it: the slots stay one heap tuple,
allocated per struct and written field by field for the whole decode. `defaults`
already carries `InlineHint::Always` for exactly this reason, and giving
`empty_slots` the same hint works — `wado dump -O2` on cbor-twitter goes from 516
`slots.N` heap accesses to none.
It is **6.5% slower**. cbor-twitter de, three alternating pairs, 212.1/212.0/214.9
→ 198.3/197.2/201.4 MB/s; json-catalog, whose structs are 2–9 fields wide, is
flat.
Forty slots become forty `ref`-typed locals live across a loop full of calls, so
the function trades one bump allocation for forty stack slots it reloads at every
call boundary — plus a `ref.null` init apiece at entry. Reverted.
Generalizes: SROA is priced by the aggregate's _width_, not by the allocation it
removes. Past the register file, decomposing a wide aggregate whose live range
spans calls is a pessimization, and "the allocation is gone" says nothing about
which side won.
## Marking the CBOR scalar decoders' slow paths cold (2026-09-01)
`deserialize_bool` / `deserialize_string` are a two-byte fast path followed by a
tolerant `loop` for tag-wrapped and indefinite encodings. `inline_cost` discounts
what a `cold_path()` marker opens, so a marker before the loop should price each
function by its fast path and get it inlined at every field.
It does not: the WIR A/B is byte-identical on the hot path — the same inlined
`peek_byte`, the same two compares — and the call sites still call. The marker
only stops `is_passthrough_tag` from being inlined into the arm nobody runs. The
fast path alone is still over `-O2`'s 16-instruction budget, so the discount
changes nothing to admit.
Generalizes: the cold discount decides admission, not size. Marking a slow path
cold is worth doing when the hot path _would_ fit under the threshold without it;
when it would not, the marker buys only cold-side bytes.
## Splitting `encode_char` at the ASCII boundary (2026-08-30)
`encode_char` carried `#[inline]` then, so its four-way UTF-8 width dispatch and
nine stores landed at every write site. Keeping the one-byte case and calling
out for the rest — what the manual-split advice prescribes — lost 2-4% on five
serde rows and gained nothing, including on json-catalog ser, the row it was
aimed at.
The split adds a call in the middle of a byte-writing loop, taking `&mut Array<u8>`, so the loop reloads the array and the position across it. What it
removed was three compares the branch predictor gets right on ASCII text.
Generalizes to: the manual-split advice is for a sub-case whose _body_ is heavy
(an allocation, a grow, a formatter), not for one that is merely branchy. Ask
what the caller copies instead — if the answer is "a compare", there was nothing
to move.
## Hoisting `HighlightVisitor::classify`'s common path to get it inlined (2026-09-02)
`classify` is one call per token and per trivia, ~5300 on syntax-highlight, and
`package-gale/perf.md` named it a lever: it walks the override list before the
`default_ids[kind]` lookup, and SQLite has no overrides at all. Splitting the
scan into `classify_override` leaves a fast path of two compares and one indexed
load, which reads like it should fit `-O2`'s 16-instruction budget.
It does not fit. `wado dump -O2` still shows both call sites, and the benchmark
is flat to slightly negative: 1.537–1.561 ms/iter against 1.525–1.536. Writing
the guard as `overrides.len() > 0` rather than `!is_empty()` changed neither,
though it did remove a non-inlined `List<ResolvedOverride>::len` call the WIR
had kept.
What paid on the same frame was deleting the caller. With no override nothing
reads the rule stack, so the CST walk that maintains it is unobservable, and
`gen_highlight` stops emitting it (+6.5%).
Generalizes: a fast path you shrink is still a call until it is under the
budget, and "under the budget" is a WIR question, not an eyeball one. Ask what
makes the call unnecessary before asking what makes it cheap.
## `TreeBuilder` handing its store over instead of copying it out (2026-09-03)
`TreeBuilder::finish(&self) -> CstStore` deep-copied `tag`/`a`/`b`/`alt` — four
~10K-element `List`s per `sqlite_parse` — because the builder held its own flat
columns and `finish` handed a fresh `CstStore` back by value. Making the
builder hold `store: CstStore` directly (`finish(&mut self)` fills the derived
`end`/`flags`/`next` columns in place, caller reads `p.b.store`) removes all
four `array_new` + `array_copy` pairs; confirmed in the WIR.
It is a **regression**: isolated from the scan-guard elision that landed
alongside it (`package-gale/perf.md`), three alternating pairs on
`sqlite_parse`, best-of-three — base 1.150 ms/iter, this change alone 1.162
ms/iter, **slower in all 3 rounds**. Combined with the scan elision it also
loses to the scan elision alone in all 3 rounds (1.131–1.141 vs 1.120–1.124).
`TreeBuilder::push_row` was 6.4% self-time in the profile (`perf.md`'s
"Current state" table) — called once per CST row, several times per token.
Nesting the four columns one level deeper (`self.tag` → `self.store.tag`)
adds a `struct.get` on every field access in that function and in `finish`,
paid every row; the four removed copies happen once per parse. Reverted.
Generalizes: an allocation removed once per call is not free against an
indirection paid on every access inside the function that removes it. Price
a copy-elision idea against the loop it would run inside, not just the
allocation it deletes.
## Judging gale-gen at its one benchmark heap size (2026-10-06)
gale-gen ran at `--gc-heap-initial 512m` then, and at that one size a change
can read as a regression that wins across the sweep. It now runs at the 256 MiB
default alone. Two gale changes that only remove copies
measured 4.1% slower at 512m, and each size reproduced across two sweeps.
ms/iter, best of two:
| heap | base | head |
| ------- | ----- | ----- |
| 256m | 97.64 | 93.73 |
| 384m | 95.06 | 92.41 |
| 512m | 87.53 | 91.25 |
| 640m | 91.05 | 88.92 |
| 768m | 89.81 | 88.23 |
| 1g | 88.45 | 89.21 |
| geomean | 91.52 | 90.60 |
The base reads 87.5 at 512m and 91–98 below it, so 512m is where its
collections happen to land well. Less allocation moves them, and the head loses
that alignment. `--collector null` agrees with the sweep: the head is 2.6%
faster.
Two more were dropped on 512m alone, and are open again for that reason: a
`rule_id` stamped on `RuleRefElement` so the SCC-memoised analyses skip the
hashed rule lookup (87.4–87.9 against 86.9–88.8), and a worklist for
`rule_input_end`'s fixed point (87.0–87.8 against 86.7–87.2).
Generalizes: on a benchmark that allocates heavily, sweep the heap size and
compare the geometric means. One size is one sample of where the collections
fall.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |