Skip to content

Commit 6235575

Browse files
committed
Auto merge of #119977 - Mark-Simulacrum:defid-cache, r=<try>
Cache DefId-keyed queries without hashing Not yet ready for review: * My guess is that this will be a significant memory footprint hit for sparser queries and require some more logic. * Likely merits some further consideration for parallel rustc, though as noted in a separate comment the existing IndexVec sharding looks useless to me (likely always selecting the same shard today in 99% of cases). cc #45275 r? `@ghost`
2 parents 30dfb9e + b1e2dc2 commit 6235575

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

compiler/rustc_middle/src/query/keys.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::ty::{self, Ty, TyCtxt};
99
use crate::ty::{GenericArg, GenericArgsRef};
1010
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalModDefId, ModDefId, LOCAL_CRATE};
1111
use rustc_hir::hir_id::{HirId, OwnerId};
12+
use rustc_query_system::query::DefIdCacheSelector;
1213
use rustc_query_system::query::{DefaultCacheSelector, SingleCacheSelector, VecCacheSelector};
1314
use rustc_span::symbol::{Ident, Symbol};
1415
use rustc_span::{Span, DUMMY_SP};
@@ -152,7 +153,7 @@ impl Key for LocalDefId {
152153
}
153154

154155
impl Key for DefId {
155-
type CacheSelector = DefaultCacheSelector<Self>;
156+
type CacheSelector = DefIdCacheSelector;
156157

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

compiler/rustc_query_system/src/query/caches.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use rustc_data_structures::fx::FxHashMap;
44
use rustc_data_structures::sharded::{self, Sharded};
55
use rustc_data_structures::sync::OnceLock;
66
use rustc_index::{Idx, IndexVec};
7+
use rustc_span::def_id::CrateNum;
8+
use rustc_span::def_id::DefId;
9+
use rustc_span::def_id::DefIndex;
710
use std::fmt::Debug;
811
use std::hash::Hash;
912
use std::marker::PhantomData;
@@ -148,6 +151,8 @@ where
148151

149152
#[inline(always)]
150153
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {
154+
// BUG: the "hash" passed here is a terrible hash and sharding is basically useless, I
155+
// think?
151156
let lock = self.cache.lock_shard_by_hash(key.index() as u64);
152157
if let Some(Some(value)) = lock.get(*key) { Some(*value) } else { None }
153158
}
@@ -168,3 +173,56 @@ where
168173
}
169174
}
170175
}
176+
177+
pub struct DefIdCacheSelector;
178+
179+
impl<'tcx, V: 'tcx> CacheSelector<'tcx, V> for DefIdCacheSelector {
180+
type Cache = DefIdCache<V>
181+
where
182+
V: Copy;
183+
}
184+
185+
pub struct DefIdCache<V> {
186+
cache: rustc_data_structures::sync::Lock<
187+
IndexVec<CrateNum, IndexVec<DefIndex, Option<(V, DepNodeIndex)>>>,
188+
>,
189+
}
190+
191+
impl<V> Default for DefIdCache<V> {
192+
fn default() -> Self {
193+
DefIdCache { cache: Default::default() }
194+
}
195+
}
196+
197+
impl<V> QueryCache for DefIdCache<V>
198+
where
199+
V: Copy,
200+
{
201+
type Key = DefId;
202+
type Value = V;
203+
204+
#[inline(always)]
205+
fn lookup(&self, key: &DefId) -> Option<(V, DepNodeIndex)> {
206+
let cache = self.cache.lock();
207+
let krate_cache = cache.get(key.krate)?;
208+
krate_cache.get(key.index).and_then(|v| *v)
209+
}
210+
211+
#[inline]
212+
fn complete(&self, key: DefId, value: V, index: DepNodeIndex) {
213+
let mut cache = self.cache.lock();
214+
let krate_cache = cache.ensure_contains_elem(key.krate, Default::default);
215+
*krate_cache.ensure_contains_elem(key.index, Default::default) = Some((value, index));
216+
}
217+
218+
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
219+
let guard = self.cache.lock();
220+
for (krate, v) in guard.iter_enumerated() {
221+
for (idx, v) in v.iter_enumerated() {
222+
if let Some(v) = v {
223+
f(&DefId { krate, index: idx }, &v.0, v.1);
224+
}
225+
}
226+
}
227+
}
228+
}

compiler/rustc_query_system/src/query/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ pub use self::job::{
1010

1111
mod caches;
1212
pub use self::caches::{
13-
CacheSelector, DefaultCacheSelector, QueryCache, SingleCacheSelector, VecCacheSelector,
13+
CacheSelector, DefIdCacheSelector, DefaultCacheSelector, QueryCache, SingleCacheSelector,
14+
VecCacheSelector,
1415
};
1516

1617
mod config;

0 commit comments

Comments
 (0)