Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hashing experiments #220

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ In a router of 130 routes, benchmark matching 4 paths.

| Library | Time | Alloc Count | Alloc Size | Dealloc Count | Dealloc Size |
|:-----------------|----------:|------------:|-----------:|--------------:|-------------:|
| wayfind | 365.01 ns | 5 | 329 B | 5 | 329 B |
| wayfind | 401.83 ns | 5 | 329 B | 5 | 329 B |
| matchit | 571.88 ns | 5 | 480 B | 5 | 512 B |
| path-tree | 584.19 ns | 5 | 480 B | 5 | 512 B |
| xitca-router | 652.85 ns | 8 | 864 B | 8 | 896 B |
Expand All @@ -523,7 +523,7 @@ In a router of 320 routes, benchmark matching 80 paths.

| Library | Time | Alloc Count | Alloc Size | Dealloc Count | Dealloc Size |
|:-----------------|----------:|------------:|-----------:|--------------:|-------------:|
| wayfind | 5.0882 µs | 60 | 3.847 KB | 60 | 3.847 KB |
| wayfind | 5.8701 µs | 60 | 3.847 KB | 60 | 3.847 KB |
| path-tree | 8.6580 µs | 60 | 8.727 KB | 60 | 8.75 KB |
| matchit | 9.9301 µs | 141 | 19.09 KB | 141 | 19.11 KB |
| xitca-router | 11.894 µs | 210 | 26.79 KB | 210 | 26.81 KB |
Expand Down
6 changes: 6 additions & 0 deletions src/chain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::routers::path::id::PathId;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct DataChain {
pub path: PathId,
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![doc = include_str!("../README.md")]

pub(crate) mod chain;

pub(crate) mod decode;

pub mod errors;

pub(crate) mod map;

pub(crate) mod request;
pub use request::{Request, RequestBuilder};

Expand Down
30 changes: 0 additions & 30 deletions src/map.rs

This file was deleted.

26 changes: 17 additions & 9 deletions src/routers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{
chain::DataChain,
errors::{DeleteError, InsertError, SearchError},
map::RouteMap,
Request, Route,
};
use path::{PathParameters, PathRouter};
use std::collections::HashMap;

pub mod path;

Expand All @@ -23,22 +24,24 @@ pub struct PathMatch<'r, 'p> {
#[derive(Clone)]
pub struct Router<'r, T> {
pub path: PathRouter<'r>,
data: RouteMap<T>,
data: HashMap<DataChain, T>,
}

impl<'r, T> Router<'r, T> {
#[must_use]
pub fn new() -> Self {
Self {
path: PathRouter::new(),
data: RouteMap::default(),
data: HashMap::default(),
}
}

#[allow(clippy::missing_errors_doc)]
pub fn insert(&mut self, route: &Route<'r>, value: T) -> Result<(), InsertError> {
let id = self.path.insert(route.route)?;
self.data.insert(id, value);
let path_id = self.path.insert(route.route)?;

let chain = DataChain { path: path_id };
self.data.insert(chain, value);

Ok(())
}
Expand All @@ -47,8 +50,8 @@ impl<'r, T> Router<'r, T> {
pub fn delete(&mut self, route: &Route<'r>) -> Result<(), DeleteError> {
let path_data = self.path.delete(route.route)?;

let path_id = path_data.id;
self.data.remove(&path_id);
let chain = DataChain { path: path_data.id };
self.data.remove(&chain);

Ok(())
}
Expand All @@ -58,12 +61,17 @@ impl<'r, T> Router<'r, T> {
&'r self,
request: &'p Request<'p>,
) -> Result<Option<Match<'r, 'p, T>>, SearchError> {
let Some(search) = self.path.search(request, &self.data)? else {
let Some(search) = self.path.search(request.path.as_ref())? else {
return Ok(None);
};

let chain = DataChain { path: search.id };
let Some(data) = self.data.get(&chain) else {
return Ok(None);
};

Ok(Some(Match {
data: search.data,
data,
path: PathMatch {
route: search.route,
expanded: search.expanded,
Expand Down
42 changes: 11 additions & 31 deletions src/routers/path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{map::RouteMap, vec::SortedVec, Request};
use crate::vec::SortedVec;
use errors::{constraint::PathConstraintError, PathDeleteError, PathInsertError, PathSearchError};
use id::{PathId, PathIdGenerator};
use node::Node;
Expand Down Expand Up @@ -30,29 +30,17 @@ pub use constraints::PathConstraint;
/// Holds data associated with a given node.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PathData<'r> {
/// The original route.
pub(crate) route: &'r str,

/// The expanded route.
pub(crate) expanded: Option<Arc<str>>,

/// The associated data ID.
pub(crate) id: PathId,
pub id: PathId,
pub route: &'r str,
pub expanded: Option<Arc<str>>,
}

/// Stores data from a successful router match.
#[derive(Debug, Eq, PartialEq)]
pub struct PathMatch<'r, 'p, T> {
/// A reference to the matching route data.
pub data: &'r T,

/// The matching route.
pub struct PathMatch<'r, 'p> {
pub id: PathId,
pub route: &'r str,

/// The expanded route, if applicable.
pub expanded: Option<&'r str>,

/// Key-value pairs of parameters, extracted from the route.
pub parameters: PathParameters<'r, 'p>,
}

Expand Down Expand Up @@ -342,16 +330,12 @@ impl<'r> PathRouter<'r> {
/// .unwrap();
/// let search = router.search(&request).unwrap();
/// ```
pub(crate) fn search<'p, T>(
pub(crate) fn search<'p>(
&'r self,
request: &'p Request<'p>,
map: &'r RouteMap<T>,
) -> Result<Option<PathMatch<'r, 'p, T>>, PathSearchError> {
path: &'p [u8],
) -> Result<Option<PathMatch<'r, 'p>>, PathSearchError> {
let mut parameters = smallvec![];
let Some((data, _)) =
self.root
.search(request.path.as_ref(), &mut parameters, &self.constraints)?
else {
let Some((data, _)) = self.root.search(path, &mut parameters, &self.constraints)? else {
return Ok(None);
};

Expand All @@ -362,12 +346,8 @@ impl<'r> PathRouter<'r> {
..
} = data;

let Some(data) = map.get(id) else {
return Ok(None);
};

Ok(Some(PathMatch {
data,
id: *id,
route,
expanded: expanded.as_deref(),
parameters,
Expand Down