Skip to content

Commit e1684fb

Browse files
committed
Stabilize query cache size by fixing encoding order of some query results
1 parent 2162e9d commit e1684fb

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4435,6 +4435,7 @@ dependencies = [
44354435
name = "rustc_query_system"
44364436
version = "0.0.0"
44374437
dependencies = [
4438+
"indexmap",
44384439
"parking_lot",
44394440
"rustc-rayon-core",
44404441
"rustc_abi",

compiler/rustc_middle/src/query/keys.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId, ModDefId};
44
use rustc_hir::hir_id::{HirId, OwnerId};
55
use rustc_query_system::dep_graph::DepNodeIndex;
6-
use rustc_query_system::query::{DefIdCache, DefaultCache, SingleCache, VecCache};
6+
use rustc_query_system::query::{DefIdCache, DefaultCache, IndexCache, SingleCache, VecCache};
77
use rustc_span::{DUMMY_SP, Ident, Span, Symbol};
88

99
use crate::infer::canonical::CanonicalQueryInput;
@@ -471,7 +471,7 @@ impl<'tcx> Key for ty::ParamEnv<'tcx> {
471471
}
472472

473473
impl<'tcx, T: Key> Key for ty::PseudoCanonicalInput<'tcx, T> {
474-
type Cache<V> = DefaultCache<Self, V>;
474+
type Cache<V> = IndexCache<Self, V>;
475475

476476
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
477477
self.value.default_span(tcx)

compiler/rustc_query_system/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
# tidy-alphabetical-start
8+
indexmap = "2.4"
89
parking_lot = "0.12"
910
rustc-rayon-core = { version = "0.5.0" }
1011
rustc_abi = { path = "../rustc_abi" }

compiler/rustc_query_system/src/query/caches.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::Debug;
22
use std::hash::Hash;
33

4-
use rustc_data_structures::fx::FxHashMap;
4+
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
55
use rustc_data_structures::sharded::{self, Sharded};
66
use rustc_data_structures::sync::OnceLock;
77
pub use rustc_data_structures::vec_cache::VecCache;
@@ -187,3 +187,48 @@ where
187187
self.iter(f)
188188
}
189189
}
190+
191+
pub struct IndexCache<K, V> {
192+
cache: Sharded<FxIndexMap<K, (V, DepNodeIndex)>>,
193+
}
194+
195+
impl<K, V> Default for IndexCache<K, V> {
196+
fn default() -> Self {
197+
IndexCache { cache: Default::default() }
198+
}
199+
}
200+
201+
impl<K, V> QueryCache for IndexCache<K, V>
202+
where
203+
K: Eq + Hash + Copy + Debug,
204+
V: Copy,
205+
{
206+
type Key = K;
207+
type Value = V;
208+
209+
#[inline(always)]
210+
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {
211+
use indexmap::map::raw_entry_v1::RawEntryApiV1;
212+
let key_hash = sharded::make_hash(key);
213+
let lock = self.cache.lock_shard_by_hash(key_hash);
214+
let result = lock.raw_entry_v1().from_key_hashed_nocheck(key_hash, key);
215+
216+
if let Some((_, value)) = result { Some(*value) } else { None }
217+
}
218+
219+
#[inline]
220+
fn complete(&self, key: K, value: V, index: DepNodeIndex) {
221+
let mut lock = self.cache.lock_shard_by_value(&key);
222+
// We may be overwriting another value. This is all right, since the dep-graph
223+
// will check that the fingerprint matches.
224+
lock.insert(key, (value, index));
225+
}
226+
227+
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
228+
for shard in self.cache.lock_shards() {
229+
for (k, v) in shard.iter() {
230+
f(k, &v.0, v.1);
231+
}
232+
}
233+
}
234+
}

compiler/rustc_query_system/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use self::job::{
88
};
99

1010
mod caches;
11-
pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache};
11+
pub use self::caches::{DefIdCache, DefaultCache, IndexCache, QueryCache, SingleCache, VecCache};
1212

1313
mod config;
1414
use rustc_data_structures::sync::Lock;

0 commit comments

Comments
 (0)