#![forbid(
missing_docs,
unsafe_op_in_unsafe_fn,
clippy::missing_safety_doc,
clippy::multiple_unsafe_ops_per_block,
clippy::undocumented_unsafe_blocks
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
mod lock;
use crossbeam_utils::CachePadded;
#[cfg(feature = "get-size2")]
use get_size2::{GetSize, GetSizeTracker};
use hashbrown::{HashTable, hash_table};
use lock::{RwLock, RwLockReadGuardDetached, RwLockWriteGuardDetached};
use std::ops::Deref;
use std::sync::LazyLock;
fn default_shard_shift() -> u32 {
static DEFAULT_SHARD_SHIFT: LazyLock<u32> =
LazyLock::new(|| shard_shift(std::thread::available_parallelism().map_or(1, usize::from)));
*DEFAULT_SHARD_SHIFT
}
fn shard_shift(parallelism: usize) -> u32 {
if parallelism == 0 {
1
} else {
((parallelism.saturating_mul(4) - 1).ilog2() + 1).min(usize::BITS - 1)
}
}
fn capacity_per_shard(capacity: usize, shard_shift: u32) -> usize {
let shard_amount = 1 << shard_shift;
capacity.div_ceil(shard_amount)
}
pub struct DashTable<T> {
shift: u32,
shards: Box<[CachePadded<RwLock<HashTable<T>>>]>,
}
impl<T> Default for DashTable<T> {
fn default() -> Self {
Self::new()
}
}
#[cfg(feature = "get-size2")]
impl<T: GetSize> GetSize for DashTable<T> {
fn get_heap_size_with_tracker<Tr: GetSizeTracker>(&self, tracker: Tr) -> (usize, Tr) {
let (size, tracker) = self
.shards
.iter()
.fold((0, tracker), |(size, tracker), table| {
let (table_size, tracker) =
HashTable::get_size_with_tracker(table.read().deref(), tracker);
(size + table_size, tracker)
});
let allocation_size =
self.shards.len() * std::mem::size_of::<CachePadded<RwLock<HashTable<T>>>>();
(size + allocation_size, tracker)
}
}
impl<T> DashTable<T> {
pub fn new() -> Self {
let shard_shift = default_shard_shift();
assert!(shard_shift > 0);
assert!(shard_shift < usize::BITS);
let shard_amount = 1 << shard_shift;
let shift = usize::BITS - shard_shift;
let shards = (0..shard_amount)
.map(|_| CachePadded::new(RwLock::new(HashTable::new())))
.collect();
Self { shift, shards }
}
pub fn with_capacity(capacity: usize) -> Self {
let shard_shift = default_shard_shift();
assert!(shard_shift > 0);
assert!(shard_shift < usize::BITS);
let shard_amount = 1 << shard_shift;
let shift = usize::BITS - shard_shift;
let cps = capacity_per_shard(capacity, shard_shift);
let shards = (0..shard_amount)
.map(|_| CachePadded::new(RwLock::new(HashTable::with_capacity(cps))))
.collect();
Self { shift, shards }
}
pub fn entry<'a>(
&'a self,
hash: u64,
eq: impl FnMut(&T) -> bool,
hasher: impl Fn(&T) -> u64,
) -> Entry<'a, T> {
let shard = self.determine_shard(hash as usize);
let guard = self.shards[shard].write();
let (_guard, shard) = unsafe { guard.detach() };
match shard.entry(hash, eq, hasher) {
hash_table::Entry::Occupied(entry) => Entry::Occupied(OccupiedEntry { _guard, entry }),
hash_table::Entry::Vacant(entry) => Entry::Vacant(VacantEntry { _guard, entry }),
}
}
pub fn entry_mut<'a>(
&'a mut self,
hash: u64,
eq: impl FnMut(&T) -> bool,
hasher: impl Fn(&T) -> u64,
) -> EntryMut<'a, T> {
let shard = self.determine_shard(hash as usize);
let table = self.shards[shard].get_mut();
table.entry(hash, eq, hasher)
}
pub fn find<'a>(&'a self, hash: u64, eq: impl FnMut(&T) -> bool) -> Option<Ref<'a, T>> {
let shard = self.determine_shard(hash as usize);
let guard = self.shards[shard].read();
let (_guard, shard) = unsafe { guard.detach() };
shard
.find(hash, eq)
.map(|reference| Ref { _guard, reference })
}
pub fn find_mut(&mut self, hash: u64, eq: impl FnMut(&T) -> bool) -> Option<&T> {
let shard = self.determine_shard(hash as usize);
let table = self.shards[shard].get_mut();
table.find(hash, eq)
}
pub fn insert_unique<'a>(
&'a self,
hash: u64,
value: T,
hasher: impl Fn(&T) -> u64,
) -> OccupiedEntry<'a, T> {
let shard = self.determine_shard(hash as usize);
let guard = self.shards[shard].write();
let (_guard, shard) = unsafe { guard.detach() };
let entry = shard.insert_unique(hash, value, hasher);
OccupiedEntry { _guard, entry }
}
pub fn insert_unique_mut<'a>(
&'a mut self,
hash: u64,
value: T,
hasher: impl Fn(&T) -> u64,
) -> OccupiedEntryMut<'a, T> {
let shard = self.determine_shard(hash as usize);
let table = self.shards[shard].get_mut();
table.insert_unique(hash, value, hasher)
}
fn determine_shard(&self, hash: usize) -> usize {
(hash << 7) >> self.shift
}
}
pub struct Ref<'a, T> {
_guard: RwLockReadGuardDetached<'a>,
reference: &'a T,
}
impl<'a, T> Deref for Ref<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.reference
}
}
pub enum Entry<'a, T> {
Occupied(OccupiedEntry<'a, T>),
Vacant(VacantEntry<'a, T>),
}
impl<'a, T> Entry<'a, T> {
pub fn or_insert_with(self, default: impl FnOnce() -> T) -> OccupiedEntry<'a, T> {
match self {
Entry::Occupied(entry) => entry,
Entry::Vacant(entry) => entry.insert(default()),
}
}
}
pub struct OccupiedEntry<'a, T> {
_guard: RwLockWriteGuardDetached<'a>,
entry: hash_table::OccupiedEntry<'a, T>,
}
impl<'a, T> OccupiedEntry<'a, T> {
pub fn get(&self) -> &T {
self.entry.get()
}
pub fn get_mut(&mut self) -> &mut T {
self.entry.get_mut()
}
pub fn remove(self) -> (T, VacantEntry<'a, T>) {
let (t, entry) = self.entry.remove();
(
t,
VacantEntry {
_guard: self._guard,
entry,
},
)
}
}
pub struct VacantEntry<'a, T> {
_guard: RwLockWriteGuardDetached<'a>,
entry: hash_table::VacantEntry<'a, T>,
}
impl<'a, T> VacantEntry<'a, T> {
pub fn insert(self, value: T) -> OccupiedEntry<'a, T> {
OccupiedEntry {
_guard: self._guard,
entry: self.entry.insert(value),
}
}
}
pub type EntryMut<'a, T> = hash_table::Entry<'a, T>;
pub type OccupiedEntryMut<'a, T> = hash_table::OccupiedEntry<'a, T>;
#[cfg(test)]
mod test {
use super::*;
use std::hash::{BuildHasher, RandomState};
use std::thread;
#[test]
fn test_shard_shift() {
assert_eq!(shard_shift(0), 1);
assert_eq!(shard_shift(1), 2);
assert_eq!(shard_shift(2), 3);
assert_eq!(shard_shift(3), 4);
assert_eq!(shard_shift(4), 4);
assert_eq!(shard_shift(5), 5);
assert_eq!(shard_shift(6), 5);
assert_eq!(shard_shift(7), 5);
assert_eq!(shard_shift(8), 5);
assert_eq!(shard_shift(9), 6);
assert_eq!(shard_shift(10), 6);
assert_eq!(shard_shift(usize::MAX), usize::BITS - 1);
}
#[test]
fn test_capacity_per_shard() {
assert_eq!(capacity_per_shard(0, 2), 0);
assert_eq!(capacity_per_shard(1, 2), 1);
assert_eq!(capacity_per_shard(2, 2), 1);
assert_eq!(capacity_per_shard(3, 2), 1);
assert_eq!(capacity_per_shard(4, 2), 1);
assert_eq!(capacity_per_shard(5, 2), 2);
assert_eq!(capacity_per_shard(42, 2), 11);
assert_eq!(capacity_per_shard(usize::MAX, 2), 1 << (usize::BITS - 2));
}
const NUM_READERS: usize = 4;
const NUM_WRITERS: usize = 4;
#[cfg(not(miri))]
const NUM_ITEMS: usize = 100_000;
#[cfg(miri)]
const NUM_ITEMS: usize = 100;
#[test]
fn test_insert_unique() {
let mut table = DashTable::default();
let hasher = RandomState::default();
thread::scope(|s| {
let table = &table;
let hasher = &hasher;
for i in 0..NUM_WRITERS {
s.spawn(move || {
for j in 0..NUM_ITEMS {
let value = i * NUM_ITEMS + j;
assert_eq!(
*table
.insert_unique(hasher.hash_one(value), value, |&x| {
hasher.hash_one(x)
})
.get(),
value,
);
}
});
}
});
for value in 0..NUM_WRITERS * NUM_ITEMS {
assert_eq!(
table.find_mut(hasher.hash_one(value), |&x| x == value),
Some(&value)
);
}
}
#[test]
fn test_with_capacity_insert_unique() {
let mut table = DashTable::with_capacity(NUM_WRITERS * NUM_ITEMS);
let hasher = RandomState::default();
thread::scope(|s| {
let table = &table;
let hasher = &hasher;
for i in 0..NUM_WRITERS {
s.spawn(move || {
for j in 0..NUM_ITEMS {
let value = i * NUM_ITEMS + j;
assert_eq!(
*table
.insert_unique(hasher.hash_one(value), value, |&x| {
hasher.hash_one(x)
})
.get(),
value,
);
}
});
}
});
for value in 0..NUM_WRITERS * NUM_ITEMS {
assert_eq!(
table.find_mut(hasher.hash_one(value), |&x| x == value),
Some(&value)
);
}
}
#[test]
fn test_entry_or_insert_with() {
let mut table: DashTable<(usize, usize)> = DashTable::default();
let hasher = RandomState::default();
thread::scope(|s| {
let table = &table;
let hasher = &hasher;
for _ in 0..NUM_WRITERS {
s.spawn(move || {
for value in 0..NUM_ITEMS {
table
.entry(
hasher.hash_one(value),
|x| x.0 == value,
|x| hasher.hash_one(x.0),
)
.or_insert_with(|| (value, 0))
.get_mut()
.1 += 1;
}
});
}
});
for value in 0..NUM_ITEMS {
assert_eq!(
table.find_mut(hasher.hash_one(value), |x| x.0 == value),
Some(&(value, NUM_WRITERS))
);
}
}
#[test]
fn test_entry_remove() {
let mut table = DashTable::default();
let hasher = RandomState::default();
for value in 0..NUM_ITEMS {
assert_eq!(
*table
.insert_unique_mut(hasher.hash_one(value), value, |&x| { hasher.hash_one(x) })
.get(),
value
);
}
thread::scope(|s| {
let table = &table;
let hasher = &hasher;
for _ in 0..NUM_WRITERS {
s.spawn(move || {
for value in 0..NUM_ITEMS {
if let Entry::Occupied(entry) = table.entry(
hasher.hash_one(value),
|&x| x == value,
|&x| hasher.hash_one(x),
) {
assert_eq!(entry.remove().0, value);
}
}
});
}
});
for value in 0..NUM_ITEMS {
assert_eq!(
table.find_mut(hasher.hash_one(value), |&x| x == value),
None
);
}
}
#[test]
fn test_entry_mut() {
let mut table: DashTable<(usize, usize)> = DashTable::default();
let hasher = RandomState::default();
for _ in 0..NUM_WRITERS {
for value in 0..NUM_ITEMS {
table
.entry_mut(
hasher.hash_one(value),
|x| x.0 == value,
|x| hasher.hash_one(x.0),
)
.or_insert_with(|| (value, 0))
.get_mut()
.1 += 1;
}
}
for value in 0..NUM_ITEMS {
assert_eq!(
table.find_mut(hasher.hash_one(value), |x| x.0 == value),
Some(&(value, NUM_WRITERS))
);
}
}
#[test]
fn test_find() {
let mut table = DashTable::default();
let hasher = RandomState::default();
for value in 0..NUM_ITEMS {
assert_eq!(
*table
.insert_unique_mut(hasher.hash_one(value), value, |&x| { hasher.hash_one(x) })
.get(),
value
);
}
thread::scope(|s| {
let table = &table;
let hasher = &hasher;
for _ in 0..NUM_READERS {
s.spawn(move || {
for value in 0..NUM_ITEMS {
assert_eq!(
table
.find(hasher.hash_one(value), |&x| x == value)
.as_deref(),
Some(&value)
);
}
});
}
});
}
#[test]
fn test_concurrent_insert_unique_find() {
let mut table: DashTable<usize> = DashTable::default();
let hasher = RandomState::default();
thread::scope(|s| {
let table = &table;
let hasher = &hasher;
for _ in 0..NUM_READERS {
s.spawn(move || {
'outer: loop {
for value in 0..NUM_WRITERS * NUM_ITEMS {
match table.find(hasher.hash_one(value), |&x| x == value) {
None => continue 'outer,
Some(v) => assert_eq!(*v.deref(), value),
}
}
break;
}
});
}
for i in 0..NUM_WRITERS {
s.spawn(move || {
for j in 0..NUM_ITEMS {
let value = i * NUM_ITEMS + j;
assert_eq!(
*table
.insert_unique(hasher.hash_one(value), value, |&x| {
hasher.hash_one(x)
})
.get(),
value,
);
}
});
}
});
for value in 0..NUM_WRITERS * NUM_ITEMS {
assert_eq!(
table.find_mut(hasher.hash_one(value), |&x| x == value),
Some(&value)
);
}
}
}