Skip to content

Commit 594ea74

Browse files
Refactor Sharded out of non-parallel active query map
1 parent 41f124c commit 594ea74

File tree

1 file changed

+51
-27
lines changed

1 file changed

+51
-27
lines changed

compiler/rustc_query_system/src/query/plumbing.rs

+51-27
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ use crate::query::config::{QueryDescription, QueryVtable};
88
use crate::query::job::{report_cycle, QueryInfo, QueryJob, QueryJobId, QueryJobInfo};
99
use crate::query::{QueryContext, QueryMap, QuerySideEffects, QueryStackFrame};
1010
use rustc_data_structures::fingerprint::Fingerprint;
11-
use rustc_data_structures::fx::{FxHashMap, FxHasher};
11+
use rustc_data_structures::fx::FxHashMap;
1212
#[cfg(parallel_compiler)]
1313
use rustc_data_structures::profiling::TimingGuard;
14-
use rustc_data_structures::sharded::{get_shard_index_by_hash, Sharded};
14+
#[cfg(parallel_compiler)]
15+
use rustc_data_structures::sharded::Sharded;
1516
use rustc_data_structures::sync::Lock;
1617
use rustc_data_structures::thin_vec::ThinVec;
1718
use rustc_errors::{DiagnosticBuilder, FatalError};
@@ -20,21 +21,15 @@ use rustc_span::{Span, DUMMY_SP};
2021
use std::cell::Cell;
2122
use std::collections::hash_map::Entry;
2223
use std::fmt::Debug;
23-
use std::hash::{Hash, Hasher};
24+
use std::hash::Hash;
2425
use std::mem;
2526
use std::ptr;
2627

27-
// We compute the key's hash once and then use it for both the
28-
// shard lookup and the hashmap lookup. This relies on the fact
29-
// that both of them use `FxHasher`.
30-
fn hash_for_shard<K: Hash>(key: &K) -> u64 {
31-
let mut hasher = FxHasher::default();
32-
key.hash(&mut hasher);
33-
hasher.finish()
34-
}
35-
3628
pub struct QueryState<K> {
37-
shards: Sharded<FxHashMap<K, QueryResult>>,
29+
#[cfg(parallel_compiler)]
30+
active: Sharded<FxHashMap<K, QueryResult>>,
31+
#[cfg(not(parallel_compiler))]
32+
active: Lock<FxHashMap<K, QueryResult>>,
3833
}
3934

4035
/// Indicates the state of a query for a given key in a query map.
@@ -52,8 +47,15 @@ where
5247
K: Eq + Hash + Clone + Debug,
5348
{
5449
pub fn all_inactive(&self) -> bool {
55-
let shards = self.shards.lock_shards();
56-
shards.iter().all(|shard| shard.is_empty())
50+
#[cfg(parallel_compiler)]
51+
{
52+
let shards = self.active.lock_shards();
53+
shards.iter().all(|shard| shard.is_empty())
54+
}
55+
#[cfg(not(parallel_compiler))]
56+
{
57+
self.active.lock().is_empty()
58+
}
5759
}
5860

5961
pub fn try_collect_active_jobs<CTX: Copy>(
@@ -62,11 +64,27 @@ where
6264
make_query: fn(CTX, K) -> QueryStackFrame,
6365
jobs: &mut QueryMap,
6466
) -> Option<()> {
65-
// We use try_lock_shards here since we are called from the
66-
// deadlock handler, and this shouldn't be locked.
67-
let shards = self.shards.try_lock_shards()?;
68-
for shard in shards.iter() {
69-
for (k, v) in shard.iter() {
67+
#[cfg(parallel_compiler)]
68+
{
69+
// We use try_lock_shards here since we are called from the
70+
// deadlock handler, and this shouldn't be locked.
71+
let shards = self.active.try_lock_shards()?;
72+
for shard in shards.iter() {
73+
for (k, v) in shard.iter() {
74+
if let QueryResult::Started(ref job) = *v {
75+
let query = make_query(tcx, k.clone());
76+
jobs.insert(job.id, QueryJobInfo { query, job: job.clone() });
77+
}
78+
}
79+
}
80+
}
81+
#[cfg(not(parallel_compiler))]
82+
{
83+
// We use try_lock here since we are called from the
84+
// deadlock handler, and this shouldn't be locked.
85+
// (FIXME: Is this relevant for non-parallel compilers? It doesn't
86+
// really hurt much.)
87+
for (k, v) in self.active.try_lock()?.iter() {
7088
if let QueryResult::Started(ref job) = *v {
7189
let query = make_query(tcx, k.clone());
7290
jobs.insert(job.id, QueryJobInfo { query, job: job.clone() });
@@ -80,7 +98,7 @@ where
8098

8199
impl<K> Default for QueryState<K> {
82100
fn default() -> QueryState<K> {
83-
QueryState { shards: Default::default() }
101+
QueryState { active: Default::default() }
84102
}
85103
}
86104

@@ -135,7 +153,10 @@ where
135153
where
136154
CTX: QueryContext,
137155
{
138-
let mut state_lock = state.shards.get_shard_by_value(&key).lock();
156+
#[cfg(parallel_compiler)]
157+
let mut state_lock = state.active.get_shard_by_value(&key).lock();
158+
#[cfg(not(parallel_compiler))]
159+
let mut state_lock = state.active.lock();
139160
let lock = &mut *state_lock;
140161

141162
match lock.entry(key) {
@@ -206,10 +227,11 @@ where
206227
mem::forget(self);
207228

208229
let (job, result) = {
209-
let key_hash = hash_for_shard(&key);
210-
let shard = get_shard_index_by_hash(key_hash);
211230
let job = {
212-
let mut lock = state.shards.get_shard_by_index(shard).lock();
231+
#[cfg(parallel_compiler)]
232+
let mut lock = state.active.get_shard_by_value(&key).lock();
233+
#[cfg(not(parallel_compiler))]
234+
let mut lock = state.active.lock();
213235
match lock.remove(&key).unwrap() {
214236
QueryResult::Started(job) => job,
215237
QueryResult::Poisoned => panic!(),
@@ -233,9 +255,11 @@ where
233255
fn drop(&mut self) {
234256
// Poison the query so jobs waiting on it panic.
235257
let state = self.state;
236-
let shard = state.shards.get_shard_by_value(&self.key);
237258
let job = {
238-
let mut shard = shard.lock();
259+
#[cfg(parallel_compiler)]
260+
let mut shard = state.active.get_shard_by_value(&self.key).lock();
261+
#[cfg(not(parallel_compiler))]
262+
let mut shard = state.active.lock();
239263
let job = match shard.remove(&self.key).unwrap() {
240264
QueryResult::Started(job) => job,
241265
QueryResult::Poisoned => panic!(),

0 commit comments

Comments
 (0)