Skip to content

Commit 12bebac

Browse files
committed
Create a query cache for DefId
1 parent eda41ad commit 12bebac

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

compiler/rustc_middle/src/query/keys.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use crate::ty::subst::{GenericArg, SubstsRef};
99
use crate::ty::{self, Ty, TyCtxt};
1010
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
1111
use rustc_hir::hir_id::{HirId, OwnerId};
12-
use rustc_query_system::query::{DefaultCacheSelector, SingleCacheSelector, VecCacheSelector};
12+
use rustc_query_system::query::{
13+
DefIdCacheSelector, DefaultCacheSelector, SingleCacheSelector, VecCacheSelector,
14+
};
1315
use rustc_span::symbol::{Ident, Symbol};
1416
use rustc_span::{Span, DUMMY_SP};
1517
use rustc_target::abi::FieldIdx;
@@ -153,7 +155,7 @@ impl Key for LocalDefId {
153155
}
154156

155157
impl Key for DefId {
156-
type CacheSelector = DefaultCacheSelector<Self>;
158+
type CacheSelector = DefIdCacheSelector;
157159

158160
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
159161
tcx.def_span(*self)

compiler/rustc_query_system/src/query/caches.rs

+60-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use crate::dep_graph::DepNodeIndex;
22

33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_data_structures::sharded;
5-
#[cfg(parallel_compiler)]
65
use rustc_data_structures::sharded::Sharded;
76
use rustc_data_structures::sync::Lock;
87
use rustc_index::{Idx, IndexVec};
8+
use rustc_span::def_id::CrateNum;
9+
use rustc_span::def_id::DefId;
10+
use rustc_span::def_id::DefIndex;
911
use std::fmt::Debug;
1012
use std::hash::Hash;
1113
use std::marker::PhantomData;
@@ -212,3 +214,60 @@ where
212214
}
213215
}
214216
}
217+
218+
pub struct DefIdCacheSelector;
219+
220+
impl<'tcx, V: 'tcx> CacheSelector<'tcx, V> for DefIdCacheSelector {
221+
type Cache = DefIdCache< V>
222+
where
223+
V: Copy;
224+
}
225+
226+
pub struct DefIdCache<V> {
227+
cache: Sharded<IndexVec<CrateNum, FxHashMap<DefIndex, (V, DepNodeIndex)>>>,
228+
}
229+
230+
impl<V> Default for DefIdCache<V> {
231+
fn default() -> Self {
232+
Self { cache: Default::default() }
233+
}
234+
}
235+
236+
impl<V> QueryCache for DefIdCache<V>
237+
where
238+
V: Copy,
239+
{
240+
type Key = DefId;
241+
type Value = V;
242+
243+
#[inline(always)]
244+
fn lookup(&self, key: &DefId) -> Option<(V, DepNodeIndex)> {
245+
let key_hash = sharded::make_hash(&key.index);
246+
let lock = self.cache.get_shard_by_hash(key_hash).lock();
247+
if let Some(defs) = lock.get(key.krate) {
248+
let result = defs.raw_entry().from_key_hashed_nocheck(key_hash, &key.index);
249+
if let Some((_, value)) = result { Some(*value) } else { None }
250+
} else {
251+
None
252+
}
253+
}
254+
255+
#[inline]
256+
fn complete(&self, key: DefId, value: V, index: DepNodeIndex) {
257+
let key_hash = sharded::make_hash(&key.index);
258+
let mut lock = self.cache.get_shard_by_hash(key_hash).lock();
259+
lock.ensure_contains_elem(key.krate, || FxHashMap::default());
260+
lock[key.krate].insert(key.index, (value, index));
261+
}
262+
263+
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
264+
let shards = self.cache.lock_shards();
265+
for shard in shards.iter() {
266+
for (k, v) in shard.iter_enumerated() {
267+
for (&index, v) in v.iter() {
268+
f(&DefId { krate: k, index }, &v.0, v.1);
269+
}
270+
}
271+
}
272+
}
273+
}

compiler/rustc_query_system/src/query/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ pub use self::job::{print_query_stack, QueryInfo, QueryJob, QueryJobId, QueryJob
88

99
mod caches;
1010
pub use self::caches::{
11-
CacheSelector, DefaultCacheSelector, QueryCache, SingleCacheSelector, VecCacheSelector,
11+
CacheSelector, DefIdCacheSelector, DefaultCacheSelector, QueryCache, SingleCacheSelector,
12+
VecCacheSelector,
1213
};
1314

1415
mod config;

0 commit comments

Comments
 (0)