Repository navigation
perf(route): match routes by walking a trie of route segments - #7939
FarhanAliRaza wants to merge 1 commit into
Conversation
get_router tried each route's regex in turn until one matched, so resolving a path the router had not seen cost one regex call per route: about 1.2, 5.4 and 49 us for 10, 100 and 1000 routes. Each call also allocated through glibc, about half of test_route_matching's measured time, and the allocator's heap state made that benchmark flip by about 7% between commits on CodSpeed. Routes now go into a trie with one level per path segment: static children keyed by the ASCII-folded segment, plus one child each for [arg] and [[arg]] and a slot for [[...splat]]. A route keeps the rank the regex scan tried it in (route_specificity, ties in order given), and a lookup returns the lowest-ranked route the path matches, skipping any branch whose best rank cannot win. A new path takes about 0.5 us at any table size. Matching stays exactly what the regexes did: ASCII-only case folding, one trailing slash allowed on top of the one stripped, [arg] matching an empty segment, [[arg]] matching nothing or one non-empty segment, and a catch-all stopping at a newline as the regex dot did. A test checks the trie against the regex scan on 24,000 random route tables and paths. A catch-all that is not the last segment now raises, as verify_route_validity already did for every page route. test_route_matching keeps only its 1000-route case, as a guard against a return to scanning, with 400 lookups per measured call. Claude-Session: https://claude.ai/code/session_01LwJUCWYQL5BocrPjnH5hFX
[Medium impact] The PR appears safe to merge. SummaryReplaces the backend’s route-by-route regex scan with a tree of path segments while preserving route priority and cached lookups.
No actionable issues found. Reviews (1) · Last reviewed commit: "Match routes by walking a trie of route ..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 5 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
Merging this PR will improve performance by ×132 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
|
The one red check,
Re-running the failed job isn't available to this session (GitHub returns 403), so it needs a manual re-run. Generated by Claude Code |
All Submissions:
Type of change
Describe your changes
get_routerresolved a path by trying each route's regex in turn until one matched. Every event calls the router (EventNamespace.on_event,App.get_load_events). Anlru_cachemakes repeat paths a dictionary hit, but each new path paid one regex call per route.It also kept
test_route_matchingflaky after #7878. CodSpeed runs Python withPYTHONMALLOC=malloc, and everyfullmatchallocates through glibc. That was about half the benchmark's measured time, and glibc's heap state moved it between 90 and 96.8 ms. 24 of the 59 CodSpeed reports on PRs based after #7878 flag it, including docs-only and README-only PRs.The trie. Following the radix-trie design of routers like Rust's
matchit:[arg]and one for[[arg]], plus a slot for a[[...splat]]route.route_specificity, ties in the order given.Unlike
matchit, it doesn't let a static segment win at each position. The winner is still the route the regex scan picked, soon_loadevents resolve exactly as before.Same matching rules as the regexes:
/\N{KELVIN SIGN}itstill doesn't matchkit).[arg]can match an empty segment.[[arg]]matches nothing or one non-empty segment..did./indexresolves to the index route.test_get_router_matches_the_regex_scankeeps the old regex scan as a reference, built from the unchanged publicget_route_regexandroute_specificity. It checks that both pick the same route on 24,000 seeded random route tables and paths: mixed case, non-ASCII, empty segments, extra slashes,/indexand newlines.One behavior change: a
[[...splat]]that isn't the route's last segment now raisesValueErroringet_router.verify_route_validityalready rejects such routes for every page added to an app, so only a directget_routercall with an invalid route sees it.Benchmark.
test_route_matchingkeeps only its 1000-route case, as a guard against going back to scanning every route, and now does 400 lookups per measured call (235 µs uninstrumented).PYTHONMALLOC=malloc, fixed cache sizes, no address randomization), changing only the size of the environment moved the old benchmark's instruction count by up to 3.7%.New Feature Submission:
Changes To Core Features:
tests/units/test_route.py,tests/units/test_app.pyandtests/units/utils/test_exec.pypass, as do ruff and pyright on the changed files.tests/units/reflex_bench/drivers/test_browser.pyneed a Chromium build this container doesn't have.tests/units/components/graphing/test_plotly.pyalso fail on unmodifiedmainlocally.https://claude.ai/code/session_01LwJUCWYQL5BocrPjnH5hFX
Generated by Claude Code