Skip to content

Commit

Permalink
Merge pull request #123 from DuskSystems/121-make-use-of-black_box-in…
Browse files Browse the repository at this point in the history
…-benchmarks

Add black_box to benchmarks
  • Loading branch information
CathalMullan authored Aug 23, 2024
2 parents 3d4608d + 5f95e62 commit f2929a0
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 342 deletions.
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[![crates.io](https://img.shields.io/crates/v/wayfind)](https://crates.io/crates/wayfind)
[![documentation](https://docs.rs/wayfind/badge.svg)](https://docs.rs/wayfind)
[![rust 1.66](https://img.shields.io/badge/rust-1.66-orange.svg)](https://img.shields.io/badge/rust-1.66-orange.svg)
[![license: MIT/Apache-2.0](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](LICENSE-MIT)
![license: MIT/Apache-2.0](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)

[![codspeed](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/DuskSystems/wayfind)
[![codecov](https://codecov.io/gh/DuskSystems/wayfind/graph/badge.svg?token=QMSW55438K)](https://codecov.io/gh/DuskSystems/wayfind)

Expand Down Expand Up @@ -148,29 +149,29 @@ In a router of 130 routes, benchmark matching 4 paths.

| Library | Time | Alloc Count | Alloc Size | Dealloc Count | Dealloc Size |
|:-----------------|----------:|------------:|-----------:|--------------:|-------------:|
| wayfind | 300.61 ns | 4 | 265 B | 4 | 265 B |
| matchit | 468.29 ns | 4 | 416 B | 4 | 448 B |
| xitca-router | 565.00 ns | 7 | 800 B | 7 | 832 B |
| path-tree | 573.81 ns | 4 | 416 B | 4 | 448 B |
| ntex-router | 1.7806 µs | 18 | 1.248 KB | 18 | 1.28 KB |
| route-recognizer | 4.6020 µs | 160 | 8.515 KB | 160 | 8.547 KB |
| routefinder | 6.5115 µs | 67 | 5.024 KB | 67 | 5.056 KB |
| actix-router | 21.123 µs | 214 | 13.93 KB | 214 | 13.96 KB |
| wayfind | 367.63 ns | 4 | 265 B | 4 | 265 B |
| matchit | 460.51 ns | 4 | 416 B | 4 | 448 B |
| xitca-router | 565.88 ns | 7 | 800 B | 7 | 832 B |
| path-tree | 588.71 ns | 4 | 416 B | 4 | 448 B |
| ntex-router | 1.8403 µs | 18 | 1.248 KB | 18 | 1.28 KB |
| route-recognizer | 4.5838 µs | 160 | 8.515 KB | 160 | 8.547 KB |
| routefinder | 6.4449 µs | 67 | 5.024 KB | 67 | 5.056 KB |
| actix-router | 21.180 µ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 | 4.0274 µs | 59 | 2.567 KB | 59 | 2.567 KB |
| matchit | 8.8889 µs | 140 | 17.81 KB | 140 | 17.83 KB |
| path-tree | 9.5200 µs | 59 | 7.447 KB | 59 | 7.47 KB |
| xitca-router | 10.920 µs | 209 | 25.51 KB | 209 | 25.53 KB |
| ntex-router | 30.716 µs | 201 | 19.54 KB | 201 | 19.56 KB |
| route-recognizer | 91.087 µs | 2872 | 191.8 KB | 2872 | 205 KB |
| routefinder | 98.884 µs | 525 | 48.4 KB | 525 | 48.43 KB |
| actix-router | 179.22 µs | 2201 | 128.8 KB | 2201 | 128.8 KB |
| wayfind | 5.1001 µs | 59 | 2.567 KB | 59 | 2.567 KB |
| matchit | 9.0166 µs | 140 | 17.81 KB | 140 | 17.83 KB |
| path-tree | 9.5755 µs | 59 | 7.447 KB | 59 | 7.47 KB |
| xitca-router | 10.885 µs | 209 | 25.51 KB | 209 | 25.53 KB |
| ntex-router | 30.400 µs | 201 | 19.54 KB | 201 | 19.56 KB |
| route-recognizer | 92.255 µs | 2872 | 191.8 KB | 2872 | 205 KB |
| routefinder | 98.727 µs | 525 | 48.4 KB | 525 | 48.43 KB |
| actix-router | 179.14 µs | 2201 | 128.8 KB | 2201 | 128.8 KB |

## Minimum Supported Rust Version (MSRV)

Expand Down
147 changes: 63 additions & 84 deletions benches/matchit_criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,168 +6,147 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion};
use matchit_routes::paths;
use percent_encoding::percent_decode;
use std::hint::black_box;

pub mod matchit_routes;

criterion_group!(benches, benchmark);
criterion_main!(benches);

fn benchmark(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("matchit benchmarks");

group.bench_function("matchit benchmarks/wayfind", |bencher| {
let mut wayfind = wayfind::router::Router::new();
let mut router = wayfind::router::Router::new();
for route in routes!(brackets) {
wayfind.insert(route, true).unwrap();
router.insert(route, true).unwrap();
}

bencher.iter(|| {
for route in paths() {
for route in black_box(paths()) {
let path = wayfind::path::Path::new(route).unwrap();
let search = wayfind.search(&path).unwrap();
let _ = search
.parameters
.iter()
.map(|p| (p.key, p.value))
.collect::<Vec<(&str, &str)>>();
let output = black_box(router.search(black_box(&path)).unwrap());
let _parameters: Vec<(&str, &str)> =
black_box(output.parameters.iter().map(|p| (p.key, p.value)).collect());
}
});
});

group.bench_function("matchit benchmarks/actix-router", |bencher| {
let mut actix = actix_router::Router::<bool>::build();
let mut router = actix_router::Router::<bool>::build();
for route in routes!(brackets) {
actix.path(route, true);
router.path(route, true);
}
let actix = actix.finish();
let router = router.finish();

bencher.iter(|| {
for route in paths() {
let route = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let mut path = actix_router::Path::new(route.as_ref());
actix.recognize(&mut path).unwrap();
let _ = path
.iter()
.map(|p| (p.0, p.1))
.collect::<Vec<(&str, &str)>>();
for route in black_box(paths()) {
let path = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let mut path = actix_router::Path::new(path.as_ref());
black_box(router.recognize(black_box(&mut path)).unwrap());
let _parameters: Vec<(&str, &str)> =
black_box(path.iter().map(|p| (p.0, p.1)).collect());
}
});
});

group.bench_function("matchit benchmarks/matchit", |bencher| {
let mut matchit = matchit::Router::new();
let mut router = matchit::Router::new();
for route in routes!(brackets) {
matchit.insert(route, true).unwrap();
router.insert(route, true).unwrap();
}

bencher.iter(|| {
for route in paths() {
let route = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let at = matchit.at(route.as_ref()).unwrap();
let _ = at
.params
.iter()
.map(|p| (p.0, p.1))
.collect::<Vec<(&str, &str)>>();
for route in black_box(paths()) {
let path = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let output = black_box(router.at(black_box(&path)).unwrap());
let _parameters: Vec<(&str, &str)> =
black_box(output.params.iter().map(|p| (p.0, p.1)).collect());
}
});
});

group.bench_function("matchit benchmarks/ntex-router", |bencher| {
let mut ntex = ntex_router::Router::<bool>::build();
let mut router = ntex_router::Router::<bool>::build();
for route in routes!(brackets) {
ntex.path(route, true);
router.path(route, true);
}
let ntex = ntex.finish();
let router = router.finish();

bencher.iter(|| {
for route in paths() {
let route = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let mut path = ntex_router::Path::new(route.as_ref());
ntex.recognize(&mut path).unwrap();
let _ = path
.iter()
.map(|p| (p.0, p.1))
.collect::<Vec<(&str, &str)>>();
for route in black_box(paths()) {
let path = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let mut path = ntex_router::Path::new(path.as_ref());
router.recognize(&mut path).unwrap();
let _parameters: Vec<(&str, &str)> =
black_box(path.iter().map(|p| (p.0, p.1)).collect());
}
});
});

group.bench_function("matchit benchmarks/path-tree", |bencher| {
let mut path_tree = path_tree::PathTree::new();
let mut router = path_tree::PathTree::new();
for route in routes!(colon) {
let _ = path_tree.insert(route, true);
let _ = router.insert(route, true);
}

bencher.iter(|| {
for route in paths() {
let route = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let route = path_tree.find(route.as_ref()).unwrap();
let _ = route
.1
.params_iter()
.map(|p| (p.0, p.1))
.collect::<Vec<(&str, &str)>>();
for route in black_box(paths()) {
let path = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let output = router.find(&path).unwrap();
let _parameters: Vec<(&str, &str)> =
black_box(output.1.params_iter().map(|p| (p.0, p.1)).collect());
}
});
});

group.bench_function("matchit benchmarks/route-recognizer", |bencher| {
let mut route_recognizer = route_recognizer::Router::new();
let mut router = route_recognizer::Router::new();
for route in routes!(colon) {
route_recognizer.add(route, true);
router.add(route, true);
}

bencher.iter(|| {
for route in paths() {
let route = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let recognize = route_recognizer.recognize(route.as_ref()).unwrap();
let _ = recognize
.params()
.iter()
.map(|p| (p.0, p.1))
.collect::<Vec<(&str, &str)>>();
for route in black_box(paths()) {
let path = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let output = router.recognize(&path).unwrap();
let _parameters: Vec<(&str, &str)> =
black_box(output.params().iter().map(|p| (p.0, p.1)).collect());
}
});
});

group.bench_function("matchit benchmarks/routefinder", |bencher| {
let mut routefinder = routefinder::Router::new();
let mut router = routefinder::Router::new();
for route in routes!(colon) {
routefinder.add(route, true).unwrap();
router.add(route, true).unwrap();
}

bencher.iter(|| {
for route in paths() {
let route = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let best_match = routefinder.best_match(route.as_ref()).unwrap();
let _ = best_match
.captures()
.iter()
.map(|p| (p.0, p.1))
.collect::<Vec<(&str, &str)>>();
for route in black_box(paths()) {
let path = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let output = router.best_match(&path).unwrap();
let _parameters: Vec<(&str, &str)> =
black_box(output.captures().iter().map(|p| (p.0, p.1)).collect());
}
});
});

group.bench_function("matchit benchmarks/xitca-router", |bencher| {
let mut xitca = xitca_router::Router::new();
let mut router = xitca_router::Router::new();
for route in routes!(colon) {
xitca.insert(route, true).unwrap();
router.insert(route, true).unwrap();
}

bencher.iter(|| {
for route in paths() {
let route = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let at = xitca.at(route.as_ref()).unwrap();
let _ = at
.params
.iter()
.map(|p| (p.0, p.1))
.collect::<Vec<(&str, &str)>>();
for route in black_box(paths()) {
let path = percent_decode(route.as_bytes()).decode_utf8().unwrap();
let output = router.at(&path).unwrap();
let _parameters: Vec<(&str, &str)> =
black_box(output.params.iter().map(|p| (p.0, p.1)).collect());
}
});
});

group.finish();
}

criterion_group!(benches, benchmark);
criterion_main!(benches);
Loading

0 comments on commit f2929a0

Please sign in to comment.