wayfind
A speedy, flexible router for Rust.
Currently in a pre-alpha state.
Why another router?
wayfind attempts to bridge the gap between existing Rust router options:
- fast routers, lacking in flexibility
- flexible routers, lacking in speed
Real-world projects often need fancy routing capabilities, such as projects ported from frameworks like Ruby on Rails, or those adhering to specifications like the Open Container Initiative (OCI) Distribution Specification.
The goal of wayfind is to remain competitive with the fastest libraries, while offering advanced routing features when needed. Unused features shouldn't impact performance - you only pay for what you use.
Features
Dynamic Routing
Dynamic parameters allow matching for any byte, excluding the path delimiter /.
We support both:
- Whole segment parameters:
/{name}/ - Inline parameters:
/{year}-{month}-{day}/
Inline dynamic parameters are greedy in nature, similar to a regex .*, and will attempt to match as many bytes as possible.
Example
use Error;
use ;
Wildcard Routing
Wildcard parameters enable matching of one or more segments within a path.
We support both:
- mid-route wildcards:
/api/{*path}/help - end-route catch-all:
/{*catch_all}
Example
use Error;
use ;
Constraints
Constraints allow for custom logic to be injected into the routing process.
We support constraints for all types of parameters:
- Dynamic constraint:
/{name:constraint} - Wildcard constraint:
/{*name:constraint}
The typical use-case for constraints would be to run a regex, or a simple FromStr implementation, against a path segment.
A common mistake would be to use these for validation of parameters. This should be avoided.
If a constraint fails to match, and no other suitable match exists, it results in a Not Found response, rather than any sort of Bad Request.
They act as an escape-hatch for when you need to disambiguate routes.
The current constraint implementation has a number of limitations:
- constraints cannot take parameters
- checks cannot make use of any prior state
- checks cannot store data after a successful check
Example
use Error;
use ;
;
User-Friendly Error Messages
Where possible, we try to provide user-friendly error messages.
Example
use Error;
use ;
const ERROR_DISPLAY: &str = "
duplicate constraint name
The constraint name 'my_constraint' is already in use:
- existing constraint type: 'rust_out::ConstraintA'
- new constraint type: 'rust_out::ConstraintB'
help: each constraint must have a unique name
try:
- Check if you have accidentally added the same constraint twice
- Ensure different constraints have different names
";
;
;
Router Display
Routers can print their routes as an tree diagram.
[*] here represents nodes within the route tree that can be matched against.
Example
use Error;
use Router;
const ROUTER_DISPLAY: &str = "
$
╰─ /
├─ pet [*]
│ ╰─ /
│ ├─ findBy
│ │ ├─ Status [*]
│ │ ╰─ Tags [*]
│ ╰─ {petId} [*]
│ ╰─ /uploadImage [*]
├─ store/
│ ├─ inventory [*]
│ ╰─ order [*]
│ ╰─ /
│ ╰─ {orderId} [*]
╰─ user [*]
╰─ /
├─ createWithList [*]
├─ log
│ ├─ in [*]
│ ╰─ out [*]
╰─ {username} [*]
";
Performance
wayfind is fast, and appears to be competitive against other top performers in all benchmarks we currently run.
This is due to a number of reasons:
- use of recursion, rather than manual walking of a tree, which seems to perform better.
- use of
smallvec, allowing for storage of small parameter lists on the stack. - enforcement of UTF-8 upfront, which can prevent duplicate UTF-8 checks internally while extracting parameters (via
unsafeusage).
However, as is often the case, your mileage may vary (YMMV). Benchmarks, especially micro-benchmarks, should be taken with a grain of salt.
Benchmarks
All benchmarks ran on a M1 Pro laptop.
Check out our codspeed results for a more accurate set of timings.
Context
For all benchmarks, we percent-decode the path before matching. After matching, we convert any extracted parameters to strings.
Some routers perform these operations automatically, while others require them to be done manually.
We do this to try and match behaviour as best as possible. This is as close to an "apples-to-apples" comparison as we can get.
matchit inspired benches
In a router of 130 routes, benchmark matching 4 paths.
| Library | Time | Alloc Count | Alloc Size | Dealloc Count | Dealloc Size |
|---|---|---|---|---|---|
| wayfind | 393.27 ns | 4 | 265 B | 4 | 265 B |
| matchit | 457.42 ns | 4 | 416 B | 4 | 448 B |
| xitca-router | 565.32 ns | 7 | 800 B | 7 | 832 B |
| path-tree | 586.64 ns | 4 | 416 B | 4 | 448 B |
| ntex-router | 1.7999 µs | 18 | 1.248 KB | 18 | 1.28 KB |
| route-recognizer | 4.6049 µs | 160 | 8.515 KB | 160 | 8.547 KB |
| routefinder | 6.4261 µs | 67 | 5.024 KB | 67 | 5.056 KB |
| actix-router | 21.356 µs | 214 | 13.93 KB | 214 | 13.96 KB |
path-tree inspired benches
In a router of 320 routes, benchmark matching 80 paths.
| Library | Time | Alloc Count | Alloc Size | Dealloc Count | Dealloc Size |
|---|---|---|---|---|---|
| wayfind | 5.7827 µs | 59 | 2.567 KB | 59 | 2.567 KB |
| matchit | 8.9428 µs | 140 | 17.81 KB | 140 | 17.83 KB |
| path-tree | 9.5266 µs | 59 | 7.447 KB | 59 | 7.47 KB |
| xitca-router | 10.871 µs | 209 | 25.51 KB | 209 | 25.53 KB |
| ntex-router | 30.850 µs | 201 | 19.54 KB | 201 | 19.56 KB |
| route-recognizer | 91.922 µs | 2872 | 191.8 KB | 2872 | 205 KB |
| routefinder | 99.100 µs | 525 | 48.4 KB | 525 | 48.43 KB |
| actix-router | 178.52 µs | 2201 | 128.8 KB | 2201 | 128.8 KB |
Minimum Supported Rust Version (MSRV)
The MSRV is 1.66.
License
wayfind is licensed under the terms of both the MIT License and the Apache License (Version 2.0).
Inspirations
- poem: Initial experimentations started out as a Poem router fork
- matchit: Performance leader among pre-existing routers
- path-tree: Extensive testing and router display feature
- ASP.NET Core: Constraints-based approach to routing