feat: Add router bench for guard check failures

This commit is contained in:
manuelgdlvh 2025-05-14 20:41:45 +02:00
parent ffa95b7f60
commit 934f68ebf6
2 changed files with 14 additions and 5 deletions

View File

@ -1,6 +1,6 @@
//! Based on https://github.com/ibraheemdev/matchit/blob/master/benches/bench.rs //! Based on https://github.com/ibraheemdev/matchit/blob/master/benches/bench.rs
use criterion::{black_box, criterion_group, criterion_main, Criterion}; use criterion::{black_box, Criterion, criterion_group, criterion_main};
macro_rules! register { macro_rules! register {
(colon) => {{ (colon) => {{
@ -179,6 +179,15 @@ fn compare_routers(c: &mut Criterion) {
}); });
}); });
group.bench_function("actix_guard_failures", |b| {
b.iter(|| {
for route in call() {
let mut path = actix_router::Path::new(route);
black_box(actix.recognize_fn(&mut path, |_, _| false));
}
});
});
let regex_set = regex::RegexSet::new(register!(regex)).unwrap(); let regex_set = regex::RegexSet::new(register!(regex)).unwrap();
group.bench_function("regex", |b| { group.bench_function("regex", |b| {
b.iter(|| { b.iter(|| {

View File

@ -52,18 +52,18 @@ impl<T, U> Router<T, U> {
R: Resource, R: Resource,
F: FnMut(&R, &U) -> bool, F: FnMut(&R, &U) -> bool,
{ {
let mut matches = 0; let mut next_match_count = 1;
for (rdef, val, ctx) in self.routes.iter() { for (rdef, val, ctx) in self.routes.iter() {
match rdef.capture_match_info(resource) { match rdef.capture_match_info(resource) {
None => {} None => {}
Some(match_info) => { Some(match_info) => {
matches += 1;
if check_fn(resource, ctx) { if check_fn(resource, ctx) {
rdef.resolve_resource(resource, match_info); rdef.resolve_resource(resource, match_info);
return Some((val, ResourceId(rdef.id()))); return Some((val, ResourceId(rdef.id())));
} else if matches == self.max_path_conflicts { } else if next_match_count == self.max_path_conflicts {
return None; return None;
} }
next_match_count += 1;
} }
} }
} }