From d072a4f73a0758f718854df54385572bfcf3c0de Mon Sep 17 00:00:00 2001 From: Vincent Thomas <77443389+vincent-thomas@users.noreply.github.com> Date: Mon, 13 Jan 2025 14:49:51 +0100 Subject: [PATCH] perf(titan-router): more cache use and debloat vec --- titan-router/src/router.rs | 38 ++++++++++++----------------------- titan/examples/with_guards.rs | 2 +- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/titan-router/src/router.rs b/titan-router/src/router.rs index 88da38e..52e9b83 100644 --- a/titan-router/src/router.rs +++ b/titan-router/src/router.rs @@ -2,16 +2,13 @@ use std::collections::HashMap; use crate::segments::{FindSegmentResult, Segments}; -#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)] -pub(crate) struct RouteId(usize); - #[derive(Clone)] pub struct Router where V: Clone, { routes: Vec<(Segments, V)>, - lookup_cache: HashMap, + lookup_cache: HashMap, } impl Default for Router @@ -36,30 +33,25 @@ where pub fn at(&mut self, route: &str, handler: V) { let segments: Segments = route.to_string().into(); - let index = self.routes.len(); // (self.routes.len() - 1) + 1 - - let route_id = RouteId(index); - - self.routes.push((segments.clone(), handler)); - if segments.num_params() == 0 { - self.lookup_cache.insert(segments, route_id); + self.lookup_cache.insert(segments, handler); + } else { + self.routes.push((segments.clone(), handler)); } } pub fn find(&mut self, route: &str) -> Option> { let segments = Segments::from(route.to_string()); - if let Some(RouteId(route_index)) = self.lookup_cache.get(&segments) { - let (_, value) = &self.routes[*route_index]; + if let Some(value) = self.lookup_cache.get(&segments) { return Some(Match { value, params: HashMap::default() }); }; - for (index, (key, value)) in self.routes.iter().enumerate() { + for (key, value) in self.routes.iter() { if let FindSegmentResult::Match(params) = key.find(&segments.0) { - if params.is_empty() { - self.lookup_cache.insert(segments, RouteId(index)); - } + //if params.is_empty() { + //self.lookup_cache.insert(segments, RouteId(index)); + //} return Some(Match { value, params }); }; } @@ -69,8 +61,8 @@ where pub fn lookup(&self, route: &str) -> Option> { let from_request = Segments::from(route.to_string()); - if let Some(RouteId(route_index)) = self.lookup_cache.get(&from_request) { - let (_, value) = &self.routes[*route_index]; + if let Some(value) = self.lookup_cache.get(&from_request) { + //let (_, value) = &self.routes[*route_index]; return Some(Match { value, params: HashMap::default() }); }; @@ -84,16 +76,12 @@ where pub fn lookup_mut(&mut self, route: &str) -> Option> { let segments = Segments::from(route.to_string()); - if let Some(RouteId(route_index)) = self.lookup_cache.get_mut(&segments) { - let (_, value) = &mut self.routes[*route_index]; + if let Some(value) = self.lookup_cache.get_mut(&segments) { return Some(Match { value, params: HashMap::default() }); }; - for (index, (key, value)) in self.routes.iter_mut().enumerate() { + for (key, value) in self.routes.iter_mut() { if let FindSegmentResult::Match(params) = key.find(&segments.0) { - if params.is_empty() { - self.lookup_cache.insert(segments, RouteId(index)); - } return Some(Match { value, params }); }; } diff --git a/titan/examples/with_guards.rs b/titan/examples/with_guards.rs index 0e1279c..7173de4 100644 --- a/titan/examples/with_guards.rs +++ b/titan/examples/with_guards.rs @@ -16,7 +16,7 @@ async fn main() -> io::Result<()> { let app = App::default() .at("/", web::get(index)) .at("/admin", web::get(protected)) - .at("/redirect", web::Redirect::new(true, "/admin")); + .at("/redirect", web::Redirect::permanent("/admin")); let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap(); titan::serve(listener, app).await