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

Use FixedArray instead of Box<[T]> #294

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cfg-if = "1.0.0"
rayon = { version = "1.7.0", optional = true }
once_cell = "1.18.0"
arbitrary = { version = "1.3.0", optional = true }
small-fixed-array = "0.4.0"

[package.metadata.docs.rs]
features = ["rayon", "raw-api", "serde"]
6 changes: 3 additions & 3 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::sync::Arc;
/// ```
pub struct OwningIter<K, V, S = RandomState> {
map: DashMap<K, V, S>,
shard_i: usize,
shard_i: u32,
current: Option<GuardOwningIter<K, V>>,
}

Expand Down Expand Up @@ -114,7 +114,7 @@ type GuardIterMut<'a, K, V, S> = (
/// ```
pub struct Iter<'a, K, V, S = RandomState, M = DashMap<K, V, S>> {
map: &'a M,
shard_i: usize,
shard_i: u32,
current: Option<GuardIter<'a, K, V, S>>,
}

Expand Down Expand Up @@ -198,7 +198,7 @@ impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iter
/// ```
pub struct IterMut<'a, K, V, S = RandomState, M = DashMap<K, V, S>> {
map: &'a M,
shard_i: usize,
shard_i: u32,
current: Option<GuardIterMut<'a, K, V, S>>,
}

Expand Down
71 changes: 43 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ use mapref::one::{Ref, RefMut};
use once_cell::sync::OnceCell;
pub use read_only::ReadOnlyView;
pub use set::DashSet;
use small_fixed_array::{FixedArray, ValidLength as _};
use std::collections::hash_map::RandomState;
use std::convert::{TryFrom as _, TryInto as _};
pub use t::Map;
use try_result::TryResult;

Expand Down Expand Up @@ -70,8 +72,8 @@ fn default_shard_amount() -> usize {
})
}

fn ncb(shard_amount: usize) -> usize {
shard_amount.trailing_zeros() as usize
fn ncb(shard_amount: usize) -> u16 {
shard_amount.trailing_zeros() as _
}

/// DashMap is an implementation of a concurrent associative array/hashmap in Rust.
Expand All @@ -86,24 +88,28 @@ fn ncb(shard_amount: usize) -> usize {
/// Documentation mentioning locking behaviour acts in the reference frame of the calling thread.
/// This means that it is safe to ignore it across multiple threads.
pub struct DashMap<K, V, S = RandomState> {
shift: usize,
shards: Box<[RwLock<HashMap<K, V, S>>]>,
shift: u16,
shards: FixedArray<RwLock<HashMap<K, V, S>>>,
hasher: S,
}

impl<K: Eq + Hash + Clone, V: Clone, S: Clone> Clone for DashMap<K, V, S> {
fn clone(&self) -> Self {
let mut inner_shards = Vec::new();
let mut inner_shards = Vec::with_capacity(self.shards.len() as usize);

for shard in self.shards.iter() {
let shard = shard.read();

inner_shards.push(RwLock::new((*shard).clone()));
}

let shards = inner_shards
.into_boxed_slice()
.try_into()
.unwrap_or_else(|_| panic!("size should not change so cannot overflow"));

Self {
shards,
shift: self.shift,
shards: inner_shards.into_boxed_slice(),
hasher: self.hasher.clone(),
}
}
Expand Down Expand Up @@ -281,10 +287,13 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {

let cps = capacity / shard_amount;

let shards = (0..shard_amount)
let shards: Box<[_]> = (0..shard_amount)
.map(|_| RwLock::new(HashMap::with_capacity_and_hasher(cps, hasher.clone())))
.collect();

let shards = FixedArray::try_from(shards)
.unwrap_or_else(|_| panic!("cannot store more than 4 billion shards"));

Self {
shift,
shards,
Expand All @@ -295,11 +304,15 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
/// Hash a given item to produce a usize.
/// Uses the provided or default HashBuilder.
pub fn hash_usize<T: Hash>(&self, item: &T) -> usize {
self.hash_u64(item) as usize
}

fn hash_u64<T: Hash>(&self, item: &T) -> u64 {
let mut hasher = self.hasher.build_hasher();

item.hash(&mut hasher);

hasher.finish() as usize
hasher.finish()
}

cfg_if! {
Expand Down Expand Up @@ -333,7 +346,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
/// use dashmap::SharedValue;
///
/// let mut map = DashMap::<i32, &'static str>::new();
/// let shard_ind = map.determine_map(&42);
/// let shard_ind = map.determine_map(&42) as usize;
/// map.shards_mut()[shard_ind].get_mut().insert(42, SharedValue::new("forty two"));
/// assert_eq!(*map.get(&42).unwrap(), "forty two");
/// ```
Expand All @@ -348,7 +361,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
///
/// See [`DashMap::shards()`] and [`DashMap::shards_mut()`] for more information.
pub fn into_shards(self) -> Box<[RwLock<HashMap<K, V, S>>]> {
self.shards
self.shards.into_boxed_slice()
}
} else {
#[allow(dead_code)]
Expand All @@ -363,7 +376,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {

#[allow(dead_code)]
pub(crate) fn into_shards(self) -> Box<[RwLock<HashMap<K, V, S>>]> {
self.shards
self.shards.into_boxed_slice()
}
}
}
Expand All @@ -385,7 +398,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
/// map.insert("coca-cola", 1.4);
/// println!("coca-cola is stored in shard: {}", map.determine_map("coca-cola"));
/// ```
pub fn determine_map<Q>(&self, key: &Q) -> usize
pub fn determine_map<Q>(&self, key: &Q) -> u32
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Expand All @@ -411,16 +424,18 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
/// let key = "key";
/// let hash = map.hash_usize(&key);
/// println!("hash is stored in shard: {}", map.determine_shard(hash));
/// panic!()
/// ```
pub fn determine_shard(&self, hash: usize) -> usize {
pub fn determine_shard(&self, hash: usize) -> u32 {
// Leave the high 7 bits for the HashBrown SIMD tag.
(hash << 7) >> self.shift
((hash << 7) >> self.shift) as u32
}
} else {

pub(crate) fn determine_shard(&self, hash: usize) -> usize {
pub(crate) fn determine_shard(&self, hash: usize) -> u32 {
// Leave the high 7 bits for the HashBrown SIMD tag.
(hash << 7) >> self.shift
((hash << 7) >> self.shift) as u32

}
}
}
Expand Down Expand Up @@ -875,44 +890,44 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
for DashMap<K, V, S>
{
fn _shard_count(&self) -> usize {
fn _shard_count(&self) -> u32 {
self.shards.len()
}

unsafe fn _get_read_shard(&'a self, i: usize) -> &'a HashMap<K, V, S> {
unsafe fn _get_read_shard(&'a self, i: u32) -> &'a HashMap<K, V, S> {
debug_assert!(i < self.shards.len());

&*self.shards.get_unchecked(i).data_ptr()
&*self.shards.get_unchecked(i.to_usize()).data_ptr()
}

unsafe fn _yield_read_shard(&'a self, i: usize) -> RwLockReadGuard<'a, HashMap<K, V, S>> {
unsafe fn _yield_read_shard(&'a self, i: u32) -> RwLockReadGuard<'a, HashMap<K, V, S>> {
debug_assert!(i < self.shards.len());

self.shards.get_unchecked(i).read()
self.shards.get_unchecked(i.to_usize()).read()
}

unsafe fn _yield_write_shard(&'a self, i: usize) -> RwLockWriteGuard<'a, HashMap<K, V, S>> {
unsafe fn _yield_write_shard(&'a self, i: u32) -> RwLockWriteGuard<'a, HashMap<K, V, S>> {
debug_assert!(i < self.shards.len());

self.shards.get_unchecked(i).write()
self.shards.get_unchecked(i.to_usize()).write()
}

unsafe fn _try_yield_read_shard(
&'a self,
i: usize,
i: u32,
) -> Option<RwLockReadGuard<'a, HashMap<K, V, S>>> {
debug_assert!(i < self.shards.len());

self.shards.get_unchecked(i).try_read()
self.shards.get_unchecked(i.to_usize()).try_read()
}

unsafe fn _try_yield_write_shard(
&'a self,
i: usize,
i: u32,
) -> Option<RwLockWriteGuard<'a, HashMap<K, V, S>>> {
debug_assert!(i < self.shards.len());

self.shards.get_unchecked(i).try_write()
self.shards.get_unchecked(i.to_usize()).try_write()
}

fn _insert(&self, key: K, value: V) -> Option<V> {
Expand Down
3 changes: 2 additions & 1 deletion src/rayon/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{DashMap, HashMap};
use core::hash::{BuildHasher, Hash};
use rayon::iter::plumbing::UnindexedConsumer;
use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator};
use small_fixed_array::FixedArray;
use std::collections::hash_map::RandomState;
use std::sync::Arc;

Expand Down Expand Up @@ -80,7 +81,7 @@ where
}

pub struct OwningIter<K, V, S = RandomState> {
pub(super) shards: Box<[RwLock<HashMap<K, V, S>>]>,
pub(super) shards: FixedArray<RwLock<HashMap<K, V, S>>>,
}

impl<K, V, S> ParallelIterator for OwningIter<K, V, S>
Expand Down
4 changes: 2 additions & 2 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
/// set.insert("coca-cola");
/// println!("coca-cola is stored in shard: {}", set.determine_map("coca-cola"));
/// ```
pub fn determine_map<Q>(&self, key: &Q) -> usize
pub fn determine_map<Q>(&self, key: &Q) -> u32
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Expand All @@ -185,7 +185,7 @@ impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
/// let hash = set.hash_usize(&key);
/// println!("hash is stored in shard: {}", set.determine_shard(hash));
/// ```
pub fn determine_shard(&self, hash: usize) -> usize {
pub fn determine_shard(&self, hash: usize) -> u32 {
self.inner.determine_shard(hash)
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ use core::hash::{BuildHasher, Hash};

/// Implementation detail that is exposed due to generic constraints in public types.
pub trait Map<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + Clone + BuildHasher> {
fn _shard_count(&self) -> usize;
fn _shard_count(&self) -> u32;

/// # Safety
///
/// The index must not be out of bounds.
unsafe fn _get_read_shard(&'a self, i: usize) -> &'a HashMap<K, V, S>;
unsafe fn _get_read_shard(&'a self, i: u32) -> &'a HashMap<K, V, S>;

/// # Safety
///
/// The index must not be out of bounds.
unsafe fn _yield_read_shard(&'a self, i: usize) -> RwLockReadGuard<'a, HashMap<K, V, S>>;
unsafe fn _yield_read_shard(&'a self, i: u32) -> RwLockReadGuard<'a, HashMap<K, V, S>>;

/// # Safety
///
/// The index must not be out of bounds.
unsafe fn _yield_write_shard(&'a self, i: usize) -> RwLockWriteGuard<'a, HashMap<K, V, S>>;
unsafe fn _yield_write_shard(&'a self, i: u32) -> RwLockWriteGuard<'a, HashMap<K, V, S>>;

/// # Safety
///
/// The index must not be out of bounds.
unsafe fn _try_yield_read_shard(
&'a self,
i: usize,
i: u32,
) -> Option<RwLockReadGuard<'a, HashMap<K, V, S>>>;

/// # Safety
///
/// The index must not be out of bounds.
unsafe fn _try_yield_write_shard(
&'a self,
i: usize,
i: u32,
) -> Option<RwLockWriteGuard<'a, HashMap<K, V, S>>>;

fn _insert(&self, key: K, value: V) -> Option<V>;
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use core::cell::UnsafeCell;
use core::{mem, ptr};

pub const fn ptr_size_bits() -> usize {
mem::size_of::<usize>() * 8
pub const fn ptr_size_bits() -> u16 {
(mem::size_of::<usize>() * 8) as _
}

pub fn map_in_place_2<T, U, F: FnOnce(U, T) -> T>((k, v): (U, &mut T), f: F) {
Expand Down
Loading