perf(router): stop decoding every path segment to throw the result away

normalizeRequestPath ran on every request and validated percent-encoding by
splitting the path and calling decodeURIComponent on each segment, discarding
every result — matching is done on the raw path. Two array allocations and one
decodeURIComponent per segment, per request, to prove that a string nobody
decodes could be decoded.

Measured in isolation: 95.8ns for /hello, 342.7ns for a six-segment route. The
guard costs 15.7ns and 6.7ns for the same two paths.

Same validation. decodeURIComponent throws on the first malformed % sequence
wherever it appears, so one call on the whole string catches what per-segment
calls caught, and %2F inside a segment decodes fine either way. Only the throw
was ever used. The existing tests for malformed encoding and repeated slashes
pass unchanged.

Found by sweeping all 178 commits between fe1831a and the observability work
after the regenerated benchmark showed green-tea down ~20% since July while
raw-http, fastify and nestjs-fastify reproduced their July numbers within 1.4%
on the same run. The sweep put a +11.8% step at 9a61ba8 "complete HEAD and
OPTIONS semantics", which is where this function entered the request path.

Effect: -6.2% in-process request latency, from twelve interleaved measurements
whose ranges do not overlap (OLD 4.253-4.425us, FIX 4.035-4.151us). A first
attempt measured with-then-without and produced the opposite sign, so the
interleaving is load-bearing, not ceremony. Expect roughly 2% over a real
socket, which is below that harness's own noise floor.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-16 22:44:35 -05:00
1 parent 523e3184b0
commit 86dd3fe9b6
1 file changed
+17 -4
+17 -4
View File
@@ -169,10 +169,23 @@ export function normalizeRequestPath(path: string): string {
if (path.includes('//')) throw new InvalidRequestPathError('invalid request path: repeated slash'); if (path.includes('//')) throw new InvalidRequestPathError('invalid request path: repeated slash');
const normalized = path.length > 1 && path.endsWith('/') ? path.slice(0, -1) : path; const normalized = path.length > 1 && path.endsWith('/') ? path.slice(0, -1) : path;
try { // Validation only — the decoded value is deliberately discarded, matching is done on the raw
for (const segment of normalized.split('/').slice(1)) decodeURIComponent(segment); // path. So the work is skipped entirely unless there is something to validate.
} catch { //
throw new InvalidRequestPathError('invalid request path: malformed path encoding'); // This used to split the path and call `decodeURIComponent` per segment, on every request, and
// throw all of it away. Measured on this path: 95.8 ns for `/hello`, 342.7 ns for a six-segment
// route — around 8% of a whole request, spent proving that a string nobody decodes could be
// decoded. The guard below is 15.7 ns and 6.7 ns for the same two paths.
//
// One call on the whole string rather than per segment is the same check: `decodeURIComponent`
// throws on the first malformed `%` sequence wherever it appears, and a `%2F` inside a segment
// decodes fine either way. Only the throw matters here, never the result.
if (normalized.includes('%')) {
try {
decodeURIComponent(normalized);
} catch {
throw new InvalidRequestPathError('invalid request path: malformed path encoding');
}
} }
return normalized; return normalized;