perf(router): reset the walk stack only when a lookup throws (#109) #131

Merged
chrnx merged 1 commits from fix/router-walk-stack-reset into develop 2026-10-12 20:10:46 +00:00
Owner

Fixes a lookup regression from a2b71e2 (the last review fix in #128). CARD-10's formal D4 run (#130) found it.

Cause

a2b71e2 reset stackNodes.length and stackDepths.length to base in a finally on every lookup. #128's description called that "off the hot path", but it was not measured, and it is not. Writing length drops the arrays' backing storage, so every lookup that walks the tree pays to reallocate it. Static hits use the exact-path map and never walk, so they were unaffected.

Fix

A normal walk already ends with both stacks at base. The reset now runs only in a catch, which then rethrows. The test added in a2b71e2 ("radix index: malformed paths") still passes: the stack is empty after repeated URIErrors, and a later lookup still matches.

Evidence (jarvis)

node:22, cpuset 6,7,14,15, LOOKUP_RUNS=8 × 21 = 168 paired rounds per cell, bootstrap 95% CI. All four routers ran interleaved in one run: the oracle; 5e0ae94, the walk before a2b71e2; cb123e3, current develop; and this branch. Load was 0.77 at start and 1.02 at end.

routes workload this PR / oracle 5e0ae94 / oracle develop / oracle
10 late param hit +9.2% (+7.4 … +10.5) +10.8% (+10.5 … +11.2) +50.0% (+48.1 … +52.9)
10 method miss −6.0% (−6.5 … −5.8) −4.1% +32.6%
10 path miss −1.0% (−1.4 … −0.3) −0.0% +103.7%
100 path miss −80.0% −79.9% −59.2%
500 late param hit −86.9% −86.6% −82.5%
500 path miss −96.0% −96.0% −92.1%

This PR matches or beats 5e0ae94 on every cell. Static hits were within noise for all three routers.

The 10-route late-param cell is still above +5% for all three routers, including 5e0ae94. #130 measured an A/A noise band of +8.3% (+7.1 … +9.1) for that same cell against a byte-identical copy of the oracle. Whether D4 accounts for that bias is the CARD-10 decision; this PR does not settle it.

Gates

gate result
lint 0 errors
format clean
npm test 813 pass
npm run parity:mutants 21/21 killed
build ok
Deno 36 passed
Bun 26 pass
edge PASS
npm audit --audit-level=moderate 0
Fixes a lookup regression from `a2b71e2` (the last review fix in #128). CARD-10's formal D4 run (#130) found it. ## Cause `a2b71e2` reset `stackNodes.length` and `stackDepths.length` to `base` in a `finally` on every lookup. #128's description called that "off the hot path", but it was not measured, and it is not. Writing `length` drops the arrays' backing storage, so every lookup that walks the tree pays to reallocate it. Static hits use the exact-path map and never walk, so they were unaffected. ## Fix A normal walk already ends with both stacks at `base`. The reset now runs only in a `catch`, which then rethrows. The test added in `a2b71e2` ("radix index: malformed paths") still passes: the stack is empty after repeated `URIError`s, and a later lookup still matches. ## Evidence (jarvis) `node:22`, cpuset 6,7,14,15, `LOOKUP_RUNS=8` × 21 = 168 paired rounds per cell, bootstrap 95% CI. All four routers ran interleaved in one run: the oracle; `5e0ae94`, the walk before `a2b71e2`; `cb123e3`, current `develop`; and this branch. Load was 0.77 at start and 1.02 at end. | routes | workload | this PR / oracle | `5e0ae94` / oracle | `develop` / oracle | | ---: | --- | --- | --- | --- | | 10 | late param hit | +9.2% (+7.4 … +10.5) | +10.8% (+10.5 … +11.2) | +50.0% (+48.1 … +52.9) | | 10 | method miss | −6.0% (−6.5 … −5.8) | −4.1% | +32.6% | | 10 | path miss | −1.0% (−1.4 … −0.3) | −0.0% | +103.7% | | 100 | path miss | −80.0% | −79.9% | −59.2% | | 500 | late param hit | −86.9% | −86.6% | −82.5% | | 500 | path miss | −96.0% | −96.0% | −92.1% | This PR matches or beats `5e0ae94` on every cell. Static hits were within noise for all three routers. The 10-route late-param cell is still above +5% for all three routers, including `5e0ae94`. #130 measured an A/A noise band of +8.3% (+7.1 … +9.1) for that same cell against a byte-identical copy of the oracle. Whether D4 accounts for that bias is the CARD-10 decision; this PR does not settle it. ## Gates | gate | result | | --- | --- | | lint | 0 errors | | format | clean | | `npm test` | 813 pass | | `npm run parity:mutants` | 21/21 killed | | build | ok | | Deno | 36 passed | | Bun | 26 pass | | edge | PASS | | `npm audit --audit-level=moderate` | 0 |
chrnx added 1 commit 2026-10-12 19:28:18 +00:00
perf(router): reset the walk stack only when a lookup throws
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 16s
CI / build (pull_request) Successful in 26s
CI / runtimes (edge) (pull_request) Successful in 14s
CI / runtimes (deno) (pull_request) Successful in 19s
1b30f15c02
The finally block added in a2b71e2 wrote both stacks' length on every
lookup, which drops their backing storage and made every walked lookup
pay for reallocation: +50% late param hit, +33% method miss and +104%
path miss at 10 routes on jarvis (CARD-10). A normal walk already ends
at base, so reset only in a catch and rethrow.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx merged commit d07d6add04 into develop 2026-10-12 20:10:46 +00:00
Sign in to join this conversation.