项目作者: ibraheemdev

项目描述 :
A blazing fast URL router and path matcher.
高级语言: Rust
项目地址: git://github.com/ibraheemdev/matchit.git
创建时间: 2020-12-21T16:55:26Z
项目社区:https://github.com/ibraheemdev/matchit

开源协议:Other

下载


matchit

crates.io
github
docs.rs

A high performance, zero-copy URL router.

  1. use matchit::Router;
  2. fn main() -> Result<(), Box<dyn std::error::Error>> {
  3. let mut router = Router::new();
  4. router.insert("/home", "Welcome!")?;
  5. router.insert("/users/{id}", "A User")?;
  6. let matched = router.at("/users/978")?;
  7. assert_eq!(matched.params.get("id"), Some("978"));
  8. assert_eq!(*matched.value, "A User");
  9. Ok(())
  10. }

Parameters

The router supports dynamic route segments. These can either be named or catch-all parameters.

Named parameters like /{id} match anything until the next static segment or the end of the path.

  1. let mut m = Router::new();
  2. m.insert("/users/{id}", true)?;
  3. assert_eq!(m.at("/users/1")?.params.get("id"), Some("1"));
  4. assert_eq!(m.at("/users/23")?.params.get("id"), Some("23"));
  5. assert!(m.at("/users").is_err());

Prefixes and suffixes within a segment are also supported. However, there may only be a single named parameter per route segment.

  1. let mut m = Router::new();
  2. m.insert("/images/img{id}.png", true)?;
  3. assert_eq!(m.at("/images/img1.png")?.params.get("id"), Some("1"));
  4. assert!(m.at("/images/img1.jpg").is_err());

Catch-all parameters start with * and match anything until the end of the path. They must always be at the end of the route.

  1. let mut m = Router::new();
  2. m.insert("/{*p}", true)?;
  3. assert_eq!(m.at("/foo.js")?.params.get("p"), Some("foo.js"));
  4. assert_eq!(m.at("/c/bar.css")?.params.get("p"), Some("c/bar.css"));
  5. // Note that this would lead to an empty parameter.
  6. assert!(m.at("/").is_err());

The literal characters { and } may be included in a static route by escaping them with the same character.
For example, the { character is escaped with {{ and the } character is escaped with }}.

  1. let mut m = Router::new();
  2. m.insert("/{{hello}}", true)?;
  3. m.insert("/{hello}", true)?;
  4. // Match the static route.
  5. assert!(m.at("/{hello}")?.value);
  6. // Match the dynamic route.
  7. assert_eq!(m.at("/hello")?.params.get("hello"), Some("hello"));

Routing Priority

Static and dynamic route segments are allowed to overlap. If they do, static segments will be given higher priority:

  1. let mut m = Router::new();
  2. m.insert("/", "Welcome!").unwrap(); // Priority: 1
  3. m.insert("/about", "About Me").unwrap(); // Priority: 1
  4. m.insert("/{*filepath}", "...").unwrap(); // Priority: 2

How does it work?

The router takes advantage of the fact that URL routes generally follow a hierarchical structure.
Routes are stored them in a radix trie that makes heavy use of common prefixes.

  1. Priority Path Value
  2. 9 \ 1
  3. 3 s None
  4. 2 |├earch\ 2
  5. 1 |└upport\ 3
  6. 2 blog\ 4
  7. 1 | └{post} None
  8. 1 | \ 5
  9. 2 about-us\ 6
  10. 1 | team\ 7
  11. 1 contact\ 8

This allows us to reduce the route search to a small number of branches. Child nodes on the same level of the tree are also
prioritized by the number of children with registered values, increasing the chance of choosing the correct branch of the first try.

Benchmarks

As it turns out, this method of routing is extremely fast. Below are the benchmark results matching against 130 registered routes.
You can view the benchmark code here.

  1. Compare Routers/matchit
  2. time: [2.4451 µs 2.4456 µs 2.4462 µs]
  3. Compare Routers/gonzales
  4. time: [4.2618 µs 4.2632 µs 4.2646 µs]
  5. Compare Routers/path-tree
  6. time: [4.8666 µs 4.8696 µs 4.8728 µs]
  7. Compare Routers/wayfind
  8. time: [4.9440 µs 4.9539 µs 4.9668 µs]
  9. Compare Routers/route-recognizer
  10. time: [49.203 µs 49.214 µs 49.226 µs]
  11. Compare Routers/routefinder
  12. time: [70.598 µs 70.636 µs 70.670 µs]
  13. Compare Routers/actix
  14. time: [453.91 µs 454.01 µs 454.11 µs]
  15. Compare Routers/regex
  16. time: [421.76 µs 421.82 µs 421.89 µs]

Credits

A lot of the code in this package was inspired by Julien Schmidt’s httprouter.