Skip to content

feat!: Use a radix trie for route matching - #628

Draft
ElijahAhianyo wants to merge 14 commits into
masterfrom
elijah/router-trie
Draft

feat!: Use a radix trie for route matching#628
ElijahAhianyo wants to merge 14 commits into
masterfrom
elijah/router-trie

Conversation

@ElijahAhianyo

@ElijahAhianyo ElijahAhianyo commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Description

The current router implementation uses a Vec to store routers, which has some real limitations. For example, using a vec meant route conflict detection was cumbersome and hacky to get right. Using a Radix trie is the right data structure for this problem. This PR delegates the core Trie logic to the matchit crate. We still keep our business logic in a light wrapper over the Matchit Router.

Breaking Changes

  1. Registering a route with a duplicate parameter at the same segment will now fail
Router::with_urls([
    Route::with_handler_and_name("/foo/{bar}", index, "index"),
        Route::with_handler_and_name("/foo/{baz}", add_example_form, "add"),
])
  1. Registering 2 views with the same url_prefix will fail
struct TestProject;
impl Project for TestProject {
   ...
    fn register_apps(&self, apps: &mut AppBuilder, _context: &RegisterAppsContext) {
        apps.register_with_views(App1, "");
        apps.register_with_views(App2, "");
    }
}
  1. When a handler and router are registered for the same route, the handler will take precedence at lookup time
let nested_router = Router::with_urls([Route::with_handler_and_name(
        "/inner/{id}",
        nested,
       "nested",
)]);

Router::with_urls([
    Route::with_handler_and_name("/foo", index, "index"),
    Route::with_router("/foo", nested_router),
])

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Refactor / cleanup
  • Performance improvement
  • Other (describe above)

@github-actions github-actions Bot added the C-lib Crate: cot (main library crate) label Aug 1, 2026
@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown

🐰 Bencher Report

Projectcot
Branchelijah/router-trie
Testbedgithub-ubuntu-latest
Click to view all benchmark results
BenchmarkLatencyBenchmark Result
microseconds (µs)
(Result Δ%)
Upper Boundary
microseconds (µs)
(Limit %)
empty_router/empty_router📈 view plot
🚷 view threshold
14,227.00 µs
(+66.11%)Baseline: 8,564.87 µs
16,300.96 µs
(87.28%)
json_api/json_api📈 view plot
🚷 view threshold
1,106.60 µs
(+5.25%)Baseline: 1,051.42 µs
1,373.79 µs
(80.55%)
nested_routers/nested_routers📈 view plot
🚷 view threshold
1,075.10 µs
(+9.33%)Baseline: 983.37 µs
1,257.69 µs
(85.48%)
single_root_route/single_root_route📈 view plot
🚷 view threshold
999.84 µs
(+5.60%)Baseline: 946.79 µs
1,219.97 µs
(81.96%)
single_root_route_burst/single_root_route_burst📈 view plot
🚷 view threshold
17,725.00 µs
(+3.69%)Baseline: 17,094.45 µs
21,564.70 µs
(82.19%)
🐰 View full continuous benchmarking report in Bencher

@github-actions github-actions Bot added the A-deps Area: Dependencies label Aug 17, 2026
@ElijahAhianyo

Copy link
Copy Markdown
Contributor Author

Should be rebased on and merged after #586

@ElijahAhianyo ElijahAhianyo changed the title Use a radix trie for route matching feat!: Use a radix trie for route matching Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-deps Area: Dependencies C-lib Crate: cot (main library crate)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant