Radix-tree router #109

Closed
opened 2026-09-25 14:02:14 +00:00 by chrnx · 1 comment
Owner

Route matching is a linear scan today. A tree is a requirement for stable: Fastify (find-my-way), Hono and h3 (rou3) all match through a tree or a precompiled matcher, and they are what green-tea gets compared with.

Where it costs, in src/http/router.ts:

  • matchRoute walks the method's routes, sorted by specificity, until the first match, so every request is O(routes before its own).
  • allowedMethods, used for every 404/405/OPTIONS, walks all routes and runs compilePattern for each one on every call. An attacker picks the misses.
  • WebSocket routes are matched separately (matchWsRoute).

What must not change: precedence (static > constrained > plain param > catch-all), ties in registration order, the safe-regex constraint subset, implicit HEAD from buffered GET, the canonical Allow, the 400s for // and malformed encoding, the ambiguity checks at boot, and mesh splicing routes into a fresh table. test/router.test.ts and the parity suite are the contract, and they have to pass unmodified.

Also: one tree per method, plus a method set at each leaf so allowedMethods is a single lookup. WS routes go in the same structure. Bench before and after with npm run bench, reported both ways.

Lands before rc.1. The public API does not change, but it is the largest behaviour-risk change left.

Route matching is a linear scan today. A tree is a requirement for stable: Fastify (find-my-way), Hono and h3 (rou3) all match through a tree or a precompiled matcher, and they are what green-tea gets compared with. Where it costs, in src/http/router.ts: - `matchRoute` walks the method's routes, sorted by specificity, until the first match, so every request is O(routes before its own). - `allowedMethods`, used for every 404/405/OPTIONS, walks **all** routes and runs `compilePattern` for each one on every call. An attacker picks the misses. - WebSocket routes are matched separately (`matchWsRoute`). **What must not change:** precedence (static > constrained > plain param > catch-all), ties in registration order, the safe-regex constraint subset, implicit HEAD from buffered GET, the canonical `Allow`, the 400s for `//` and malformed encoding, the ambiguity checks at boot, and mesh splicing routes into a fresh table. `test/router.test.ts` and the parity suite are the contract, and they have to pass unmodified. **Also:** one tree per method, plus a method set at each leaf so `allowedMethods` is a single lookup. WS routes go in the same structure. Bench before and after with `npm run bench`, reported both ways. **Lands before rc.1.** The public API does not change, but it is the largest behaviour-risk change left.
chrnx added the enhancement label 2026-09-25 16:06:53 +00:00
Author
Owner

Fixed in 26.11.0-rc.1 (2e2c68d, a64f514, 7a9932f). 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%).
Fixed in **26.11.0-rc.1** (`2e2c68d`, `a64f514`, `7a9932f`). 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:04:03 +00:00
Sign in to join this conversation.