Skip to content

Commit 9deed6f

Browse files
Move Sharded maps into each QueryCache impl
1 parent 3b348d9 commit 9deed6f

File tree

8 files changed

+53
-122
lines changed

8 files changed

+53
-122
lines changed

compiler/rustc_data_structures/src/sharded.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<K: Eq + Hash + Copy + IntoPointer> ShardedHashMap<K, ()> {
129129
}
130130

131131
#[inline]
132-
fn make_hash<K: Hash + ?Sized>(val: &K) -> u64 {
132+
pub fn make_hash<K: Hash + ?Sized>(val: &K) -> u64 {
133133
let mut state = FxHasher::default();
134134
val.hash(&mut state);
135135
state.finish()

compiler/rustc_middle/src/ty/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ macro_rules! define_callbacks {
210210

211211
#[derive(Default)]
212212
pub struct QueryCaches<$tcx> {
213-
$($(#[$attr])* pub $name: QueryCacheStore<query_storage::$name<$tcx>>,)*
213+
$($(#[$attr])* pub $name: query_storage::$name<$tcx>,)*
214214
}
215215

216216
impl<$tcx> TyCtxtEnsure<$tcx> {

compiler/rustc_query_impl/src/on_disk_cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::thir;
1313
use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
1414
use rustc_middle::ty::{self, Ty, TyCtxt};
1515
use rustc_query_system::dep_graph::DepContext;
16-
use rustc_query_system::query::{QueryContext, QuerySideEffects};
16+
use rustc_query_system::query::{QueryCache, QueryContext, QuerySideEffects};
1717
use rustc_serialize::{
1818
opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize},
1919
Decodable, Decoder, Encodable, Encoder,
@@ -1034,7 +1034,7 @@ where
10341034
assert!(Q::query_state(tcx).all_inactive());
10351035
let cache = Q::query_cache(tcx);
10361036
let mut res = Ok(());
1037-
cache.iter_results(&mut |key, value, dep_node| {
1037+
cache.iter(&mut |key, value, dep_node| {
10381038
if res.is_err() {
10391039
return;
10401040
}

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ macro_rules! define_queries {
337337
}
338338

339339
#[inline(always)]
340-
fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryCacheStore<Self::Cache>
340+
fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a Self::Cache
341341
where 'tcx:'a
342342
{
343343
&tcx.query_caches.$name

compiler/rustc_query_impl/src/profiling_support.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::profiling::SelfProfiler;
44
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
55
use rustc_hir::definitions::DefPathData;
66
use rustc_middle::ty::{TyCtxt, WithOptConstParam};
7-
use rustc_query_system::query::{QueryCache, QueryCacheStore};
7+
use rustc_query_system::query::QueryCache;
88
use std::fmt::Debug;
99
use std::io::Write;
1010

@@ -229,7 +229,7 @@ where
229229
fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>(
230230
tcx: TyCtxt<'tcx>,
231231
query_name: &'static str,
232-
query_cache: &QueryCacheStore<C>,
232+
query_cache: &C,
233233
string_cache: &mut QueryKeyStringCache,
234234
) where
235235
C: QueryCache,
@@ -251,7 +251,7 @@ fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>(
251251
// locked while doing so. Instead we copy out the
252252
// `(query_key, dep_node_index)` pairs and release the lock again.
253253
let mut query_keys_and_indices = Vec::new();
254-
query_cache.iter_results(&mut |k, _, i| query_keys_and_indices.push((k.clone(), i)));
254+
query_cache.iter(&mut |k, _, i| query_keys_and_indices.push((k.clone(), i)));
255255

256256
// Now actually allocate the strings. If allocating the strings
257257
// generates new entries in the query cache, we'll miss them but
@@ -276,7 +276,7 @@ fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>(
276276
let event_id = event_id_builder.from_label(query_name).to_string_id();
277277

278278
let mut query_invocation_ids = Vec::new();
279-
query_cache.iter_results(&mut |_, _, i| {
279+
query_cache.iter(&mut |_, _, i| {
280280
query_invocation_ids.push(i.into());
281281
});
282282

compiler/rustc_query_system/src/query/caches.rs

Lines changed: 32 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::dep_graph::DepNodeIndex;
2-
use crate::query::plumbing::{QueryCacheStore, QueryLookup};
2+
use crate::query::plumbing::QueryLookup;
33

44
use rustc_arena::TypedArena;
55
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_data_structures::sharded::Sharded;
6+
use rustc_data_structures::sharded::{self, Sharded};
77
use rustc_data_structures::sync::WorkerLocal;
88
use std::default::Default;
99
use std::fmt::Debug;
@@ -25,35 +25,23 @@ pub trait QueryStorage {
2525

2626
pub trait QueryCache: QueryStorage + Sized {
2727
type Key: Hash + Eq + Clone + Debug;
28-
type Sharded: Default;
2928

3029
/// Checks if the query is already computed and in the cache.
3130
/// It returns the shard index and a lock guard to the shard,
3231
/// which will be used if the query is not in the cache and we need
3332
/// to compute it.
34-
fn lookup<'s, R, OnHit>(
33+
fn lookup<R, OnHit>(
3534
&self,
36-
state: &'s QueryCacheStore<Self>,
3735
key: &Self::Key,
3836
// `on_hit` can be called while holding a lock to the query state shard.
3937
on_hit: OnHit,
4038
) -> Result<R, QueryLookup>
4139
where
4240
OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R;
4341

44-
fn complete(
45-
&self,
46-
lock_sharded_storage: &mut Self::Sharded,
47-
key: Self::Key,
48-
value: Self::Value,
49-
index: DepNodeIndex,
50-
) -> Self::Stored;
42+
fn complete(&self, key: Self::Key, value: Self::Value, index: DepNodeIndex) -> Self::Stored;
5143

52-
fn iter(
53-
&self,
54-
shards: &Sharded<Self::Sharded>,
55-
f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex),
56-
);
44+
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex));
5745
}
5846

5947
pub struct DefaultCacheSelector;
@@ -62,11 +50,13 @@ impl<K: Eq + Hash, V: Clone> CacheSelector<K, V> for DefaultCacheSelector {
6250
type Cache = DefaultCache<K, V>;
6351
}
6452

65-
pub struct DefaultCache<K, V>(PhantomData<(K, V)>);
53+
pub struct DefaultCache<K, V> {
54+
shards: Sharded<FxHashMap<K, (V, DepNodeIndex)>>,
55+
}
6656

6757
impl<K, V> Default for DefaultCache<K, V> {
6858
fn default() -> Self {
69-
DefaultCache(PhantomData)
59+
DefaultCache { shards: Default::default() }
7060
}
7161
}
7262

@@ -87,19 +77,16 @@ where
8777
V: Clone + Debug,
8878
{
8979
type Key = K;
90-
type Sharded = FxHashMap<K, (V, DepNodeIndex)>;
9180

9281
#[inline(always)]
93-
fn lookup<'s, R, OnHit>(
94-
&self,
95-
state: &'s QueryCacheStore<Self>,
96-
key: &K,
97-
on_hit: OnHit,
98-
) -> Result<R, QueryLookup>
82+
fn lookup<R, OnHit>(&self, key: &K, on_hit: OnHit) -> Result<R, QueryLookup>
9983
where
10084
OnHit: FnOnce(&V, DepNodeIndex) -> R,
10185
{
102-
let (lookup, lock) = state.get_lookup(key);
86+
let key_hash = sharded::make_hash(key);
87+
let shard = sharded::get_shard_index_by_hash(key_hash);
88+
let lock = self.shards.get_shard_by_index(shard).lock();
89+
let lookup = QueryLookup { key_hash, shard };
10390
let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
10491

10592
if let Some((_, value)) = result {
@@ -111,23 +98,13 @@ where
11198
}
11299

113100
#[inline]
114-
fn complete(
115-
&self,
116-
lock_sharded_storage: &mut Self::Sharded,
117-
key: K,
118-
value: V,
119-
index: DepNodeIndex,
120-
) -> Self::Stored {
121-
lock_sharded_storage.insert(key, (value.clone(), index));
101+
fn complete(&self, key: K, value: V, index: DepNodeIndex) -> Self::Stored {
102+
self.shards.get_shard_by_value(&key).lock().insert(key, (value.clone(), index));
122103
value
123104
}
124105

125-
fn iter(
126-
&self,
127-
shards: &Sharded<Self::Sharded>,
128-
f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex),
129-
) {
130-
let shards = shards.lock_shards();
106+
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
107+
let shards = self.shards.lock_shards();
131108
for shard in shards.iter() {
132109
for (k, v) in shard.iter() {
133110
f(k, &v.0, v.1);
@@ -144,12 +121,15 @@ impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<K, V> for ArenaCacheSelector<'tc
144121

145122
pub struct ArenaCache<'tcx, K, V> {
146123
arena: WorkerLocal<TypedArena<(V, DepNodeIndex)>>,
147-
phantom: PhantomData<(K, &'tcx V)>,
124+
shards: Sharded<FxHashMap<K, &'tcx (V, DepNodeIndex)>>,
148125
}
149126

150127
impl<'tcx, K, V> Default for ArenaCache<'tcx, K, V> {
151128
fn default() -> Self {
152-
ArenaCache { arena: WorkerLocal::new(|_| TypedArena::default()), phantom: PhantomData }
129+
ArenaCache {
130+
arena: WorkerLocal::new(|_| TypedArena::default()),
131+
shards: Default::default(),
132+
}
153133
}
154134
}
155135

@@ -171,19 +151,16 @@ where
171151
V: Debug,
172152
{
173153
type Key = K;
174-
type Sharded = FxHashMap<K, &'tcx (V, DepNodeIndex)>;
175154

176155
#[inline(always)]
177-
fn lookup<'s, R, OnHit>(
178-
&self,
179-
state: &'s QueryCacheStore<Self>,
180-
key: &K,
181-
on_hit: OnHit,
182-
) -> Result<R, QueryLookup>
156+
fn lookup<R, OnHit>(&self, key: &K, on_hit: OnHit) -> Result<R, QueryLookup>
183157
where
184158
OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
185159
{
186-
let (lookup, lock) = state.get_lookup(key);
160+
let key_hash = sharded::make_hash(key);
161+
let shard = sharded::get_shard_index_by_hash(key_hash);
162+
let lock = self.shards.get_shard_by_index(shard).lock();
163+
let lookup = QueryLookup { key_hash, shard };
187164
let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
188165

189166
if let Some((_, value)) = result {
@@ -195,25 +172,15 @@ where
195172
}
196173

197174
#[inline]
198-
fn complete(
199-
&self,
200-
lock_sharded_storage: &mut Self::Sharded,
201-
key: K,
202-
value: V,
203-
index: DepNodeIndex,
204-
) -> Self::Stored {
175+
fn complete(&self, key: K, value: V, index: DepNodeIndex) -> Self::Stored {
205176
let value = self.arena.alloc((value, index));
206177
let value = unsafe { &*(value as *const _) };
207-
lock_sharded_storage.insert(key, value);
178+
self.shards.get_shard_by_value(&key).lock().insert(key, value);
208179
&value.0
209180
}
210181

211-
fn iter(
212-
&self,
213-
shards: &Sharded<Self::Sharded>,
214-
f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex),
215-
) {
216-
let shards = shards.lock_shards();
182+
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
183+
let shards = self.shards.lock_shards();
217184
for shard in shards.iter() {
218185
for (k, v) in shard.iter() {
219186
f(k, &v.0, v.1);

compiler/rustc_query_system/src/query/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::dep_graph::DepNode;
44
use crate::dep_graph::SerializedDepNodeIndex;
55
use crate::ich::StableHashingContext;
66
use crate::query::caches::QueryCache;
7-
use crate::query::{QueryCacheStore, QueryContext, QueryState};
7+
use crate::query::{QueryContext, QueryState};
88

99
use rustc_data_structures::fingerprint::Fingerprint;
1010
use rustc_errors::DiagnosticBuilder;
@@ -64,7 +64,7 @@ pub trait QueryDescription<CTX: QueryContext>: QueryConfig {
6464
CTX: 'a;
6565

6666
// Don't use this method to access query results, instead use the methods on TyCtxt
67-
fn query_cache<'a>(tcx: CTX) -> &'a QueryCacheStore<Self::Cache>
67+
fn query_cache<'a>(tcx: CTX) -> &'a Self::Cache
6868
where
6969
CTX: 'a;
7070

0 commit comments

Comments
 (0)