summary refs log tree commit diff
path: root/compiler/rustc_data_structures
diff options
context:
space:
mode:
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2023-08-16 10:00:25 +0200
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2023-08-16 10:00:25 +0200
commit81220c0acef24bd88e960a9b2c2eea3140365864 (patch)
tree6599d9575936a57e73792aea001129abcb741dba /compiler/rustc_data_structures
parentc737c62e70cf8627e01e0b3a1088e96bff1b154d (diff)
downloadrust-81220c0acef24bd88e960a9b2c2eea3140365864.tar.gz
rust-81220c0acef24bd88e960a9b2c2eea3140365864.zip
Keep SHARDS fixed instead of a function of `cfg!(parallel_compiler)`
Diffstat (limited to 'compiler/rustc_data_structures')
-rw-r--r--compiler/rustc_data_structures/src/sharded.rs23
1 files changed, 19 insertions, 4 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> {