Add optimized lock methods for Sharded
This commit is contained in:
parent
3d249706aa
commit
8fc160b742
5 changed files with 133 additions and 33 deletions
|
@ -73,6 +73,53 @@ impl<T> Sharded<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// The shard is selected by hashing `val` with `FxHasher`.
|
||||
#[inline]
|
||||
pub fn lock_shard_by_value<K: Hash + ?Sized>(&self, _val: &K) -> LockGuard<'_, T> {
|
||||
match self {
|
||||
Self::Single(single) => {
|
||||
// Syncronization is disabled so use the `lock_assume_no_sync` method optimized
|
||||
// for that case.
|
||||
|
||||
// SAFETY: We know `is_dyn_thread_safe` was false when creating the lock thus
|
||||
// `might_be_dyn_thread_safe` was also false.
|
||||
unsafe { single.lock_assume_no_sync() }
|
||||
}
|
||||
#[cfg(parallel_compiler)]
|
||||
Self::Shards(..) => self.lock_shard_by_hash(make_hash(_val)),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn lock_shard_by_hash(&self, hash: u64) -> LockGuard<'_, T> {
|
||||
self.lock_shard_by_index(get_shard_hash(hash))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn lock_shard_by_index(&self, _i: usize) -> LockGuard<'_, T> {
|
||||
match self {
|
||||
Self::Single(single) => {
|
||||
// Syncronization is disabled so use the `lock_assume_no_sync` method optimized
|
||||
// for that case.
|
||||
|
||||
// SAFETY: We know `is_dyn_thread_safe` was false when creating the lock thus
|
||||
// `might_be_dyn_thread_safe` was also false.
|
||||
unsafe { single.lock_assume_no_sync() }
|
||||
}
|
||||
#[cfg(parallel_compiler)]
|
||||
Self::Shards(shards) => {
|
||||
// Syncronization is enabled so use the `lock_assume_sync` method optimized
|
||||
// for that case.
|
||||
|
||||
// SAFETY (get_unchecked): The index gets ANDed with the shard mask, ensuring it is
|
||||
// always inbounds.
|
||||
// SAFETY (lock_assume_sync): We know `is_dyn_thread_safe` was true when creating
|
||||
// the lock thus `might_be_dyn_thread_safe` was also true.
|
||||
unsafe { shards.get_unchecked(_i & (SHARDS - 1)).0.lock_assume_sync() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn lock_shards(&self) -> impl Iterator<Item = LockGuard<'_, T>> {
|
||||
match self {
|
||||
|
@ -124,7 +171,7 @@ impl<K: Eq + Hash + Copy> ShardedHashMap<K, ()> {
|
|||
Q: Hash + Eq,
|
||||
{
|
||||
let hash = make_hash(value);
|
||||
let mut shard = self.get_shard_by_hash(hash).lock();
|
||||
let mut shard = self.lock_shard_by_hash(hash);
|
||||
let entry = shard.raw_entry_mut().from_key_hashed_nocheck(hash, value);
|
||||
|
||||
match entry {
|
||||
|
@ -144,7 +191,7 @@ impl<K: Eq + Hash + Copy> ShardedHashMap<K, ()> {
|
|||
Q: Hash + Eq,
|
||||
{
|
||||
let hash = make_hash(&value);
|
||||
let mut shard = self.get_shard_by_hash(hash).lock();
|
||||
let mut shard = self.lock_shard_by_hash(hash);
|
||||
let entry = shard.raw_entry_mut().from_key_hashed_nocheck(hash, &value);
|
||||
|
||||
match entry {
|
||||
|
@ -166,7 +213,7 @@ pub trait IntoPointer {
|
|||
impl<K: Eq + Hash + Copy + IntoPointer> ShardedHashMap<K, ()> {
|
||||
pub fn contains_pointer_to<T: Hash + IntoPointer>(&self, value: &T) -> bool {
|
||||
let hash = make_hash(&value);
|
||||
let shard = self.get_shard_by_hash(hash).lock();
|
||||
let shard = self.lock_shard_by_hash(hash);
|
||||
let value = value.into_pointer();
|
||||
shard.raw_entry().from_hash(hash, |entry| entry.into_pointer() == value).is_some()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue