Route ranking at build time, and a benchmark that can measure a release #87

Merged
chrnx merged 3 commits from perf/matcher-and-benchmarks into develop 2026-09-02 03:07:39 +00:00
Owner

Three commits, split because they are three different kinds of change: one alters the framework, one adds instruments, one publishes measurements.

8c5855c — route ranking settles when the table is built

Matching scanned every method-matching route on each request and ranked candidates as it went, calling moreSpecific — two compilePattern lookups each — per candidate. Both the scan and the ranking scale with the route table, and the answer cannot change between requests: the table is assembled once and handed to the adapter unchanged.

Routes are now compiled, bucketed by method and sorted most-specific-first once. Matching walks to the first match and returns. sort is stable, so equal specificity keeps registration order, which is what the old scan produced by refusing to replace its best on a tie.

route table Δ
6 routes nothing — every row overlaps
50 routes +3.3%, +3.5% across two runs
200 routes +14.9%, +12.0% — twelve rounds, not one crossing sample

Plus two smaller savings on the same path: decodeSegment skips decodeURIComponent when the segment holds no % (the gate normalizeRequestPath already applies to the whole path), and decoding memoizes per request rather than per candidate. allStatic is deleted — sorting subsumes its short-circuit.

matchRoute precedence already covered the ordering this had to preserve and passes unchanged. Two cases were missing and are added; both pass against the previous implementation too, which is what makes them evidence the rewrite preserved behaviour rather than evidence of the change.

9c78842 — npm run bench:compare, runtimes, table width

BENCHMARKS.md answered "how does this compare to fastify". Nothing answered "did the last two months cost anything", which is the question a release has to pass.

bench:compare runs two worktrees against each other on one box, alternating which side goes first each round. It refuses a run whose connections errored, counts cors.origins invocations rather than timing them, and documents that its range-overlap check means "measure it again" — each of those after getting it wrong once.

The Deno and Bun servers finally run. Written in July, referenced by nothing: not the runner, not a script, not CI. The runner only knew how to launch Node. They get their own section now, and a new green-tea-node.ts loads dist/ so the runtimes table varies the runtime and nothing else.

A route-table-width dimension, because three matcher findings measured as noise at six routes — which said nothing about them and everything about the instrument.

e617710 — tables refreshed, and what moved between releases

Measured at 8c5855c, every row under 1.2% CV. The previous tables were from 2026-07-07, two releases ago.

The headline is not in the cross-framework tables. green-tea is within 2% of fastify on hello and param and second only to raw-http on validate — but the same application serves 153,408 req/s on Deno and 131,674 on Bun against Node's 90,976, above the 119,357 Node's own http module manages with no framework.

The notes below the marker carry the release gate (neutral against 26.8.0-beta.1, +2.7% on the CORS preflight), the interval against 26.7 and why −5% to −8% is not a regression, the figures recorded in code when each change landed, and what is still unattributed — which is most of it. Three candidates were measured and cleared: randomUUID, the specificity ranking, and the bundle versus the source.


Filed from this work: #85 (radix, now that a benchmark can see one) and #86 (boot time and streaming still unmeasured).

Gates: lint 0 errors, format, complexity, typecheck for src and bench, 539 vitest on Node 22, 17 Deno, 7 Bun, edge PASS, build, deno publish --dry-run.

Three commits, split because they are three different kinds of change: one alters the framework, one adds instruments, one publishes measurements. ### `8c5855c` — route ranking settles when the table is built Matching scanned every method-matching route on each request and ranked candidates as it went, calling `moreSpecific` — two `compilePattern` lookups each — per candidate. Both the scan and the ranking scale with the route table, and the answer cannot change between requests: the table is assembled once and handed to the adapter unchanged. Routes are now compiled, bucketed by method and sorted most-specific-first once. Matching walks to the first match and returns. `sort` is stable, so equal specificity keeps registration order, which is what the old scan produced by refusing to replace its best on a tie. | route table | Δ | | --- | --- | | 6 routes | nothing — every row overlaps | | 50 routes | +3.3%, +3.5% across two runs | | 200 routes | **+14.9%, +12.0%** — twelve rounds, not one crossing sample | Plus two smaller savings on the same path: `decodeSegment` skips `decodeURIComponent` when the segment holds no `%` (the gate `normalizeRequestPath` already applies to the whole path), and decoding memoizes per request rather than per candidate. `allStatic` is deleted — sorting subsumes its short-circuit. `matchRoute precedence` already covered the ordering this had to preserve and passes unchanged. Two cases were missing and are added; **both pass against the previous implementation too**, which is what makes them evidence the rewrite preserved behaviour rather than evidence of the change. ### `9c78842` — `npm run bench:compare`, runtimes, table width `BENCHMARKS.md` answered "how does this compare to fastify". Nothing answered "did the last two months cost anything", which is the question a release has to pass. `bench:compare` runs two worktrees against each other on one box, alternating which side goes first each round. It refuses a run whose connections errored, counts `cors.origins` invocations rather than timing them, and documents that its range-overlap check means "measure it again" — each of those after getting it wrong once. **The Deno and Bun servers finally run.** Written in July, referenced by nothing: not the runner, not a script, not CI. The runner only knew how to launch Node. They get their own section now, and a new `green-tea-node.ts` loads `dist/` so the runtimes table varies the runtime and nothing else. A route-table-width dimension, because three matcher findings measured as noise at six routes — which said nothing about them and everything about the instrument. ### `e617710` — tables refreshed, and what moved between releases Measured at `8c5855c`, every row under 1.2% CV. The previous tables were from 2026-07-07, two releases ago. The headline is not in the cross-framework tables. green-tea is within 2% of fastify on `hello` and `param` and second only to `raw-http` on `validate` — but the same application serves **153,408 req/s on Deno** and 131,674 on Bun against Node's 90,976, above the 119,357 Node's own `http` module manages with no framework. The notes below the marker carry the release gate (neutral against `26.8.0-beta.1`, +2.7% on the CORS preflight), the interval against `26.7` and why −5% to −8% is not a regression, the figures recorded in code when each change landed, and **what is still unattributed** — which is most of it. Three candidates were measured and cleared: `randomUUID`, the specificity ranking, and the bundle versus the source. --- Filed from this work: #85 (radix, now that a benchmark can see one) and #86 (boot time and streaming still unmeasured). Gates: lint 0 errors, format, complexity, typecheck for `src` and `bench`, **539** vitest on Node 22, 17 Deno, 7 Bun, edge PASS, build, `deno publish --dry-run`.
chrnx added 3 commits 2026-09-02 03:06:18 +00:00
Route matching scanned every method-matching route on every request and ranked the
candidates as it went, calling `moreSpecific` — two `compilePattern` lookups each — per
candidate. Both the scan and the ranking scale with the size of the route table, and the
answer they compute cannot change between requests: the table is assembled once, after the
graph is prepared, and handed to the adapter unchanged.

So the ranking moves to where the table is built. Routes are compiled, bucketed by method
and sorted most-specific-first, and matching walks until the first match and returns it.
`sort` is stable, so patterns of equal specificity keep registration order — which is what
the old scan produced by refusing to replace its best on a tie.

Measured on a request to a parameterised route registered last, against the same tree
without these changes, medians of five and seven rounds:

      6 routes      no change (every row overlaps)
     50 routes      +3.3%, +3.5% across two runs
    200 routes      +14.9%, +12.0% — twelve rounds, not one crossing sample

The shape is the point rather than any single number: the saving is proportional to how
many candidates a request walks past, so it is invisible on a small table by construction.
That is also why it went unnoticed. The benchmark had no route-table-width dimension until
this commit added one, so every matcher change measured as noise no matter how real.

Two smaller savings on the same path. `decodeSegment` now skips `decodeURIComponent` when
the segment holds no `%`, the same gate `normalizeRequestPath` already applies to the whole
path for the same reason. And decoding memoizes per request rather than per candidate: a
scan reaching the same parameter position on several routes used to decode the same string
once per route, and position is all decoding depends on.

`allStatic` is deleted. It short-circuited the scan on an all-literal match, which sorting
subsumes — such a pattern now sorts first and wins by position.

The index is keyed by the identity of the route array, so it is built once and collected
with the table it describes. A table assembled fresh — mesh splicing a teapot's exports in
at boot — is a different array and gets its own index, so no caller has to invalidate
anything.

`matchRoute precedence` already covered the ordering this rewrite had to preserve, and it
passes unchanged. Two cases were missing and are added: that equal specificity keeps
registration order, which is the property the stable sort now carries, and that a parameter
decodes to the same value however many candidates the scan walks. Both pass against the
previous implementation too — they describe behaviour that already existed, which is what
makes them evidence the rewrite preserved it rather than evidence of the change.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
`BENCHMARKS.md` answered "how does this compare to fastify" and nothing answered "did the
last two months cost anything". Those are different questions and the second one is the
one a release has to pass, so it needed its own tool rather than a second reading of the
first one's table.

`npm run bench:compare` runs two worktrees of this repository against each other on one
box in one sitting, alternating which side goes first each round so drift lands on both
rather than on whichever ran second. It shares one `node_modules` and one byte-identical
server file, which holds everything except `src/` constant — and is only sound while
`package.json` has not moved between the revisions, which the header says out loud.

Three things it refuses to do quietly, each of which it did once before being taught not
to:

- A run whose connections all errored reports 0 req/s, and a median absorbs that without
  complaint. Non-2xx, errors and timeouts now fail the case, and so does a zero. Not on
  the warmup, which exists to absorb exactly the cold-start noise a fresh server produces.
- A range-overlap check flags a row as directional, and its own documentation says to
  treat that as "measure it again", never as a result. Three samples of two overlapping
  distributions separate by chance often enough to have produced a regression report that
  five rounds then erased.
- `cors.origins` invocations are counted rather than timed. The benchmark's predicate is a
  `Set` lookup, so calls saved are invisible in req/s — but the option takes a function so
  it can be a lookup, and there the same saving is a network round trip.

**The runtime servers finally run.** `bench/servers/green-tea-deno.ts` and `-bun.ts` were
written in July and referenced by nothing — not the runner, not a script, not CI. The
runner only knew how to launch Node, so both sat there looking like coverage the table did
not have. Launchers are per runtime now and they get their own section, kept out of the
cross-framework tables because a green-tea-on-Bun row beside fastify-on-Node reads as a
comparison nobody made.

`green-tea-node.ts` is new and loads `dist/` like its Deno and Bun counterparts. The
runtimes table exists to vary the runtime, and a Node row reading `src/` through tsx would
have varied the load form at the same time.

A route-table-width dimension, because every matcher cost scales with how many candidates
a request walks past and the tables here use six routes. Three separate matcher findings
measured as noise before this existed, which said nothing about them and everything about
the instrument.

Finally, `run.ts` preserves whatever sits below a marker instead of overwriting the file
whole. The tables answer what is true today; the notes below them carry which release moved
a number and what the move bought, and regenerating used to delete that silently.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
docs(bench): refresh the tables and record what moved between releases
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 15s
CI / build (pull_request) Successful in 31s
CI / runtimes (deno) (pull_request) Successful in 19s
CI / runtimes (edge) (pull_request) Successful in 14s
e61771077c
The tables were measured on 2026-07-07 at `fe1831a`, two releases ago. Re-measured at
`8c5855c` with the runtime matrix included, every row under 1.2% CV.

The headline is not in the cross-framework tables. green-tea sits within 2% of fastify on
`hello` and `param` and second only to `raw-http` on `validate` — but the same application
on Deno serves 153,408 req/s against Node's 90,976, and on Bun 131,674. Node's own `http`
module with no framework manages 119,357. The runtime-neutral boundary that costs on Node
is what makes those rows possible, and until this run nothing measured them.

The notes below the marker say what a table cannot. In order: the release gate — this
release is neutral against `26.8.0-beta.1` on every scenario but the CORS preflight, which
gains 2.7%; the interval against `26.7`, which is roughly −5% to −8% and is **not** a
regression, because July matched routes by splitting a string in one flat file and had no
constraints, no specificity, no correlation and one adapter; the figures recorded in the
code when each change landed, including one that reached 45% of a request before it was
fixed; and what is still unattributed, which is most of it.

That last part is the point. Three candidates were measured and none of them explain the
interval: `randomUUID` is 0.7% and lands within noise, replacing specificity ranking with
July's first-match-wins is noise, and the bundle versus the source is noise. A section that
explained everything would be a defence. This one says what was found and what was not.

The measurements were always there — `core.ts`, `bus.ts`, `pipeline.ts`, `headers.ts`,
`body.ts`, `router.ts` all carry them, with alternatives compared and rejected. They cite
different baselines, 4.4 µs to 10.19 µs, because each was taken against whatever a request
cost that day, so they cannot be added up. What was missing was never rigour. It was a
number per version against a fixed baseline, in the file where the question gets asked.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx merged commit 7ccb55d07a into develop 2026-09-02 03:07:39 +00:00
Sign in to join this conversation.