Skip to content

Commit

Permalink
Merge pull request #217 from DuskSystems/storage
Browse files Browse the repository at this point in the history
Remove storage abstraction for now.
  • Loading branch information
CathalMullan authored Nov 17, 2024
2 parents a360053 + 6cc6116 commit 5bab4c8
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 225 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,29 +512,29 @@ In a router of 130 routes, benchmark matching 4 paths.

| Library | Time | Alloc Count | Alloc Size | Dealloc Count | Dealloc Size |
|:-----------------|----------:|------------:|-----------:|--------------:|-------------:|
| wayfind | 335.58 ns | 4 | 265 B | 4 | 265 B |
| matchit | 377.06 ns | 4 | 416 B | 4 | 448 B |
| xitca-router | 410.98 ns | 7 | 800 B | 7 | 832 B |
| path-tree | 439.78 ns | 4 | 416 B | 4 | 448 B |
| ntex-router | 1.7437 µs | 18 | 1.248 KB | 18 | 1.28 KB |
| route-recognizer | 2.0488 µs | 160 | 8.505 KB | 160 | 8.537 KB |
| routefinder | 4.7027 µs | 67 | 5.024 KB | 67 | 5.056 KB |
| actix-router | 17.785 µs | 214 | 13.93 KB | 214 | 13.96 KB |
| wayfind | 341.71 ns | 4 | 265 B | 4 | 265 B |
| matchit | 376.67 ns | 4 | 416 B | 4 | 448 B |
| xitca-router | 415.14 ns | 7 | 800 B | 7 | 832 B |
| path-tree | 442.06 ns | 4 | 416 B | 4 | 448 B |
| ntex-router | 1.7614 µs | 18 | 1.248 KB | 18 | 1.28 KB |
| route-recognizer | 2.0531 µs | 160 | 8.505 KB | 160 | 8.537 KB |
| routefinder | 4.7332 µs | 67 | 5.024 KB | 67 | 5.056 KB |
| actix-router | 17.897 µ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.7872 µs | 59 | 2.567 KB | 59 | 2.567 KB |
| matchit | 6.4767 µs | 140 | 17.81 KB | 140 | 17.83 KB |
| path-tree | 7.2644 µs | 59 | 7.447 KB | 59 | 7.47 KB |
| xitca-router | 7.5313 µs | 209 | 25.51 KB | 209 | 25.53 KB |
| ntex-router | 31.085 µs | 201 | 19.54 KB | 201 | 19.56 KB |
| route-recognizer | 54.813 µs | 2872 | 191.7 KB | 2872 | 204.8 KB |
| routefinder | 75.617 µs | 525 | 48.4 KB | 525 | 48.43 KB |
| actix-router | 151.15 µs | 2201 | 128.8 KB | 2201 | 128.8 KB |
| wayfind | 5.1040 µs | 59 | 2.567 KB | 59 | 2.567 KB |
| matchit | 6.4678 µs | 140 | 17.81 KB | 140 | 17.83 KB |
| path-tree | 7.0941 µs | 59 | 7.447 KB | 59 | 7.47 KB |
| xitca-router | 7.5814 µs | 209 | 25.51 KB | 209 | 25.53 KB |
| ntex-router | 31.100 µs | 201 | 19.54 KB | 201 | 19.56 KB |
| route-recognizer | 54.829 µs | 2872 | 191.7 KB | 2872 | 204.8 KB |
| routefinder | 75.961 µs | 525 | 48.4 KB | 525 | 48.43 KB |
| actix-router | 152.62 µs | 2201 | 128.8 KB | 2201 | 128.8 KB |

## License

Expand Down
15 changes: 12 additions & 3 deletions src/id.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use core::sync::atomic::{AtomicUsize, Ordering};
use core::{
hash::{Hash, Hasher},
sync::atomic::{AtomicUsize, Ordering},
};

static ID: AtomicUsize = AtomicUsize::new(0);

/// A unique ID for a route within the router.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RouteId(usize);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RouteId(pub(crate) usize);

impl RouteId {
pub fn new() -> Self {
Expand All @@ -17,3 +20,9 @@ impl Default for RouteId {
Self::new()
}
}

impl Hash for RouteId {
fn hash<H: Hasher>(&self, hasher: &mut H) {
hasher.write_usize(self.0);
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub mod errors;

pub(crate) mod id;

pub(crate) mod map;

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

Expand All @@ -18,5 +20,3 @@ pub use route::{Route, RouteBuilder};
pub(crate) mod routers;
pub use routers::path::{PathConstraint, PathMatch, PathParameters};
pub use routers::{Match, Router};

pub(crate) mod storage;
30 changes: 30 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::id::RouteId;
use core::{
hash::{BuildHasherDefault, Hasher},
marker::PhantomData,
};
use hashbrown::HashMap;

pub type RouteMap<V> = HashMap<RouteId, V, BuildHasherDefault<NoHashHasher<RouteId>>>;

pub struct NoHashHasher<T>(u64, PhantomData<T>);

impl<T> Default for NoHashHasher<T> {
fn default() -> Self {
Self(0, PhantomData)
}
}

impl<T> Hasher for NoHashHasher<T> {
fn write(&mut self, _: &[u8]) {
panic!("Invalid use of NoHashHasher")
}

fn write_usize(&mut self, n: usize) {
self.0 = n as u64;
}

fn finish(&self) -> u64 {
self.0
}
}
8 changes: 2 additions & 6 deletions src/route.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{decode::percent_decode, errors::RouteError, storage::StorageKind};
use crate::{decode::percent_decode, errors::RouteError};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
Expand All @@ -8,7 +8,6 @@ use alloc::{
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Route<'r> {
pub(crate) route: &'r str,
pub storage: StorageKind,
}

/// Builder pattern for creating a [`Route`].
Expand Down Expand Up @@ -45,10 +44,7 @@ impl<'r> RouteBuilder<'r> {
})?;
}

Ok(Route {
route,
storage: StorageKind::Inline,
})
Ok(Route { route })
}
}

Expand Down
42 changes: 11 additions & 31 deletions src/routers.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,46 @@
use crate::{
errors::{DeleteError, InsertError, SearchError},
id::RouteId,
storage::{Storage, StorageKind},
map::RouteMap,
Request, Route,
};
use hashbrown::HashMap;
use path::{node::Data, PathMatch, PathRouter};
use path::{PathMatch, PathRouter};

pub mod path;

pub type Match<'r, 'p, T> = PathMatch<'r, 'p, T>;

#[derive(Clone)]
pub struct Router<'r, T> {
pub path: PathRouter<'r, T>,

/// Stored data.
data: HashMap<RouteId, T>,
pub path: PathRouter<'r>,
data: RouteMap<T>,
}

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

#[allow(clippy::missing_errors_doc)]
pub fn insert(&mut self, route: &Route<'r>, value: T) -> Result<(), InsertError> {
let value = match route.storage {
StorageKind::Inline => Some(value),
StorageKind::Router(id) => {
self.data.insert(id, value);
None
}
};
let id = RouteId::new();

self.path.insert(route, value)?;
self.path.insert(route, id)?;
self.data.insert(id, value);

Ok(())
}

#[allow(clippy::missing_errors_doc)]
pub fn delete(&mut self, route: &Route<'r>) -> Result<(), DeleteError> {
let data = self.path.delete(route)?;
let path_data = self.path.delete(route)?;

match &data {
Data::Inline { storage, .. } => match storage {
Storage::Inline(_) => (),
Storage::Router(id) => {
self.data.remove(id);
}
},
Data::Shared { storage, .. } => match storage.as_ref() {
Storage::Inline(_) => (),
Storage::Router(id) => {
self.data.remove(id);
}
},
};
let path_id = path_data.id;
self.data.remove(&path_id);

Ok(())
}
Expand Down
Loading

0 comments on commit 5bab4c8

Please sign in to comment.