From 3907464eabfb7eb772f7306c63f0529289059953 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 2 Nov 2026 01:01:27 +0000 Subject: [PATCH 1/3] perf(router): mark a static path added after the filter is built instead of dropping the filter Adding a static route after the tree has served FILTER_AFTER requests used to drop the filter, so the next request rebuilt it over every static path. Adding routes between requests, as lazy routers do, then cost a pass over all statics per add. Marking the new path's slot keeps the filter a superset of the statics at O(1) per add. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VVaw7PzysACDLzaReKDUKE --- packages/router/src/index.ts | 9 +++++++-- packages/router/tests/late-adds.test.ts | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/router/src/index.ts b/packages/router/src/index.ts index 57363b5b..ae0627d7 100644 --- a/packages/router/src/index.ts +++ b/packages/router/src/index.ts @@ -63,7 +63,7 @@ interface MethodRoutes { * Marks the slot of each path in `statics`, so that most request paths for * routes with params skip hashing to look there. Building it costs more * than a cold start gains back, so it waits for the tree to serve - * `FILTER_AFTER` requests, and is dropped when a path is added to `statics`. + * `FILTER_AFTER` requests. */ filter: Uint8Array | undefined /** The requests the tree has served without `filter`. */ @@ -268,7 +268,12 @@ function addStatic( const statics = routes.statics if (!statics.has(path)) { statics.set(path, data) - routes.filter = undefined + // Marking the slot, rather than dropping the filter for the next request to + // build again, keeps adding routes between requests, as lazy routers do, + // from costing a pass over every static path each time. + if (routes.filter !== undefined) { + routes.filter[slot(path)] = 1 + } } } diff --git a/packages/router/tests/late-adds.test.ts b/packages/router/tests/late-adds.test.ts index e9167854..94b37959 100644 --- a/packages/router/tests/late-adds.test.ts +++ b/packages/router/tests/late-adds.test.ts @@ -146,7 +146,7 @@ describe('routes added after requests', () => { router.match('GET', `/users/${id}`) } - // once a tree request rebuilds the filter of static paths, these need it to hold them + // once the tree has served enough requests to build the filter of static paths, these need it to hold them router.add('GET', '/users/me', '/users/me') router.add('GET', '/teams/{id}', '/teams/{id}') router.add('GET', '/users/{id}/posts', '/users/{id}/posts') From 8b5ae27be07f815bc27cf6e913086159bacbdb75 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 2 Nov 2026 01:20:50 +0000 Subject: [PATCH 2/3] perf(router): spread static filter slots by length * 127 plus the last char The filter slot was the length modulo 8 next to the last char, so paths 8 apart in length shared a slot whenever they ended in the same char. On the GitHub, Stripe and Kubernetes path sets, 20-38% of param requests passed the filter into a wasted statics lookup; with this slot 8-22% do. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VVaw7PzysACDLzaReKDUKE --- packages/router/src/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/router/src/index.ts b/packages/router/src/index.ts index ae0627d7..348a1156 100644 --- a/packages/router/src/index.ts +++ b/packages/router/src/index.ts @@ -535,9 +535,14 @@ function toSegment(text: string, index: number): Segment { return decode(text) } -/** Paths that share a slot have the same length modulo 8 and last char modulo 128. */ +/** + * Spreads paths over 1024 slots by their length and last char. Paths whose + * lengths differ by 8 land 8 slots apart, where a step of 128 per length would + * put them in the same slot for the same last char, as happens to many paths + * of real APIs. + */ function slot(path: string): number { - return ((path.length & 7) << 7) | (path.charCodeAt(path.length - 1) & 127) + return (path.length * 127 + path.charCodeAt(path.length - 1)) & 1023 } function trimTrailingSlash(path: string): string { From 593fdfebadf1f25718245b80587fb1064c533a4e Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 2 Nov 2026 07:20:02 +0000 Subject: [PATCH 3/3] test(router): repeat the requests of each match bench so a compile can't land in CodSpeed's measured call Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VVaw7PzysACDLzaReKDUKE --- packages/router/benches/router.bench.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/router/benches/router.bench.ts b/packages/router/benches/router.bench.ts index 23e57ca2..c55276d4 100644 --- a/packages/router/benches/router.bench.ts +++ b/packages/router/benches/router.bench.ts @@ -15,6 +15,12 @@ import { // rou3 runs as is, without the percent-decoding oRPC does around it today, // so each comparison is against less work than this router does. +// CodSpeed measures one call of each bench after a few warmup calls. Repeating +// the few requests of a match bench in each call lets V8 finish compiling them +// during the warmup, so a compile can't land in the measured call and swing its +// result many times. +const REPEAT = 16 + function createOurs(routes: Routes): Router { const router = new Router() for (const [method, path] of routes) { @@ -37,14 +43,18 @@ function compare(name: string, routes: Routes, requests: Requests): void { const rou3 = createRou3(routes) bench('@openapi-spec/router', () => { - for (const [method, pathname] of requests) { - ours.match(method, fresh(pathname)) + for (let i = 0; i < REPEAT; i++) { + for (const [method, pathname] of requests) { + ours.match(method, fresh(pathname)) + } } }) bench('rou3', () => { - for (const [method, pathname] of requests) { - findRoute(rou3, method, fresh(pathname)) + for (let i = 0; i < REPEAT; i++) { + for (const [method, pathname] of requests) { + findRoute(rou3, method, fresh(pathname)) + } } }) })