Skip to content

Commit 8e11ec5

Browse files
dinwwwhclaude
andauthored
refactor(router): lower the shared-parts count once per braced part (#201)
<!-- claude-projects-attribution: {"github_login":"dinwwwh"} --> _Requested by **dinwwwh** · [project thread](https://claude.ai/code/project/chan_01UcKMz91tMarPV1kWLVgLYE?thread=cmsg_01UcKMz91tMarPV1kWLVgLYE6swgSUM9SyULmBVdRjY2t7)_ ## What `expand` tracks how many of the previous route's parts the next route can reuse (`literals`). It used to lower that count in two places: once where a route waits, and once where it adds a param. Both happen only for a part that starts with `{` once decoded, so the count is now lowered in one place, right after that check. That check also catches a part such as `{a.b}`, which starts with `{` but is literal text. The count now drops for that part too, so the next route shares fewer parts with this one and parses them again. Matching is unaffected. The comment on `shared` and `literals` now describes the rule this way. ## Measurements Instruction counts from callgrind, `node --single-threaded`, averaged over hash seeds 1 and 2: | | before | after | |---|---|---| | min / min+gzip | 5012 / 2092 B | 5001 / 2091 B | | REST cold+3 match | 3.498M | 3.485M (−0.35%) | | REST cold+3, one request per method | 5.075M | 5.064M (−0.2%) | | REST cold+10 | 6.010M | 5.998M (−0.2%) | `expand` only runs while the tree is being built, so steady-state matching does not change. ## Checks - Tests pass with 100% coverage. - The model fuzzers, including the late-add ones, find 0 diffs. - Stryker finds 66 survivors, down from 67, and none of them are new. The `if (true)` mutant of the merged check is killed by the late-add test added in #195. --- _Generated by [Claude Code](https://claude.ai/code/session_01VVaw7Sutbw6xMTMyDFeNfw)_ Co-authored-by: Claude <noreply@anthropic.com>
1 parent f18cd06 commit 8e11ec5

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

‎packages/router/src/index.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ function indexStatics<T>(routes: MethodRoutes<T>): void {
371371
function expand<T>(node: Node<T>, pending: unknown[], start: number, whole: boolean): void {
372372
node.pending = undefined
373373
// The parts of the previous route, how many of them come before its first
374-
// param or wait, and the node after each of those, so that the next route
375-
// skips parsing the parts it shares with them.
374+
// part starting with `{` once decoded, and the node after each of those, so
375+
// that the next route skips parsing the parts it shares with them.
376376
let shared: string[] = []
377377
let literals = 0
378378
const nodes: Node<T>[] = [node]
@@ -399,10 +399,10 @@ function expand<T>(node: Node<T>, pending: unknown[], start: number, whole: bool
399399
// which keeps the first of two that read the same. A route added whole
400400
// goes after the routes waiting there.
401401
if (segment[0] === '{') {
402+
if (index < literals) {
403+
literals = index
404+
}
402405
if (!whole && index > start) {
403-
if (index < literals) {
404-
literals = index
405-
}
406406
;(child.pending ??= []).push(parts, data, params)
407407
continue next
408408
}
@@ -418,9 +418,6 @@ function expand<T>(node: Node<T>, pending: unknown[], start: number, whole: bool
418418
continue
419419
}
420420

421-
if (index < literals) {
422-
literals = index
423-
}
424421
// An array literal holds just its items, where a push to `[]` reserves 17.
425422
if (params === undefined) {
426423
params = [segment]

0 commit comments

Comments
 (0)