feat(http/unstable): add radix tree router; keep linear scan as routeLinear#7075
feat(http/unstable): add radix tree router; keep linear scan as routeLinear#7075esroyo wants to merge 4 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7075 +/- ##
==========================================
- Coverage 94.61% 94.60% -0.01%
==========================================
Files 634 634
Lines 51799 51942 +143
Branches 9329 9376 +47
==========================================
+ Hits 49009 49141 +132
- Misses 2216 2223 +7
- Partials 574 578 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
786873d to
d522ffa
Compare
…Linear Add routeRadix(), a radix tree router that provides O(segments) dispatch for static, parametric, and wildcard routes. Routes with complex URLPattern syntax (regex constraints, optional groups, inline wildcards, modifier suffixes) fall back to linear matching while preserving insertion order. - routeRadix: radix tree with fallback to linear for complex patterns - routeLinear: the original linear scan, extracted as its own export - route: re-exported alias for routeRadix (backward compatible) The radix router matches routeLinear semantics exactly — insertion order is always respected, even when static and parametric routes overlap at the same tree depth. Benchmarks show 1.5–9x improvement on static/parametric/wildcard routes, with negligible overhead on complex fallback patterns.
Replace the per-request `radixCandidates` array + `Array#sort` + merge with a single-slot match using `minIndex` pruning and inline pathname iteration. No candidate array, segments array, or sort closure on the hot path. Each `RouteNode` now tracks `minIndex` (lowest insertion index in the subtree) and `hasStaticChildren` (skip the substring slice when there are no static children). Fallback routes registered before the lowest radix index are scanned first so their hits prune the tree walk.
…allback URLPattern requires reserved characters (`+`, `?`, `*`) to be backslash-escaped to match literally. The escape is preserved in `pattern.pathname` (e.g. `/c\+\+`), so the radix tree was inserting such patterns as static keys that could never match the unescaped request path (`/c++`), making those routes unreachable. Treat any segment containing a backslash as complex so URLPattern remains the authoritative matcher via the linear fallback. Adds a regression test covering both single (`\+`) and consecutive (`\+\+`) escape forms.
The radix router is brand new; keep `route` bound to the better-tested `routeLinear` while it matures. Users who want the radix optimizations opt in via the `routeRadix` named export.
8b90273 to
632ecdc
Compare
|
@bartlomieju Thank you for the insightful comments! I mostly agree with your feedback (except the Array#sort comment perhaps) and have addressed all the points you raised. Thanks so much for your time ❤️ |
|
[updated] ---
config:
themeVariables:
xyChart:
plotColorPalette: "#e05c5c, #4a90d9"
---
xychart-beta
title "AMD Ryzen 7 PRO 6850U — route benchmark"
x-axis [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y-axis "Avg latency (µs)" 0 --> 6
line [0.92, 1.96, 5.86, 1.63, 5.6, 1.53, 2.72, 1.06, 3.17, 0.93, 1.46, 1.89]
line [1, 1.12, 1.07, 0.43, 0.43, 1.18, 1.31, 0.54, 1.39, 1.05, 1.44, 1.88]
🔴
|
lunadogbot
left a comment
There was a problem hiding this comment.
Bartlomieju's review points are addressed across four commits: route keeps linear-scan semantics (export { routeLinear as route }), per-request radixCandidates/sort replaced with minIndex subtree pruning in RouteNode, backslash escapes route through the linear fallback, parsePathname now documents the Request#url scoping, and the routeRadix JSDoc explicitly calls out that the tree is keyed by pathname only and other URLPattern constraints get validated by pattern.exec per request.
The walk's static→param→wildcard recursion order doesn't compromise "first registered wins": tryRoute rejects any candidate where r.index >= state.bestIndex, and the param route registered before static route preserves insertion order step at http/unstable_route_test.ts:438 exercises exactly that case. Running the full test harness against both routeRadix and routeLinear (lines 461-462) is the strongest behavioural parity signal.
- nit:
Route's@linkcode routeathttp/unstable_route.ts:31still names a single function.Routeis shared by bothrouteRadixandrouteLinear, so linking to one is still slightly misleading even withroutealiasingrouteLinear.
|
@bartlomieju this is ready to merge |
Add
routeRadix(), a radix tree router that provides O(segments) dispatch for static, parametric, and wildcard routes. Routes with complex URLPattern syntax (regex constraints, optional groups, inline wildcards, modifier suffixes) fall back to linear matching while preserving insertion order.routeRadix: radix tree with fallback to linear for complex patternsrouteLinear: the original linear scan, extracted as its own exportroute: re-exported alias forrouteLinearThe radix router matches routeLinear semantics exactly; insertion order is always respected, even when static and parametric routes overlap at the same tree depth.
Benchmarks show 1.5–9x improvement on static/parametric/wildcard routes, with negligible overhead on complex fallback patterns.
--- config: themeVariables: xyChart: plotColorPalette: "#e05c5c, #4a90d9" --- xychart-beta title "route: 🔴 linear vs 🔵 radix - Avg latency (µs)" x-axis [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] y-axis "Avg latency (µs)" 0 --> 7 line [0.91, 2.04, 6.1, 1.63, 5.54, 1.49, 2.84, 1.08, 3.15, 0.9, 1.45, 1.98] line [1.08, 1.38, 1.39, 0.62, 0.61, 1.55, 1.66, 0.91, 1.73, 1.46, 1.62, 2.1]