Skip to content

Commit 41f124c

Browse files
Avoid sharding query caches entirely in single-threaded mode
1 parent 8443816 commit 41f124c

File tree

1 file changed

+59
-20
lines changed
  • compiler/rustc_query_system/src/query

1 file changed

+59
-20
lines changed

compiler/rustc_query_system/src/query/caches.rs

+59-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ use crate::dep_graph::DepNodeIndex;
22

33
use rustc_arena::TypedArena;
44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_data_structures::sharded::{self, Sharded};
5+
use rustc_data_structures::sharded;
6+
#[cfg(parallel_compiler)]
7+
use rustc_data_structures::sharded::Sharded;
8+
#[cfg(not(parallel_compiler))]
9+
use rustc_data_structures::sync::Lock;
610
use rustc_data_structures::sync::WorkerLocal;
711
use std::default::Default;
812
use std::fmt::Debug;
@@ -50,12 +54,15 @@ impl<K: Eq + Hash, V: Clone> CacheSelector<K, V> for DefaultCacheSelector {
5054
}
5155

5256
pub struct DefaultCache<K, V> {
53-
shards: Sharded<FxHashMap<K, (V, DepNodeIndex)>>,
57+
#[cfg(parallel_compiler)]
58+
cache: Sharded<FxHashMap<K, (V, DepNodeIndex)>>,
59+
#[cfg(not(parallel_compiler))]
60+
cache: Lock<FxHashMap<K, (V, DepNodeIndex)>>,
5461
}
5562

5663
impl<K, V> Default for DefaultCache<K, V> {
5764
fn default() -> Self {
58-
DefaultCache { shards: Default::default() }
65+
DefaultCache { cache: Default::default() }
5966
}
6067
}
6168

@@ -83,8 +90,10 @@ where
8390
OnHit: FnOnce(&V, DepNodeIndex) -> R,
8491
{
8592
let key_hash = sharded::make_hash(key);
86-
let shard = sharded::get_shard_index_by_hash(key_hash);
87-
let lock = self.shards.get_shard_by_index(shard).lock();
93+
#[cfg(parallel_compiler)]
94+
let lock = self.cache.get_shard_by_hash(key_hash).lock();
95+
#[cfg(not(parallel_compiler))]
96+
let lock = self.cache.lock();
8897
let result = lock.raw_entry().from_key_hashed_nocheck(key_hash, key);
8998

9099
if let Some((_, value)) = result {
@@ -97,14 +106,28 @@ where
97106

98107
#[inline]
99108
fn complete(&self, key: K, value: V, index: DepNodeIndex) -> Self::Stored {
100-
self.shards.get_shard_by_value(&key).lock().insert(key, (value.clone(), index));
109+
#[cfg(parallel_compiler)]
110+
let mut lock = self.cache.get_shard_by_value(&key).lock();
111+
#[cfg(not(parallel_compiler))]
112+
let mut lock = self.cache.lock();
113+
lock.insert(key, (value.clone(), index));
101114
value
102115
}
103116

104117
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
105-
let shards = self.shards.lock_shards();
106-
for shard in shards.iter() {
107-
for (k, v) in shard.iter() {
118+
#[cfg(parallel_compiler)]
119+
{
120+
let shards = self.cache.lock_shards();
121+
for shard in shards.iter() {
122+
for (k, v) in shard.iter() {
123+
f(k, &v.0, v.1);
124+
}
125+
}
126+
}
127+
#[cfg(not(parallel_compiler))]
128+
{
129+
let map = self.cache.lock();
130+
for (k, v) in map.iter() {
108131
f(k, &v.0, v.1);
109132
}
110133
}
@@ -119,15 +142,15 @@ impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<K, V> for ArenaCacheSelector<'tc
119142

120143
pub struct ArenaCache<'tcx, K, V> {
121144
arena: WorkerLocal<TypedArena<(V, DepNodeIndex)>>,
122-
shards: Sharded<FxHashMap<K, &'tcx (V, DepNodeIndex)>>,
145+
#[cfg(parallel_compiler)]
146+
cache: Sharded<FxHashMap<K, &'tcx (V, DepNodeIndex)>>,
147+
#[cfg(not(parallel_compiler))]
148+
cache: Lock<FxHashMap<K, &'tcx (V, DepNodeIndex)>>,
123149
}
124150

125151
impl<'tcx, K, V> Default for ArenaCache<'tcx, K, V> {
126152
fn default() -> Self {
127-
ArenaCache {
128-
arena: WorkerLocal::new(|_| TypedArena::default()),
129-
shards: Default::default(),
130-
}
153+
ArenaCache { arena: WorkerLocal::new(|_| TypedArena::default()), cache: Default::default() }
131154
}
132155
}
133156

@@ -156,8 +179,10 @@ where
156179
OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
157180
{
158181
let key_hash = sharded::make_hash(key);
159-
let shard = sharded::get_shard_index_by_hash(key_hash);
160-
let lock = self.shards.get_shard_by_index(shard).lock();
182+
#[cfg(parallel_compiler)]
183+
let lock = self.cache.get_shard_by_hash(key_hash).lock();
184+
#[cfg(not(parallel_compiler))]
185+
let lock = self.cache.lock();
161186
let result = lock.raw_entry().from_key_hashed_nocheck(key_hash, key);
162187

163188
if let Some((_, value)) = result {
@@ -172,14 +197,28 @@ where
172197
fn complete(&self, key: K, value: V, index: DepNodeIndex) -> Self::Stored {
173198
let value = self.arena.alloc((value, index));
174199
let value = unsafe { &*(value as *const _) };
175-
self.shards.get_shard_by_value(&key).lock().insert(key, value);
200+
#[cfg(parallel_compiler)]
201+
let mut lock = self.cache.get_shard_by_value(&key).lock();
202+
#[cfg(not(parallel_compiler))]
203+
let mut lock = self.cache.lock();
204+
lock.insert(key, value);
176205
&value.0
177206
}
178207

179208
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
180-
let shards = self.shards.lock_shards();
181-
for shard in shards.iter() {
182-
for (k, v) in shard.iter() {
209+
#[cfg(parallel_compiler)]
210+
{
211+
let shards = self.cache.lock_shards();
212+
for shard in shards.iter() {
213+
for (k, v) in shard.iter() {
214+
f(k, &v.0, v.1);
215+
}
216+
}
217+
}
218+
#[cfg(not(parallel_compiler))]
219+
{
220+
let map = self.cache.lock();
221+
for (k, v) in map.iter() {
183222
f(k, &v.0, v.1);
184223
}
185224
}

0 commit comments

Comments
 (0)