Keep SHARDS fixed instead of a function of cfg!(parallel_compiler)

This commit is contained in:
John Kåre Alsaker 2023-08-16 10:00:25 +02:00
parent c737c62e70
commit 81220c0ace
2 changed files with 20 additions and 5 deletions

View File

@ -10,9 +10,10 @@ use std::mem;
// 32 shards is sufficient to reduce contention on an 8-core Ryzen 7 1700,
// but this should be tested on higher core count CPUs. How the `Sharded` type gets used
// may also affect the ideal number of shards.
const SHARD_BITS: usize = if cfg!(parallel_compiler) { 5 } else { 0 };
const SHARD_BITS: usize = 5;
pub const SHARDS: usize = 1 << SHARD_BITS;
#[cfg(parallel_compiler)]
const SHARDS: usize = 1 << SHARD_BITS;
/// An array of cache-line aligned inner locked structures with convenience methods.
/// A single field is used when the compiler uses only one thread.
@ -44,8 +45,12 @@ impl<T> Sharded<T> {
/// The shard is selected by hashing `val` with `FxHasher`.
#[inline]
pub fn get_shard_by_value<K: Hash + ?Sized>(&self, val: &K) -> &Lock<T> {
self.get_shard_by_hash(if SHARDS == 1 { 0 } else { make_hash(val) })
pub fn get_shard_by_value<K: Hash + ?Sized>(&self, _val: &K) -> &Lock<T> {
match self {
Self::Single(single) => &single,
#[cfg(parallel_compiler)]
Self::Shards(shards) => self.get_shard_by_hash(make_hash(_val)),
}
}
#[inline]
@ -83,6 +88,16 @@ impl<T> Sharded<T> {
}
}
#[inline]
pub fn shards() -> usize {
#[cfg(parallel_compiler)]
if is_dyn_thread_safe() {
return SHARDS;
}
1
}
pub type ShardedHashMap<K, V> = Sharded<FxHashMap<K, V>>;
impl<K: Eq, V> ShardedHashMap<K, V> {

View File

@ -1166,7 +1166,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
)),
new_node_to_index: Sharded::new(|| {
FxHashMap::with_capacity_and_hasher(
new_node_count_estimate / sharded::SHARDS,
new_node_count_estimate / sharded::shards(),
Default::default(),
)
}),