-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: use
hashbrown::RawTable
+ dashmap
's shard selection strategy
docs: document API types and methods, add doc tests
- Loading branch information
Showing
9 changed files
with
430 additions
and
195 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,62 @@ | ||
//! # Whirlwind | ||
//! | ||
//! A collection of data structures that allow for concurrent access to shared data. | ||
//! | ||
//! Currently, this crate provides the following data structures: | ||
//! | ||
//! - [`ShardMap`]: A concurrent hashmap using a sharding strategy. | ||
//! - [`ShardSet`]: A concurrent set based on a [`ShardMap`] with values of `()`. | ||
//! | ||
//! ## ShardMap | ||
//! | ||
//! A concurrent hashmap using a sharding strategy. | ||
//! | ||
//! ### Example | ||
//! | ||
//! ```rust | ||
//! use tokio::runtime::Runtime; | ||
//! use std::sync::Arc; | ||
//! use whirlwind::ShardMap; | ||
//! | ||
//! let rt = Runtime::new().unwrap(); | ||
//! let map = Arc::new(ShardMap::new()); | ||
//! | ||
//! rt.block_on(async { | ||
//! map.insert("foo", "bar").await; | ||
//! assert_eq!(map.len().await, 1); | ||
//! assert_eq!(map.contains_key(&"foo").await, true); | ||
//! }); | ||
//! ``` | ||
//! | ||
//! ## ShardSet | ||
//! | ||
//! A concurrent set based on a [`ShardMap`] with values of `()`. | ||
//! | ||
//! ### Example | ||
//! | ||
//! ```rust | ||
//! use tokio::runtime::Runtime; | ||
//! use std::sync::Arc; | ||
//! use whirlwind::ShardSet; | ||
//! | ||
//! let rt = Runtime::new().unwrap(); | ||
//! let set = Arc::new(ShardSet::new()); | ||
//! rt.block_on(async { | ||
//! set.insert("foo").await; | ||
//! assert_eq!(set.contains(&"foo").await, true); | ||
//! set.remove(&"foo").await; | ||
//! assert_eq!(set.contains(&"foo").await, false); | ||
//! assert_eq!(set.len().await, 0); | ||
//! }); | ||
//! ``` | ||
//! | ||
//! | ||
//! See the documentation for each data structure for more information. | ||
|
||
pub mod mapref; | ||
mod shard; | ||
pub mod shard_map; | ||
pub mod shard_set; | ||
mod shard_map; | ||
mod shard_set; | ||
|
||
pub use shard_map::ShardMap; | ||
pub use shard_set::ShardSet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
use super::{Shard, ShardReader, ShardWriter}; | ||
|
||
/// A future that resolves to a read lock on a shard. | ||
pub(crate) struct Read<'a, K, V> { | ||
shard: &'a Shard<K, V>, | ||
} | ||
|
||
impl<'a, K, V> Read<'a, K, V> { | ||
pub(crate) fn new(shard: &'a Shard<K, V>) -> Self { | ||
Self { shard } | ||
} | ||
} | ||
|
||
impl<'a, K, V> Future for Read<'a, K, V> { | ||
type Output = ShardReader<'a, K, V>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { | ||
match self.shard.data.try_read() { | ||
Ok(guard) => Poll::Ready(guard), | ||
_ => { | ||
cx.waker().wake_by_ref(); | ||
Poll::Pending | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub(crate) struct Write<'a, K, V> { | ||
shard: &'a Shard<K, V>, | ||
} | ||
|
||
impl<'a, K, V> Write<'a, K, V> { | ||
pub(crate) fn new(shard: &'a Shard<K, V>) -> Self { | ||
Self { shard } | ||
} | ||
} | ||
|
||
impl<'a, K, V> Future for Write<'a, K, V> { | ||
type Output = ShardWriter<'a, K, V>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { | ||
match self.shard.data.try_write() { | ||
Ok(guard) => Poll::Ready(guard), | ||
_ => { | ||
cx.waker().wake_by_ref(); | ||
Poll::Pending | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.