matcher: a radix tree, now that there is a benchmark that can see one #85

Closed
opened 2026-09-02 02:28:24 +00:00 by chrnx · 1 comment
Owner

The matcher is a linear scan, which the README and the routing guide both already name as post-beta
work. What was missing was a number, and a way to get one — the benchmark could not see the matcher
at all until this release.

Why it measured as nothing

Every matcher cost scales with how many candidate routes a request walks past. The cross-framework
tables use six routes. Three separate matcher findings measured as noise there, and all three
were real:

  • replacing specificity ranking with July's first-match-wins: noise on four rows
  • the per-candidate decodeURIComponent: below the resolution of a load test
  • settling the ranking at build time: noise

npm run bench:compare now carries 50- and 200-route cases. With them, the same change that measured
as nothing measures as this:

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

That is the instrument, not the optimisation. Any radix work needs it or it will conclude the same
thing the linear-scan work kept concluding: that nothing happened.

What is already done, and why it is the first step rather than a detour

8c5855c moved route ranking to where the table is built: routes are compiled, bucketed by method,
and sorted most-specific-first once, so matching walks to the first match and returns. allStatic
went with it — sorting subsumes the short-circuit.

A radix tree does not remove that work, it relocates it. Indexing static prefixes still leaves
sibling branches at each node — literals, parameters, constrained parameters, a catch-all — and they
still need a deterministic order to be tried in. That order is the specificity ranking, precomputed
per node. So the ranking is a prerequisite the tree needs, and it happens to pay on its own.

Scope

find-my-way, which is what fastify routes with, is a radix tree — so "how fastify does it" and
"the roadmap item" are the same thing. What cannot be lifted directly is the pair of semantics
green-tea has and it resolves differently:

  • per-segment constraints, which become a guarded branch rather than a candidate to test;
  • specificity with registration-order tie-breaks, which becomes sibling order inside a node.

Worth measuring before building: today's linear scan reaching a parameter segment runs
decodeURIComponent per candidate at that position, and a tree reaches far fewer candidates. That
saving is separate from the traversal one and neither has been isolated.

What is not settled

The 26.7 → 26.9 interval lost roughly 5–8% on hello and param and none of it has been
attributed
. Three candidates were measured and cleared: randomUUID (0.7%, within noise), the
specificity ranking (noise at six routes), and the bundle versus the source (noise). The matcher is
therefore not known to be where that time went — this issue is about a cost that grows with the
route table, which is a different claim and the one the numbers support.

Related: BENCHMARKS.md, "What this file does not measure".

The matcher is a linear scan, which the README and the routing guide both already name as post-beta work. What was missing was a number, and a way to get one — the benchmark could not see the matcher at all until this release. ## Why it measured as nothing Every matcher cost scales with how many candidate routes a request walks past. The cross-framework tables use **six** routes. Three separate matcher findings measured as noise there, and all three were real: - replacing specificity ranking with July's first-match-wins: noise on four rows - the per-candidate `decodeURIComponent`: below the resolution of a load test - settling the ranking at build time: noise `npm run bench:compare` now carries 50- and 200-route cases. With them, the same change that measured as nothing measures as this: | 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 | That is the instrument, not the optimisation. Any radix work needs it or it will conclude the same thing the linear-scan work kept concluding: that nothing happened. ## What is already done, and why it is the first step rather than a detour `8c5855c` moved route ranking to where the table is built: routes are compiled, bucketed by method, and sorted most-specific-first once, so matching walks to the first match and returns. `allStatic` went with it — sorting subsumes the short-circuit. A radix tree does not remove that work, it relocates it. Indexing static prefixes still leaves sibling branches at each node — literals, parameters, constrained parameters, a catch-all — and they still need a deterministic order to be tried in. That order *is* the specificity ranking, precomputed per node. So the ranking is a prerequisite the tree needs, and it happens to pay on its own. ## Scope `find-my-way`, which is what fastify routes with, is a radix tree — so "how fastify does it" and "the roadmap item" are the same thing. What cannot be lifted directly is the pair of semantics green-tea has and it resolves differently: - **per-segment constraints**, which become a guarded branch rather than a candidate to test; - **specificity with registration-order tie-breaks**, which becomes sibling order inside a node. Worth measuring before building: today's linear scan reaching a parameter segment runs `decodeURIComponent` per candidate at that position, and a tree reaches far fewer candidates. That saving is separate from the traversal one and neither has been isolated. ## What is not settled The `26.7` → `26.9` interval lost roughly 5–8% on `hello` and `param` and **none of it has been attributed**. Three candidates were measured and cleared: `randomUUID` (0.7%, within noise), the specificity ranking (noise at six routes), and the bundle versus the source (noise). The matcher is therefore *not* known to be where that time went — this issue is about a cost that grows with the route table, which is a different claim and the one the numbers support. Related: BENCHMARKS.md, "What this file does not measure".
Author
Owner

Done in 26.11.0-rc.1 through #109. From the changelog:

  • HTTP route lookup uses a radix index (#109). The router used to scan a specificity-sorted list per method, and on every 404 and 405 it recompiled and matched every route to build Allow. It now walks a tree per method, built once from the route table, with mesh routes from teapots in a tree of their own. This is internal and changes no behaviour: the same route wins, with the same params, Allow, implicit HEAD and 404/405, checked against the frozen parity corpus. graph(), explain() and openapi() still read the declared routes, not the tree. Median lookup time against the old matcher (jarvis, bench:lookup): at 500 routes, static hit −54%, late param hit −88%, method miss −94%, path miss −96%; at 10 routes, every cell is within noise or faster (late param hit +3.4%, 95% CI −5.0…+12.6%).
Done in **26.11.0-rc.1** through #109. From the changelog: - **HTTP route lookup uses a radix index** (#109). The router used to scan a specificity-sorted list per method, and on every 404 and 405 it recompiled and matched every route to build `Allow`. It now walks a tree per method, built once from the route table, with mesh routes from teapots in a tree of their own. This is internal and changes no behaviour: the same route wins, with the same params, `Allow`, implicit `HEAD` and 404/405, checked against the frozen parity corpus. `graph()`, `explain()` and `openapi()` still read the declared routes, not the tree. Median lookup time against the old matcher (jarvis, `bench:lookup`): at 500 routes, static hit −54%, late param hit −88%, method miss −94%, path miss −96%; at 10 routes, every cell is within noise or faster (late param hit +3.4%, 95% CI −5.0…+12.6%).
chrnx closed this issue 2026-11-18 01:03:55 +00:00
Sign in to join this conversation.