Skip to content

Commit 97facd0

Browse files
committed
more with shard
1 parent e4e97ae commit 97facd0

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

compiler/rustc_data_structures/src/sharded.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,21 @@ impl<T: Default> Default for Sharded<T> {
3131
}
3232
}
3333

34-
impl<T> Sharded<T> {
34+
impl<T: Default> Sharded<T> {
3535
#[inline]
3636
pub fn new(mut value: impl FnMut() -> T) -> Self {
37-
Sharded {
38-
single_thread: !active(),
39-
shard: Lock::new(value()),
40-
shards: [(); SHARDS].map(|()| CacheAligned(Lock::new(value()))),
37+
if likely(!active()) {
38+
Sharded {
39+
single_thread: !active(),
40+
shard: Lock::new(value()),
41+
shards: [(); SHARDS].map(|()| CacheAligned(Lock::new(T::default()))),
42+
}
43+
} else {
44+
Sharded {
45+
single_thread: !active(),
46+
shard: Lock::new(T::default()),
47+
shards: [(); SHARDS].map(|()| CacheAligned(Lock::new(value()))),
48+
}
4149
}
4250
}
4351

@@ -59,16 +67,6 @@ impl<T> Sharded<T> {
5967
}
6068
}
6169

62-
/// The shard is selected by hashing `val` with `FxHasher`.
63-
#[inline]
64-
pub fn get_shard_by_value<K: Hash + ?Sized>(&self, val: &K) -> &Lock<T> {
65-
if likely(self.single_thread) {
66-
&self.shard
67-
} else {
68-
&self.shards[get_shard_index_by_hash(make_hash(val))].0
69-
}
70-
}
71-
7270
#[inline]
7371
pub fn with_get_shard_by_hash<F: FnOnce(&mut T) -> R, R>(
7472
&self,

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
13331333
) {
13341334
let node = &prev_graph.index_to_node(prev_index);
13351335
debug_assert!(
1336-
!self.new_node_to_index.get_shard_by_value(node).lock().contains_key(node),
1336+
!self.new_node_to_index.with_get_shard_by_value(node, |lock| lock.contains_key(node)),
13371337
"node from previous graph present in new node collection"
13381338
);
13391339
}

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,10 @@ where
161161
cache.complete(key, result, dep_node_index);
162162

163163
let job = {
164-
let mut lock = state.active.get_shard_by_value(&key).lock();
165-
166-
match lock.remove(&key).unwrap() {
164+
state.active.with_get_shard_by_value(&key, |lock| match lock.remove(&key).unwrap() {
167165
QueryResult::Started(job) => job,
168166
QueryResult::Poisoned => panic!(),
169-
}
167+
})
170168
};
171169

172170
job.signal_complete();
@@ -184,14 +182,14 @@ where
184182
// Poison the query so jobs waiting on it panic.
185183
let state = self.state;
186184
let job = {
187-
let mut shard = state.active.get_shard_by_value(&self.key).lock();
188-
189-
let job = match shard.remove(&self.key).unwrap() {
190-
QueryResult::Started(job) => job,
191-
QueryResult::Poisoned => panic!(),
192-
};
193-
shard.insert(self.key, QueryResult::Poisoned);
194-
job
185+
state.active.with_get_shard_by_value(&self.key, |shard| {
186+
let job = match shard.remove(&self.key).unwrap() {
187+
QueryResult::Started(job) => job,
188+
QueryResult::Poisoned => panic!(),
189+
};
190+
shard.insert(self.key, QueryResult::Poisoned);
191+
job
192+
})
195193
};
196194
// Also signal the completion of the job, so waiters
197195
// will continue execution.

0 commit comments

Comments
 (0)