Skip to content

Commit d71085c

Browse files
committed
refactor Sharded and not fetch pre_compiled_cgus whtn single thread
1 parent f97437e commit d71085c

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
679679
// This likely is a temporary measure. Once we don't have to support the
680680
// non-parallel compiler anymore, we can compile CGUs end-to-end in
681681
// parallel and get rid of the complicated scheduling logic.
682-
let mut pre_compiled_cgus = if cfg!(parallel_compiler) {
682+
let mut pre_compiled_cgus = if rustc_data_structures::sync::active() {
683683
tcx.sess.time("compile_first_CGU_batch", || {
684684
// Try to find one CGU to compile per thread.
685685
let cgus: Vec<_> = cgu_reuse

compiler/rustc_data_structures/src/sharded.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::fx::{FxHashMap, FxHasher};
2-
use crate::sync::{Lock, LockGuard};
2+
use crate::sync::{active, Lock, LockGuard};
33
use std::borrow::Borrow;
44
use std::collections::hash_map::RawEntryMut;
55
use std::hash::{Hash, Hasher};
@@ -23,6 +23,7 @@ pub const SHARDS: usize = 1 << SHARD_BITS;
2323
/// An array of cache-line aligned inner locked structures with convenience methods.
2424
pub struct Sharded<T> {
2525
shards: [CacheAligned<Lock<T>>; SHARDS],
26+
single_thread: bool,
2627
}
2728

2829
impl<T: Default> Default for Sharded<T> {
@@ -35,31 +36,41 @@ impl<T: Default> Default for Sharded<T> {
3536
impl<T> Sharded<T> {
3637
#[inline]
3738
pub fn new(mut value: impl FnMut() -> T) -> Self {
38-
Sharded { shards: [(); SHARDS].map(|()| CacheAligned(Lock::new(value()))) }
39+
Sharded {
40+
shards: [(); SHARDS].map(|()| CacheAligned(Lock::new(value()))),
41+
single_thread: !active(),
42+
}
3943
}
4044

4145
/// The shard is selected by hashing `val` with `FxHasher`.
4246
#[inline]
4347
pub fn get_shard_by_value<K: Hash + ?Sized>(&self, val: &K) -> &Lock<T> {
44-
if SHARDS == 1 { &self.shards[0].0 } else { self.get_shard_by_hash(make_hash(val)) }
48+
if self.single_thread { &self.shards[0].0 } else { self.get_shard_by_hash(make_hash(val)) }
4549
}
4650

4751
#[inline]
4852
pub fn get_shard_by_hash(&self, hash: u64) -> &Lock<T> {
49-
&self.shards[get_shard_index_by_hash(hash)].0
50-
}
51-
52-
#[inline]
53-
pub fn get_shard_by_index(&self, i: usize) -> &Lock<T> {
54-
&self.shards[i].0
53+
if self.single_thread {
54+
&self.shards[0].0
55+
} else {
56+
&self.shards[get_shard_index_by_hash(hash)].0
57+
}
5558
}
5659

5760
pub fn lock_shards(&self) -> Vec<LockGuard<'_, T>> {
58-
(0..SHARDS).map(|i| self.shards[i].0.lock()).collect()
61+
if self.single_thread {
62+
vec![self.shards[0].0.lock()]
63+
} else {
64+
(0..SHARDS).map(|i| self.shards[i].0.lock()).collect()
65+
}
5966
}
6067

6168
pub fn try_lock_shards(&self) -> Option<Vec<LockGuard<'_, T>>> {
62-
(0..SHARDS).map(|i| self.shards[i].0.try_lock()).collect()
69+
if self.single_thread {
70+
Some(vec![self.shards[0].0.try_lock()?])
71+
} else {
72+
(0..SHARDS).map(|i| self.shards[i].0.try_lock()).collect()
73+
}
6374
}
6475
}
6576

@@ -141,7 +152,7 @@ pub fn make_hash<K: Hash + ?Sized>(val: &K) -> u64 {
141152
/// consistently for each `Sharded` instance.
142153
#[inline]
143154
#[allow(clippy::modulo_one)]
144-
pub fn get_shard_index_by_hash(hash: u64) -> usize {
155+
fn get_shard_index_by_hash(hash: u64) -> usize {
145156
let hash_len = mem::size_of::<usize>();
146157
// Ignore the top 7 bits as hashbrown uses these and get the next SHARD_BITS highest bits.
147158
// hashbrown also uses the lowest bits, so we can't use those

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,12 @@ impl<K: DepKind> CurrentDepGraph<K> {
11591159
.get_or_alloc_cached_string("incr_comp_intern_dep_graph_node")
11601160
.map(EventId::from_label);
11611161

1162+
let capacity = if rustc_data_structures::sync::active() {
1163+
new_node_count_estimate / sharded::SHARDS
1164+
} else {
1165+
new_node_count_estimate
1166+
};
1167+
11621168
CurrentDepGraph {
11631169
encoder: Steal::new(GraphEncoder::new(
11641170
encoder,
@@ -1167,10 +1173,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
11671173
record_stats,
11681174
)),
11691175
new_node_to_index: Sharded::new(|| {
1170-
FxHashMap::with_capacity_and_hasher(
1171-
new_node_count_estimate / sharded::SHARDS,
1172-
Default::default(),
1173-
)
1176+
FxHashMap::with_capacity_and_hasher(capacity, Default::default())
11741177
}),
11751178
prev_index_to_index: Lock::new(IndexVec::from_elem_n(None, prev_graph_node_count)),
11761179
anon_id_seed,

0 commit comments

Comments
 (0)