perf(router): settle route ranking when the table is built, not per request
Route matching scanned every method-matching route on every request and ranked the
candidates as it went, calling `moreSpecific` — two `compilePattern` lookups each — per
candidate. Both the scan and the ranking scale with the size of the route table, and the
answer they compute cannot change between requests: the table is assembled once, after the
graph is prepared, and handed to the adapter unchanged.
So the ranking moves to where the table is built. Routes are compiled, bucketed by method
and sorted most-specific-first, and matching walks until the first match and returns it.
`sort` is stable, so patterns of equal specificity keep registration order — which is what
the old scan produced by refusing to replace its best on a tie.
Measured on a request to a parameterised route registered last, against the same tree
without these changes, medians of five and seven rounds:
6 routes no change (every row overlaps)
50 routes +3.3%, +3.5% across two runs
200 routes +14.9%, +12.0% — twelve rounds, not one crossing sample
The shape is the point rather than any single number: the saving is proportional to how
many candidates a request walks past, so it is invisible on a small table by construction.
That is also why it went unnoticed. The benchmark had no route-table-width dimension until
this commit added one, so every matcher change measured as noise no matter how real.
Two smaller savings on the same path. `decodeSegment` now skips `decodeURIComponent` when
the segment holds no `%`, the same gate `normalizeRequestPath` already applies to the whole
path for the same reason. And decoding memoizes per request rather than per candidate: a
scan reaching the same parameter position on several routes used to decode the same string
once per route, and position is all decoding depends on.
`allStatic` is deleted. It short-circuited the scan on an all-literal match, which sorting
subsumes — such a pattern now sorts first and wins by position.
The index is keyed by the identity of the route array, so it is built once and collected
with the table it describes. A table assembled fresh — mesh splicing a teapot's exports in
at boot — is a different array and gets its own index, so no caller has to invalidate
anything.
`matchRoute precedence` already covered the ordering this rewrite had to preserve, and it
passes unchanged. Two cases were missing and are added: that equal specificity keeps
registration order, which is the property the stable sort now carries, and that a parameter
decodes to the same value however many candidates the scan walks. Both pass against the
previous implementation too — they describe behaviour that already existed, which is what
makes them evidence the rewrite preserved it rather than evidence of the change.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+97
-34
No files matched your search
@@ -119,6 +119,23 @@ describe('matchRoute precedence', () => {
|
||||
expect(matchRoute(routes, 'GET', '/users/diego')?.def.pattern).toBe('/users/:value');
|
||||
});
|
||||
|
||||
it('keeps registration order between patterns of equal specificity', () => {
|
||||
// The tie-break the ordering rests on. Ranking is settled once when the table is built, and the
|
||||
// sort that settles it is stable — which is what preserves the behaviour the scan used to get
|
||||
// by refusing to replace its best on an equal rank. A sort that reordered equals would change
|
||||
// which of two same-shape routes answers, silently and only for the ones that collide.
|
||||
const routes = [route('GET', '/a/:first'), route('GET', '/a/:second')];
|
||||
expect(matchRoute(routes, 'GET', '/a/x')?.def.pattern).toBe('/a/:first');
|
||||
expect(matchRoute([...routes].reverse(), 'GET', '/a/x')?.def.pattern).toBe('/a/:second');
|
||||
});
|
||||
|
||||
it('decodes a parameter once per position, however many candidates the scan walks', () => {
|
||||
// Not an optimisation detail: the decoded value each candidate sees must be the same one, so
|
||||
// memoising per request cannot change which route matches or what it receives.
|
||||
const routes = [route('GET', '/a/:x/zzz'), route('GET', '/a/:y/b')];
|
||||
expect(matchRoute(routes, 'GET', '/a/a%20b/b')?.params).toEqual({ y: 'a b' });
|
||||
});
|
||||
|
||||
it('returns undefined when no pattern matches the path', () => {
|
||||
expect(matchRoute([route('GET', '/a')], 'GET', '/b')).toBeUndefined();
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user