Skip to content

Commit 81220c0

Browse files
committed
Keep SHARDS fixed instead of a function of cfg!(parallel_compiler)
1 parent c737c62 commit 81220c0

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

compiler/rustc_data_structures/src/sharded.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use std::mem;
1010
// 32 shards is sufficient to reduce contention on an 8-core Ryzen 7 1700,
1111
// but this should be tested on higher core count CPUs. How the `Sharded` type gets used
1212
// may also affect the ideal number of shards.
13-
const SHARD_BITS: usize = if cfg!(parallel_compiler) { 5 } else { 0 };
13+
const SHARD_BITS: usize = 5;
1414

15-
pub const SHARDS: usize = 1 << SHARD_BITS;
15+
#[cfg(parallel_compiler)]
16+
const SHARDS: usize = 1 << SHARD_BITS;
1617

1718
/// An array of cache-line aligned inner locked structures with convenience methods.
1819
/// A single field is used when the compiler uses only one thread.
@@ -44,8 +45,12 @@ impl<T> Sharded<T> {
4445

4546
/// The shard is selected by hashing `val` with `FxHasher`.
4647
#[inline]
47-
pub fn get_shard_by_value<K: Hash + ?Sized>(&self, val: &K) -> &Lock<T> {
48-
self.get_shard_by_hash(if SHARDS == 1 { 0 } else { make_hash(val) })
48+
pub fn get_shard_by_value<K: Hash + ?Sized>(&self, _val: &K) -> &Lock<T> {
49+
match self {
50+
Self::Single(single) => &single,
51+
#[cfg(parallel_compiler)]
52+
Self::Shards(shards) => self.get_shard_by_hash(make_hash(_val)),
53+
}
4954
}
5055

5156
#[inline]
@@ -83,6 +88,16 @@ impl<T> Sharded<T> {
8388
}
8489
}
8590

91+
#[inline]
92+
pub fn shards() -> usize {
93+
#[cfg(parallel_compiler)]
94+
if is_dyn_thread_safe() {
95+
return SHARDS;
96+
}
97+
98+
1
99+
}
100+
86101
pub type ShardedHashMap<K, V> = Sharded<FxHashMap<K, V>>;
87102

88103
impl<K: Eq, V> ShardedHashMap<K, V> {

compiler/rustc_query_system/src/dep_graph/graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
11661166
)),
11671167
new_node_to_index: Sharded::new(|| {
11681168
FxHashMap::with_capacity_and_hasher(
1169-
new_node_count_estimate / sharded::SHARDS,
1169+
new_node_count_estimate / sharded::shards(),
11701170
Default::default(),
11711171
)
11721172
}),

0 commit comments

Comments
 (0)