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

Hash keys once #194

Merged
merged 2 commits into from
Apr 27, 2022
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ send_guard = ["parking_lot/send_guard"]
[dependencies]
num_cpus = "1.13.1"
parking_lot = "0.12.0"
hashbrown = "0.11.2"
serde = { version = "1.0.136", optional = true, features = ["derive"] }
cfg-if = "1.0.0"
rayon = { version = "1.5.1", optional = true }
Expand Down
7 changes: 3 additions & 4 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{DashMap, HashMap};
use core::hash::{BuildHasher, Hash};
use core::mem;
use parking_lot::{RwLockReadGuard, RwLockWriteGuard};
use std::collections::hash_map;
use std::collections::hash_map::RandomState;
use std::sync::Arc;

Expand Down Expand Up @@ -39,7 +38,7 @@ impl<K: Eq + Hash, V, S: BuildHasher + Clone> OwningIter<K, V, S> {
}
}

type GuardOwningIter<K, V> = hash_map::IntoIter<K, SharedValue<V>>;
type GuardOwningIter<K, V> = hashbrown::hash_map::IntoIter<K, SharedValue<V>>;

impl<K: Eq + Hash, V, S: BuildHasher + Clone> Iterator for OwningIter<K, V, S> {
type Item = (K, V);
Expand Down Expand Up @@ -93,12 +92,12 @@ where

type GuardIter<'a, K, V, S> = (
Arc<RwLockReadGuard<'a, HashMap<K, V, S>>>,
hash_map::Iter<'a, K, SharedValue<V>>,
hashbrown::hash_map::Iter<'a, K, SharedValue<V>>,
);

type GuardIterMut<'a, K, V, S> = (
Arc<RwLockWriteGuard<'a, HashMap<K, V, S>>>,
hash_map::IterMut<'a, K, SharedValue<V>>,
hashbrown::hash_map::IterMut<'a, K, SharedValue<V>>,
);

/// Iterator over a DashMap yielding immutable references.
Expand Down
122 changes: 69 additions & 53 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ cfg_if! {
}
}

pub(crate) type HashMap<K, V, S> = std::collections::HashMap<K, SharedValue<V>, S>;
Copy link

@Veykril Veykril Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type alias is exposed by public facing apis and therefor is a semver breakage, this was experienced in using one of the rust-analyzer crates as a dependency rust-lang/rust-analyzer#12344 (comment)

Copy link
Owner

@xacrimon xacrimon Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Veykril Hey! This is indeed a semver breakage, however this is only visible if you use the raw_api feature which exposes the raw shards. I should probably do a better job at noting this in crate level documentation but this feature is unstable, this means you need to pin the versions if you rely on the types from that API as it may change due to internal needs like this PR and an internal lock change that was also done recently.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, good to know!

pub(crate) type HashMap<K, V, S> = hashbrown::HashMap<K, SharedValue<V>, S>;

fn default_shard_amount() -> usize {
(num_cpus::get() * 4).next_power_of_two()
Expand Down Expand Up @@ -271,14 +271,18 @@ 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 {
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()
}

/// 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
}

cfg_if! {
Expand Down Expand Up @@ -329,8 +333,8 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
self.determine_shard(hash)
let hash = self.hash_u64(&key);
self.determine_shard(hash as usize)
}
}
}
Expand Down Expand Up @@ -837,50 +841,64 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
}

fn _insert(&self, key: K, value: V) -> Option<V> {
let hash = self.hash_usize(&key);
let hash = self.hash_u64(&key);

let idx = self.determine_shard(hash);
let idx = self.determine_shard(hash as usize);

let mut shard = unsafe { self._yield_write_shard(idx) };

shard
.insert(key, SharedValue::new(value))
.map(|v| v.into_inner())
match shard.raw_entry_mut().from_key_hashed_nocheck(hash, &key) {
hashbrown::hash_map::RawEntryMut::Occupied(mut occupied) => {
Some(occupied.insert(SharedValue::new(value)).into_inner())
}
hashbrown::hash_map::RawEntryMut::Vacant(vacant) => {
vacant.insert(key, SharedValue::new(value));
None
}
}
}

fn _remove<Q>(&self, key: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let hash = self.hash_u64(&key);

let idx = self.determine_shard(hash);
let idx = self.determine_shard(hash as usize);

let mut shard = unsafe { self._yield_write_shard(idx) };

shard.remove_entry(key).map(|(k, v)| (k, v.into_inner()))
match shard.raw_entry_mut().from_key_hashed_nocheck(hash, key) {
hashbrown::hash_map::RawEntryMut::Occupied(entry) => {
let (k, v) = entry.remove_entry();
Some((k, v.into_inner()))
}
hashbrown::hash_map::RawEntryMut::Vacant(_) => None,
}
}

fn _remove_if<Q>(&self, key: &Q, f: impl FnOnce(&K, &V) -> bool) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let hash = self.hash_u64(&key);

let idx = self.determine_shard(hash);
let idx = self.determine_shard(hash as usize);

let mut shard = unsafe { self._yield_write_shard(idx) };

if let Some((k, v)) = shard.get_key_value(key) {
if f(k, v.get()) {
shard.remove_entry(key).map(|(k, v)| (k, v.into_inner()))
} else {
None
match shard.raw_entry_mut().from_key_hashed_nocheck(hash, key) {
hashbrown::hash_map::RawEntryMut::Occupied(occupied) => {
if f(&occupied.key(), &occupied.get().get()) {
let (k, v) = occupied.remove_entry();
Some((k, v.into_inner()))
} else {
None
}
}
} else {
None
hashbrown::hash_map::RawEntryMut::Vacant(_) => None,
}
}

Expand All @@ -889,25 +907,23 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let hash = self.hash_u64(&key);

let idx = self.determine_shard(hash);
let idx = self.determine_shard(hash as usize);

let mut shard = unsafe { self._yield_write_shard(idx) };

if let Some((kptr, vptr)) = shard.get_key_value(&key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.as_ptr();

if f(&*kptr, &mut *vptr) {
shard.remove_entry(key).map(|(k, v)| (k, v.into_inner()))
match shard.raw_entry_mut().from_key_hashed_nocheck(hash, key) {
hashbrown::hash_map::RawEntryMut::Occupied(mut occupied) => {
let (k, v) = occupied.get_key_value_mut();
if f(k, v.get_mut()) {
let (k, v) = occupied.remove_entry();
Some((k, v.into_inner()))
} else {
None
}
}
} else {
None
hashbrown::hash_map::RawEntryMut::Vacant(_) => None,
}
}

Expand All @@ -924,13 +940,13 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let hash = self.hash_u64(&key);

let idx = self.determine_shard(hash);
let idx = self.determine_shard(hash as usize);

let shard = unsafe { self._yield_read_shard(idx) };

if let Some((kptr, vptr)) = shard.get_key_value(key) {
if let Some((kptr, vptr)) = shard.raw_entry().from_key_hashed_nocheck(hash, key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *const V = vptr.get();
Expand All @@ -946,13 +962,13 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let hash = self.hash_u64(&key);

let idx = self.determine_shard(hash);
let idx = self.determine_shard(hash as usize);

let shard = unsafe { self._yield_write_shard(idx) };

if let Some((kptr, vptr)) = shard.get_key_value(key) {
if let Some((kptr, vptr)) = shard.raw_entry().from_key_hashed_nocheck(hash, key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.as_ptr();
Expand All @@ -968,16 +984,16 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let hash = self.hash_u64(&key);

let idx = self.determine_shard(hash);
let idx = self.determine_shard(hash as usize);

let shard = match unsafe { self._try_yield_read_shard(idx) } {
Some(shard) => shard,
None => return TryResult::Locked,
};

if let Some((kptr, vptr)) = shard.get_key_value(key) {
if let Some((kptr, vptr)) = shard.raw_entry().from_key_hashed_nocheck(hash, key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *const V = vptr.get();
Expand All @@ -993,16 +1009,16 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let hash = self.hash_u64(&key);

let idx = self.determine_shard(hash);
let idx = self.determine_shard(hash as usize);

let shard = match unsafe { self._try_yield_write_shard(idx) } {
Some(shard) => shard,
None => return TryResult::Locked,
};

if let Some((kptr, vptr)) = shard.get_key_value(key) {
if let Some((kptr, vptr)) = shard.raw_entry().from_key_hashed_nocheck(hash, key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.as_ptr();
Expand Down Expand Up @@ -1061,13 +1077,13 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
}

fn _entry(&'a self, key: K) -> Entry<'a, K, V, S> {
let hash = self.hash_usize(&key);
let hash = self.hash_u64(&key);

let idx = self.determine_shard(hash);
let idx = self.determine_shard(hash as usize);

let shard = unsafe { self._yield_write_shard(idx) };

if let Some((kptr, vptr)) = shard.get_key_value(&key) {
if let Some((kptr, vptr)) = shard.raw_entry().from_key_hashed_nocheck(hash, &key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.as_ptr();
Expand All @@ -1079,16 +1095,16 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
}

fn _try_entry(&'a self, key: K) -> Option<Entry<'a, K, V, S>> {
let hash = self.hash_usize(&key);
let hash = self.hash_u64(&key);

let idx = self.determine_shard(hash);
let idx = self.determine_shard(hash as usize);

let shard = match unsafe { self._try_yield_write_shard(idx) } {
Some(shard) => shard,
None => return None,
};

if let Some((kptr, vptr)) = shard.get_key_value(&key) {
if let Some((kptr, vptr)) = shard.raw_entry().from_key_hashed_nocheck(hash, &key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.as_ptr();
Expand Down
27 changes: 18 additions & 9 deletions src/read_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> ReadOnlyView<K, V, S>
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.map.hash_usize(&key);
let hash = self.map.hash_u64(&key);

let idx = self.map.determine_shard(hash);
let idx = self.map.determine_shard(hash as usize);

let shard = unsafe { self.map._get_read_shard(idx) };

shard.contains_key(key)
shard
.raw_entry()
.from_key_hashed_nocheck(hash, key)
.is_some()
}

/// Returns a reference to the value corresponding to the key.
Expand All @@ -74,13 +77,16 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> ReadOnlyView<K, V, S>
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.map.hash_usize(&key);
let hash = self.map.hash_u64(&key);

let idx = self.map.determine_shard(hash);
let idx = self.map.determine_shard(hash as usize);

let shard = unsafe { self.map._get_read_shard(idx) };

shard.get(key).map(|v| v.get())
shard
.raw_entry()
.from_key_hashed_nocheck(hash, key)
.map(|(_k, v)| v.get())
}

/// Returns the key-value pair corresponding to the supplied key.
Expand All @@ -89,13 +95,16 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> ReadOnlyView<K, V, S>
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.map.hash_usize(&key);
let hash = self.map.hash_u64(&key);

let idx = self.map.determine_shard(hash);
let idx = self.map.determine_shard(hash as usize);

let shard = unsafe { self.map._get_read_shard(idx) };

shard.get_key_value(key).map(|(k, v)| (k, v.get()))
shard
.raw_entry()
.from_key_hashed_nocheck(hash, key)
.map(|(k, v)| (k, v.get()))
}

fn shard_read_iter(&'a self) -> impl Iterator<Item = &'a HashMap<K, V, S>> + 'a {
Expand Down