Skip to content

Stabilize query cache size by fixing encoding order of some query results #137196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4435,6 +4435,7 @@ dependencies = [
name = "rustc_query_system"
version = "0.0.0"
dependencies = [
"indexmap",
"parking_lot",
"rustc-rayon-core",
"rustc_abi",
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/query/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId, ModDefId};
use rustc_hir::hir_id::{HirId, OwnerId};
use rustc_query_system::dep_graph::DepNodeIndex;
use rustc_query_system::query::{DefIdCache, DefaultCache, SingleCache, VecCache};
use rustc_query_system::query::{DefIdCache, DefaultCache, IndexCache, SingleCache, VecCache};
use rustc_span::{DUMMY_SP, Ident, Span, Symbol};

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

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

fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.value.default_span(tcx)
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_query_system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
# tidy-alphabetical-start
indexmap = "2.4"
parking_lot = "0.12"
rustc-rayon-core = { version = "0.5.0" }
rustc_abi = { path = "../rustc_abi" }
Expand Down
47 changes: 46 additions & 1 deletion compiler/rustc_query_system/src/query/caches.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;
use std::hash::Hash;

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_data_structures::sharded::{self, Sharded};
use rustc_data_structures::sync::OnceLock;
pub use rustc_data_structures::vec_cache::VecCache;
Expand Down Expand Up @@ -187,3 +187,48 @@ where
self.iter(f)
}
}

pub struct IndexCache<K, V> {
cache: Sharded<FxIndexMap<K, (V, DepNodeIndex)>>,
}

impl<K, V> Default for IndexCache<K, V> {
fn default() -> Self {
IndexCache { cache: Default::default() }
}
}

impl<K, V> QueryCache for IndexCache<K, V>
where
K: Eq + Hash + Copy + Debug,
V: Copy,
{
type Key = K;
type Value = V;

#[inline(always)]
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {
use indexmap::map::raw_entry_v1::RawEntryApiV1;
let key_hash = sharded::make_hash(key);
let lock = self.cache.lock_shard_by_hash(key_hash);
let result = lock.raw_entry_v1().from_key_hashed_nocheck(key_hash, key);

if let Some((_, value)) = result { Some(*value) } else { None }
}

#[inline]
fn complete(&self, key: K, value: V, index: DepNodeIndex) {
let mut lock = self.cache.lock_shard_by_value(&key);
// We may be overwriting another value. This is all right, since the dep-graph
// will check that the fingerprint matches.
lock.insert(key, (value, index));
}

fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
for shard in self.cache.lock_shards() {
for (k, v) in shard.iter() {
f(k, &v.0, v.1);
}
}
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_query_system/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use self::job::{
};

mod caches;
pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache};
pub use self::caches::{DefIdCache, DefaultCache, IndexCache, QueryCache, SingleCache, VecCache};

mod config;
use rustc_data_structures::sync::Lock;
Expand Down
Loading