feat: memoize default shard amount (#222)

Use once_cell crate to memoize the value.
This doesn't add any new dependencies, as once_cell was already a subdependency. Additionally it was merged into std in nighlty and awaiting stabilization.
This commit is contained in:
Tom Karwowski 2022-08-30 20:05:07 +02:00 committed by GitHub
parent 030671060f
commit 9deeb37ccb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -23,6 +23,7 @@ hashbrown = { version = "0.12.0", default-features = false }
serde = { version = "1.0.136", optional = true, features = ["derive"] }
cfg-if = "1.0.0"
rayon = { version = "1.5.2", optional = true }
once_cell = "1.13.0"
[package.metadata.docs.rs]
features = ["rayon", "raw-api", "serde"]

View File

@ -35,6 +35,7 @@ use iter::{Iter, IterMut, OwningIter};
use mapref::entry::{Entry, OccupiedEntry, VacantEntry};
use mapref::multiple::RefMulti;
use mapref::one::{Ref, RefMut};
use once_cell::sync::OnceCell;
pub use read_only::ReadOnlyView;
pub use set::DashSet;
use std::collections::hash_map::RandomState;
@ -60,7 +61,10 @@ pub(crate) type HashMap<K, V, S> = hashbrown::HashMap<K, SharedValue<V>, S>;
pub struct TryReserveError {}
fn default_shard_amount() -> usize {
(std::thread::available_parallelism().map_or(1, usize::from) * 4).next_power_of_two()
static DEFAULT_SHARD_AMOUNT: OnceCell<usize> = OnceCell::new();
*DEFAULT_SHARD_AMOUNT.get_or_init(|| {
(std::thread::available_parallelism().map_or(1, usize::from) * 4).next_power_of_two()
})
}
fn ncb(shard_amount: usize) -> usize {