Skip to content

Commit

Permalink
perf(titan-router): more cache use and debloat vec
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-thomas committed Jan 13, 2025
1 parent d19d365 commit d072a4f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
38 changes: 13 additions & 25 deletions titan-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<V>
where
V: Clone,
{
routes: Vec<(Segments, V)>,
lookup_cache: HashMap<Segments, RouteId>,
lookup_cache: HashMap<Segments, V>,
}

impl<V> Default for Router<V>
Expand All @@ -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<Match<&V>> {
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 });
};
}
Expand All @@ -69,8 +61,8 @@ where
pub fn lookup(&self, route: &str) -> Option<Match<&V>> {
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() });
};

Expand All @@ -84,16 +76,12 @@ where
pub fn lookup_mut(&mut self, route: &str) -> Option<Match<&mut V>> {
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 });
};
}
Expand Down
2 changes: 1 addition & 1 deletion titan/examples/with_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d072a4f

Please sign in to comment.