perf(router): index HTTP routes in a radix tree

Replace the linear, specificity-sorted scan behind matchRoute,
resolveRoute and allowedMethods with a segment trie per HTTP method,
built once per route array from the compiled segments compilePattern
already produces.

Ranking stays global: every terminal carries its position in the old
sorted bucket (moreSpecific, declaration order on ties), and the walk
keeps the lowest rank it can reach, pruning subtrees that cannot beat
it. The tree decides which routes can match; the rank alone decides
which one does. A static branch that dead-ends deeper backtracks into
its param, constrained and catch-all siblings.

All-static routes are also kept by exact path per method. A hit there
is provably the tree's best match, so a static request is answered
without splitting the path; a miss falls through to the walk.

Mesh-spliced remote routes go in a second tree, per the owner's
design decision. Both trees answer, the more specific match wins and
an equal one goes to the earlier declaration, so local still beats
remote on a tie. Allow, 405 and implicit HEAD run over the union. An
app without remote routes does one lookup.

allowedMethods walks the index instead of recompiling every route on
a miss, and an implicit HEAD no longer builds a fresh index from a
filtered array on every request.

The frozen parity corpus passes unchanged. The frozen mutant script
cannot run against the new layout by design, so
test/router-index-mutants.ts re-targets its twelve bug classes at
the new code and runs the same untouched suite.

Refs #109

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-11 12:56:37 -05:00
1 parent 7ee7ab7ec5
commit 815f76116f
5 files changed
+731 -72

No files matched your search

+299
View File
@@ -0,0 +1,299 @@
/**
* Mutation check for the radix index (CARD-07, #109): the twelve bug classes of the frozen
* `test/router-parity/mutants.ts`, re-targeted at the code that replaced the linear matcher.
*
* npm run parity:mutants:index
*
* The frozen script cannot run against this router: its search texts are lines of the old linear
* scan, and it refuses to start when one is missing — which is what it was written to do ("mutants
* against the new code belong to CARD-07"). This one keeps everything else identical: the same
* suite (`vitest run test/router-parity`, untouched), the same temporary copy of `src/`, the same
* exactly-once rule for every search text, the same unmutated control run, and the same final
* `git diff --exit-code src`. Ids and the bug each one models are unchanged; six mutants apply
* verbatim, six are re-targeted. A mutant may need more than one edit when the new code holds the
* same rule in two places (the walk filters, then `matchCompiled` collects params); every edit
* models the same bug.
*
* Not part of `npm test`: it runs the suite once per mutant.
*/
import { spawnSync, execFileSync } from 'child_process';
import { cpSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'fs';
import os from 'os';
import { join, resolve } from 'path';
const ROOT = resolve(__dirname, '..');
const ROUTER = 'src/http/router.ts';
interface Edit {
file: string;
from: string;
to: string;
}
interface Mutant {
id: string;
models: string;
/** `verbatim`: the frozen mutant's own edit. `retargeted`: the same bug, written against the index. */
origin: 'verbatim' | 'retargeted';
edits: Edit[];
}
export const MUTANTS: Mutant[] = [
{
id: 'no-backtrack',
models: 'no backtracking to a param branch once a static first segment was entered',
origin: 'retargeted',
edits: [
{
file: ROUTER,
from: ` if (next) walk(next, search, segs, depth + 1);`,
to: ` if (next && depth === 0) return walk(next, search, segs, depth + 1);
if (next) walk(next, search, segs, depth + 1);`,
},
],
},
{
id: 'precedence-swapped',
models: 'static and plain-param precedence swapped',
origin: 'verbatim',
edits: [
{
file: ROUTER,
from: `.map((segment) => (segment.kind === 'static' ? 3 : segment.constraint ? 2 : 1)),`,
to: `.map((segment) => (segment.kind === 'static' ? 1 : segment.constraint ? 2 : 3)),`,
},
],
},
{
id: 'tie-last-registered',
models: 'equal-rank tie broken by the last-registered declaration',
origin: 'retargeted',
edits: [
{
file: ROUTER,
from: ` bucket.sort((a, b) => moreSpecific(b.compiled, a.compiled));`,
to: ` bucket.reverse().sort((a, b) => moreSpecific(b.compiled, a.compiled));`,
},
],
},
{
id: 'constraint-ignored',
models: 'param constraint not tested during lookup',
origin: 'retargeted',
edits: [
{ file: ROUTER, from: ` if (edge.constraint && !edge.constraint.test(value)) continue;\n`, to: `` },
// the frozen mutant's own edit: without it the walk would pick a route whose params then fail to collect
{
file: ROUTER,
from: ` if (segment.constraint && !segment.constraint.test(value)) return undefined;\n`,
to: ``,
},
],
},
{
id: 'decode-before-split',
models: 'path decoded before splitting, so %2F becomes a separator',
origin: 'verbatim',
edits: [
{
file: ROUTER,
from: ` return path.split('/').filter(Boolean);`,
to: ` return decodeURIComponent(path).split('/').filter(Boolean);`,
},
],
},
{
id: 'catch-all-needs-segment',
models: 'catch-all does not match zero segments',
origin: 'retargeted',
edits: [
{
file: ROUTER,
from: ` if (node.catchAll.length) offer(search, node.catchAll);`,
to: ` if (node.catchAll.length && depth < segs.length) offer(search, node.catchAll);`,
},
],
},
{
id: 'implicit-head-from-stream',
models: 'implicit HEAD served by a streaming GET',
origin: 'retargeted',
edits: [
{
file: ROUTER,
from: ` const fallback = findRoute(index, 'GET', search, isBuffered);`,
to: ` const fallback = findRoute(index, 'GET', search);`,
},
],
},
{
id: 'allow-one-pattern',
models: 'Allow built from the first matching pattern only',
origin: 'retargeted',
edits: [
{
file: ROUTER,
from: ` if (method === 'GET' && findRoute(index, method, search, isBuffered)) methods.add('GET').add('HEAD');
else if (findRoute(index, method, search)) methods.add(method);`,
to: ` const hit = findRoute(index, method, search);
if (!hit) continue;
onlyPattern ??= hit.def.pattern;
if (hit.def.pattern !== onlyPattern) continue;
methods.add(method);
if (method === 'GET' && findRoute(index, method, search, isBuffered)) methods.add('HEAD');`,
},
{
file: ROUTER,
from: ` const methods = new Set<string>();\n\n for (const method of index.methods) {`,
to: ` const methods = new Set<string>();\n let onlyPattern: string | undefined;\n\n for (const method of index.methods) {`,
},
],
},
{
id: 'allow-order',
models: 'Allow in discovery order instead of canonical order',
origin: 'verbatim',
edits: [
{
file: ROUTER,
from: ` return [...methods].sort((a, b) => {`,
to: ` return [...methods];
[...methods].sort((a, b) => {`,
},
],
},
{
id: 'trailing-slash-kept',
models: 'trailing slash not normalized',
origin: 'verbatim',
edits: [
{
file: ROUTER,
from: ` const normalized = resolved.length > 1 && resolved.endsWith('/') ? resolved.slice(0, -1) : resolved;`,
to: ` const normalized = resolved;`,
},
],
},
{
id: 'ws-ranked-like-http',
models: 'WS upgrades ranked by specificity like HTTP instead of first registered',
origin: 'verbatim',
edits: [
{
file: 'src/http/ws-core.ts',
from: ` .find((entry) => entry.params);`,
to: ` .filter((entry) => entry.params)
.sort((a, b) => a.def.pattern.split(':').length - b.def.pattern.split(':').length)[0];`,
},
],
},
{
id: 'case-insensitive-static',
models: 'static segments compared case-insensitively',
origin: 'retargeted',
edits: [
{
file: ROUTER,
from: ` const next = node.statics.size ? node.statics.get(segment) : undefined;`,
to: ` const next = [...node.statics].find(([key]) => key.toLowerCase() === segment.toLowerCase())?.[1];`,
},
// the frozen mutant's own edit, so the chosen route also collects its params
{
file: ROUTER,
from: ` if (segment.value !== pathSegs[index]) return undefined;`,
to: ` if (segment.value.toLowerCase() !== pathSegs[index].toLowerCase()) return undefined;`,
},
],
},
];
function runSuite(dir: string): { passed: boolean; failed: number } {
const out = join(dir, 'vitest.json');
const res = spawnSync(
process.execPath,
[
join(ROOT, 'node_modules/vitest/vitest.mjs'),
'run',
'test/router-parity',
'--reporter=json',
`--outputFile=${out}`,
],
{ cwd: dir, env: { ...process.env, ROUTER_PARITY_CAPTURE: '' }, encoding: 'utf8', stdio: 'pipe' },
);
let failed = 0;
try {
failed = (JSON.parse(readFileSync(out, 'utf8')) as { numFailedTests: number }).numFailedTests;
} catch {
failed = -1; // the suite did not even report, which also kills the mutant
}
return { passed: res.status === 0, failed };
}
function copyTree(): string {
const dir = mkdtempSync(join(os.tmpdir(), 'router-index-mutants-'));
for (const entry of ['src', 'test']) cpSync(join(ROOT, entry), join(dir, entry), { recursive: true });
for (const file of ['package.json', 'tsconfig.json', 'vitest.config.ts']) cpSync(join(ROOT, file), join(dir, file));
symlinkSync(join(ROOT, 'node_modules'), join(dir, 'node_modules'), 'dir');
return dir;
}
/** Applies every edit of one mutant, each of which must find its search text exactly once. */
function applyMutant(dir: string, mutant: Mutant): Map<string, string> {
const originals = new Map<string, string>();
for (const edit of mutant.edits) {
const path = join(dir, edit.file);
if (!originals.has(path)) originals.set(path, readFileSync(path, 'utf8'));
const current = readFileSync(path, 'utf8');
const hits = current.split(edit.from).length - 1;
if (hits !== 1) throw new Error(`${mutant.id}: search text found ${hits} times in ${edit.file}`);
writeFileSync(path, current.replace(edit.from, edit.to));
}
return originals;
}
function main(): void {
const only = process.env.MUTANTS_ONLY?.split(',');
const mutants = only ? MUTANTS.filter((m) => only.includes(m.id)) : MUTANTS;
const dir = copyTree();
const rows: Array<{ id: string; origin: string; models: string; result: string; failedTests: number }> = [];
try {
const control = runSuite(dir);
if (!control.passed)
throw new Error('the unmutated copy fails the parity suite; no mutant result would mean anything');
for (const mutant of mutants) {
const originals = applyMutant(dir, mutant);
try {
const { passed, failed } = runSuite(dir);
rows.push({
id: mutant.id,
origin: mutant.origin,
models: mutant.models,
result: passed ? 'SURVIVED' : 'killed',
failedTests: failed,
});
process.stderr.write(`[mutants] ${mutant.id}: ${passed ? 'SURVIVED' : `killed (${failed} failing)`}\n`);
} finally {
for (const [path, original] of originals) writeFileSync(path, original);
}
}
} finally {
rmSync(dir, { recursive: true, force: true });
}
console.log('| mutant | edit | models | result | failing tests |');
console.log('| --- | --- | --- | --- | --- |');
for (const row of rows)
console.log(`| ${row.id} | ${row.origin} | ${row.models} | ${row.result} | ${row.failedTests} |`);
const killed = rows.filter((row) => row.result === 'killed').length;
console.log(`\n${killed}/${rows.length} killed`);
// The guarantee this script makes: the real src/ was never touched.
execFileSync('git', ['diff', '--exit-code', '--', 'src'], { cwd: ROOT, stdio: 'inherit' });
if (killed !== rows.length) process.exitCode = 1;
}
main();
+108
View File
@@ -0,0 +1,108 @@
/**
* The radix index's own seams, which the frozen parity corpus (test/router-parity/) cannot reach:
*
* - The local/remote split. The corpus drives the matcher with flat arrays, so every scenario lands
* in the local tree; only the app-level mesh captures reach the remote one. Here every frozen
* scenario is replayed with its declarations spread across both trees, and must still answer
* exactly as the oracle does on the same flat array.
* - Index lifetime: an array that changes length after its index was built is never served stale.
*/
import { describe, it, expect } from 'vitest';
import * as live from '../src/http/router';
import type { RouteDef } from '../src/http/types';
import * as oracle from './router-parity/oracle';
import { runScenario, type MatcherImpl, type Scenario } from './router-parity/matcher';
import { HAND_SCENARIOS, OWNER_SCENARIOS } from './router-parity/cases';
import { generateScenarios, generateScenariosV2 } from './router-parity/generator';
const SCENARIOS: Scenario[] = [...HAND_SCENARIOS, ...OWNER_SCENARIOS, ...generateScenarios(), ...generateScenariosV2()];
/** Which positions of the declaration array go to the remote tree. */
const SPLITS: Record<string, (index: number, length: number) => boolean> = {
'all remote': () => true,
'second half remote': (index, length) => index >= length / 2,
'odd positions remote': (index) => index % 2 === 1,
// Not a shape mesh produces (remote routes are spliced after local ones), but the tie rule is
// array order either way, so the answer must not depend on it.
'first half remote': (index, length) => index < length / 2,
};
/** The live router, with some declarations marked remote before the first lookup builds the index. */
function splitImpl(isRemote: (index: number, length: number) => boolean): MatcherImpl {
const mark = (routes: RouteDef[]): RouteDef[] => {
routes.forEach((route, index) => isRemote(index, routes.length) && live.markRemoteRoute(route));
return routes;
};
return {
normalizeRequestPath: live.normalizeRequestPath,
matchRoute: (routes, method, path) => live.matchRoute(mark(routes), method, path),
resolveRoute: (routes, method, path) => live.resolveRoute(mark(routes), method, path),
allowedMethods: (routes, path) => live.allowedMethods(mark(routes), path),
};
}
function route(method: string, pattern: string, transport: RouteDef['transport'] = 'buffer'): RouteDef {
return { method, pattern, transport, handler: async () => ({ status: 200, headers: {}, body: pattern }) };
}
describe('radix index: local and remote trees', () => {
for (const [name, isRemote] of Object.entries(SPLITS)) {
it(`answers every frozen scenario like the flat oracle — ${name}`, () => {
const impl = splitImpl(isRemote);
for (const scenario of SCENARIOS) {
expect(runScenario(impl, scenario), scenario.id).toEqual(runScenario(oracle, scenario));
}
});
}
it('lets a remote static route outrank a local param route, and a local twin win a tie', () => {
const localParam = route('GET', '/items/:id');
const localTwin = route('GET', '/items/special');
const remoteStatic = live.markRemoteRoute(route('GET', '/items/special'));
const remoteOnly = live.markRemoteRoute(route('GET', '/items/other'));
expect(live.matchRoute([localParam, remoteStatic, remoteOnly], 'GET', '/items/other')?.def).toBe(remoteOnly);
expect(live.matchRoute([localParam, remoteStatic], 'GET', '/items/special')?.def).toBe(remoteStatic);
expect(live.matchRoute([localParam, localTwin, remoteStatic], 'GET', '/items/special')?.def).toBe(localTwin);
});
it('breaks a cross-tree tie by array position, so a later dev route never shadows a remote one', () => {
const remote = live.markRemoteRoute(route('GET', '/__dev'));
const dev = route('GET', '/__dev');
expect(live.matchRoute([remote, dev], 'GET', '/__dev')?.def).toBe(remote);
});
it('builds Allow and implicit HEAD over the union of both trees', () => {
const local = route('POST', '/a/:id');
const remoteGet = live.markRemoteRoute(route('GET', '/a/:id'));
const routes = [local, remoteGet];
expect(live.allowedMethods(routes, '/a/1')).toEqual(['GET', 'HEAD', 'POST', 'OPTIONS']);
expect(live.resolveRoute(routes, 'HEAD', '/a/1')).toMatchObject({ def: remoteGet, implicitHead: true });
});
it('passes over a higher-ranked streaming GET in one tree for a buffered GET in the other', () => {
const stream = route('GET', '/s/live', 'sse');
const buffered = live.markRemoteRoute(route('GET', '/s/:name'));
expect(live.resolveRoute([stream, buffered], 'HEAD', '/s/live')).toMatchObject({
def: buffered,
params: { name: 'live' },
implicitHead: true,
});
});
});
describe('radix index: lifetime', () => {
it('rebuilds when the array it was built from changes length', () => {
const routes = [route('GET', '/a')];
expect(live.matchRoute(routes, 'GET', '/b')).toBeUndefined();
const added = route('GET', '/b');
routes.push(added);
expect(live.matchRoute(routes, 'GET', '/b')?.def).toBe(added);
expect(live.allowedMethods(routes, '/b')).toEqual(['GET', 'HEAD', 'OPTIONS']);
routes.pop();
expect(live.matchRoute(routes, 'GET', '/b')).toBeUndefined();
});
});