Skip to content

Commit e428ff4

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). Perf notes: * #119977 (comment) evaluated a `IndexVec<CrateNum, IndexVec<DefIndex, Option<(V, DepNodeIndex)>>` scheme. This showed poor performance on incremental scenarios as the `iter()` callbacks are slower when walking the sparse vecs. In `full` scenarios this was a win for many primary benchmarks (~1-6% instructions, ~1-10% cycles), but did show significant memory overhead (+50% on many benchmarks). Next attempt will (a) skip hashing for local storage (expected to be denser) and retains the hashing for foreign storage (expected to be sparse) and (b) keep a present Vec to speed up `iter()` callbacks. cc #45275 r? `@ghost`
2 parents 1ead476 + 4d55b76 commit e428ff4

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

compiler/rustc_middle/src/query/keys.rs

+2-1
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

+72
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use crate::dep_graph::DepNodeIndex;
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_data_structures::sharded::{self, Sharded};
55
use rustc_data_structures::sync::OnceLock;
6+
use rustc_hir::def_id::LOCAL_CRATE;
67
use rustc_index::{Idx, IndexVec};
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,70 @@ 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+
local: rustc_data_structures::sync::Lock<(
187+
IndexVec<DefIndex, Option<(V, DepNodeIndex)>>,
188+
Vec<DefIndex>,
189+
)>,
190+
foreign: DefaultCache<DefId, V>,
191+
}
192+
193+
impl<V> Default for DefIdCache<V> {
194+
fn default() -> Self {
195+
DefIdCache { local: Default::default(), foreign: Default::default() }
196+
}
197+
}
198+
199+
impl<V> QueryCache for DefIdCache<V>
200+
where
201+
V: Copy,
202+
{
203+
type Key = DefId;
204+
type Value = V;
205+
206+
#[inline(always)]
207+
fn lookup(&self, key: &DefId) -> Option<(V, DepNodeIndex)> {
208+
if key.krate == LOCAL_CRATE {
209+
let cache = self.local.lock();
210+
cache.0.get(key.index).and_then(|v| *v)
211+
} else {
212+
self.foreign.lookup(key)
213+
}
214+
}
215+
216+
#[inline]
217+
fn complete(&self, key: DefId, value: V, index: DepNodeIndex) {
218+
if key.krate == LOCAL_CRATE {
219+
let mut cache = self.local.lock();
220+
let (cache, present) = &mut *cache;
221+
let slot = cache.ensure_contains_elem(key.index, Default::default);
222+
if slot.is_none() {
223+
// FIXME: Only store the present set when running in incremental mode. `iter` is not
224+
// used outside of saving caches to disk and self-profile.
225+
present.push(key.index);
226+
}
227+
*slot = Some((value, index));
228+
} else {
229+
self.foreign.complete(key, value, index)
230+
}
231+
}
232+
233+
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
234+
let guard = self.local.lock();
235+
for &idx in guard.1.iter() {
236+
// `present` only contains slots we filled via `complete`.
237+
let value = guard.0[idx].unwrap();
238+
f(&DefId { krate: LOCAL_CRATE, index: idx }, &value.0, value.1);
239+
}
240+
self.foreign.iter(f);
241+
}
242+
}

compiler/rustc_query_system/src/query/mod.rs

+2-1
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)