chore: get rid of num_cpus and hardcode a sensible shard number

This commit is contained in:
Acrimon 2020-07-31 23:15:29 +02:00
parent 7abb7a4699
commit df4b09ba01
No known key found for this signature in database
GPG Key ID: 79B55D369EAD2A06
2 changed files with 6 additions and 14 deletions

View File

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

View File

@ -49,13 +49,8 @@ cfg_if! {
} }
} }
fn shard_amount() -> usize { const SHARD_AMOUNT: usize = 8;
(num_cpus::get() * 4).next_power_of_two() const NCB: usize = SHARD_AMOUNT.trailing_zeros() as usize;
}
fn ncb(shard_amount: usize) -> usize {
shard_amount.trailing_zeros() as usize
}
/// DashMap is an implementation of a concurrent associative array/hashmap in Rust. /// DashMap is an implementation of a concurrent associative array/hashmap in Rust.
/// ///
@ -172,17 +167,15 @@ 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 { pub fn with_capacity_and_hasher(mut capacity: usize, hasher: S) -> Self {
let shard_amount = shard_amount(); let shift = util::ptr_size_bits() - NCB;
let shift = util::ptr_size_bits() - ncb(shard_amount);
if capacity != 0 { 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()))) .map(|_| RwLock::new(HashMap::with_capacity_and_hasher(cps, hasher.clone())))
.collect(); .collect();