Notes on types/traits used for in-memory query caching

When the word "cache" appears in the context of the query system, it often
isn't obvious whether that is referring to the in-memory query cache or the
on-disk incremental cache.

For these types, we can assure the reader that they are for in-memory caching.
This commit is contained in:
Zalathar 2025-02-03 21:31:06 +11:00
parent 4a43094662
commit 623d6e8ca4
4 changed files with 42 additions and 5 deletions

View file

@ -11,18 +11,30 @@ use rustc_span::def_id::{DefId, DefIndex};
use crate::dep_graph::DepNodeIndex;
/// Trait for types that serve as an in-memory cache for query results,
/// for a given key (argument) type and value (return) type.
///
/// Types implementing this trait are associated with actual key/value types
/// by the `Cache` associated type of the `rustc_middle::query::Key` trait.
pub trait QueryCache: Sized {
type Key: Hash + Eq + Copy + Debug;
type Value: Copy;
/// Checks if the query is already computed and in the cache.
/// Returns the cached value (and other information) associated with the
/// given key, if it is present in the cache.
fn lookup(&self, key: &Self::Key) -> Option<(Self::Value, DepNodeIndex)>;
/// Adds a key/value entry to this cache.
///
/// Called by some part of the query system, after having obtained the
/// value by executing the query or loading a cached value from disk.
fn complete(&self, key: Self::Key, value: Self::Value, index: DepNodeIndex);
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex));
}
/// In-memory cache for queries whose keys aren't suitable for any of the
/// more specialized kinds of cache. Backed by a sharded hashmap.
pub struct DefaultCache<K, V> {
cache: Sharded<FxHashMap<K, (V, DepNodeIndex)>>,
}
@ -67,6 +79,8 @@ where
}
}
/// In-memory cache for queries whose key type only has one value (e.g. `()`).
/// The cache therefore only needs to store one query return value.
pub struct SingleCache<V> {
cache: OnceLock<(V, DepNodeIndex)>,
}
@ -101,6 +115,10 @@ where
}
}
/// In-memory cache for queries whose key is a [`DefId`].
///
/// Selects between one of two internal caches, depending on whether the key
/// is a local ID or foreign-crate ID.
pub struct DefIdCache<V> {
/// Stores the local DefIds in a dense map. Local queries are much more often dense, so this is
/// a win over hashing query keys at marginal memory cost (~5% at most) compared to FxHashMap.

View file

@ -222,10 +222,10 @@ pub struct CycleError {
pub cycle: Vec<QueryInfo>,
}
/// Checks if the query is already computed and in the cache.
/// It returns the shard index and a lock guard to the shard,
/// which will be used if the query is not in the cache and we need
/// to compute it.
/// Checks whether there is already a value for this key in the in-memory
/// query cache, returning that value if present.
///
/// (Also performs some associated bookkeeping, if a value was found.)
#[inline(always)]
pub fn try_get_cached<Tcx, C>(tcx: Tcx, cache: &C, key: &C::Key) -> Option<C::Value>
where