This commit is contained in:
Acrimon 2020-08-04 19:52:14 +02:00
parent ceee3769fc
commit 8a14a408a5
No known key found for this signature in database
GPG Key ID: 79B55D369EAD2A06
2 changed files with 13 additions and 6 deletions

View File

@ -18,6 +18,7 @@ raw-api = []
no_std = ["hashbrown"]
[dependencies]
num_cpus = "1.13.0"
ahash = "0.3.8"
serde = { version = "1.0.114", optional = true, features = ["derive"] }
cfg-if = "0.1.10"

View File

@ -49,8 +49,13 @@ cfg_if! {
}
}
const SHARD_AMOUNT: usize = 8;
const NCB: usize = SHARD_AMOUNT.trailing_zeros() as usize;
fn shard_amount() -> usize {
(num_cpus::get() * 4).next_power_of_two()
}
fn ncb(shard_amount: usize) -> usize {
shard_amount.trailing_zeros() as usize
}
/// DashMap is an implementation of a concurrent associative array/hashmap in Rust.
///
@ -167,15 +172,16 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
/// ```
pub fn with_capacity_and_hasher(mut capacity: usize, hasher: S) -> Self {
let shift = util::ptr_size_bits() - NCB;
let shard_amount = shard_amount();
let shift = util::ptr_size_bits() - ncb(shard_amount);
if capacity != 0 {
capacity = (capacity + (SHARD_AMOUNT - 1)) & !(SHARD_AMOUNT - 1);
capacity = (capacity + (shard_amount - 1)) & !(shard_amount - 1);
}
let cps = capacity / SHARD_AMOUNT;
let cps = capacity / shard_amount;
let shards = (0..SHARD_AMOUNT)
let shards = (0..shard_amount)
.map(|_| RwLock::new(HashMap::with_capacity_and_hasher(cps, hasher.clone())))
.collect();