diff options
| author | John Kåre Alsaker <john.kare.alsaker@gmail.com> | 2023-08-16 10:00:25 +0200 |
|---|---|---|
| committer | John Kåre Alsaker <john.kare.alsaker@gmail.com> | 2023-08-16 10:00:25 +0200 |
| commit | 81220c0acef24bd88e960a9b2c2eea3140365864 (patch) | |
| tree | 6599d9575936a57e73792aea001129abcb741dba | |
| parent | c737c62e70cf8627e01e0b3a1088e96bff1b154d (diff) | |
Keep SHARDS fixed instead of a function of `cfg!(parallel_compiler)`
| -rw-r--r-- | compiler/rustc_data_structures/src/sharded.rs | 23 | ||||
| -rw-r--r-- | compiler/rustc_query_system/src/dep_graph/graph.rs | 2 |
2 files changed, 20 insertions, 5 deletions
diff --git a/compiler/rustc_data_structures/src/sharded.rs b/compiler/rustc_data_structures/src/sharded.rs index 6bc0b346d92..39cce5c8ee6 100644 --- a/compiler/rustc_data_structures/src/sharded.rs +++ b/compiler/rustc_data_structures/src/sharded.rs @@ -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> { diff --git a/compiler/rustc_query_system/src/dep_graph/graph.rs b/compiler/rustc_query_system/src/dep_graph/graph.rs index 30422ea1102..0d4d13ac20d 100644 --- a/compiler/rustc_query_system/src/dep_graph/graph.rs +++ b/compiler/rustc_query_system/src/dep_graph/graph.rs @@ -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(), ) }), |
