Skip to content

Commit 623d6e8

Browse files
committed
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.
1 parent 4a43094 commit 623d6e8

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

Diff for: compiler/rustc_data_structures/src/vec_cache.rs

+13
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,19 @@ impl SlotIndex {
206206
}
207207
}
208208

209+
/// In-memory cache for queries whose keys are densely-numbered IDs
210+
/// (e.g `CrateNum`, `LocalDefId`), and can therefore be used as indices
211+
/// into a dense vector of cached values.
212+
///
213+
/// (As of [#124780] the underlying storage is not an actual `Vec`, but rather
214+
/// a series of increasingly-large buckets, for improved performance when the
215+
/// parallel frontend is using multiple threads.)
216+
///
217+
/// Each entry in the cache stores the query's return value (`V`), and also
218+
/// an associated index (`I`), which in practice is a `DepNodeIndex` used for
219+
/// query dependency tracking.
220+
///
221+
/// [#124780]: https://github.com/rust-lang/rust/pull/124780
209222
pub struct VecCache<K: Idx, V, I> {
210223
// Entries per bucket:
211224
// Bucket 0: 4096 2^12

Diff for: compiler/rustc_middle/src/query/keys.rs

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ pub struct LocalCrate;
2020
/// The `Key` trait controls what types can legally be used as the key
2121
/// for a query.
2222
pub trait Key: Sized {
23+
/// The type of in-memory cache to use for queries with this key type.
24+
///
25+
/// In practice the cache type must implement [`QueryCache`], though that
26+
/// constraint is not enforced here.
27+
///
28+
/// [`QueryCache`]: rustc_query_system::query::QueryCache
2329
// N.B. Most of the keys down below have `type Cache<V> = DefaultCache<Self, V>;`,
2430
// it would be reasonable to use associated type defaults, to remove the duplication...
2531
//

Diff for: compiler/rustc_query_system/src/query/caches.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,30 @@ use rustc_span::def_id::{DefId, DefIndex};
1111

1212
use crate::dep_graph::DepNodeIndex;
1313

14+
/// Trait for types that serve as an in-memory cache for query results,
15+
/// for a given key (argument) type and value (return) type.
16+
///
17+
/// Types implementing this trait are associated with actual key/value types
18+
/// by the `Cache` associated type of the `rustc_middle::query::Key` trait.
1419
pub trait QueryCache: Sized {
1520
type Key: Hash + Eq + Copy + Debug;
1621
type Value: Copy;
1722

18-
/// Checks if the query is already computed and in the cache.
23+
/// Returns the cached value (and other information) associated with the
24+
/// given key, if it is present in the cache.
1925
fn lookup(&self, key: &Self::Key) -> Option<(Self::Value, DepNodeIndex)>;
2026

27+
/// Adds a key/value entry to this cache.
28+
///
29+
/// Called by some part of the query system, after having obtained the
30+
/// value by executing the query or loading a cached value from disk.
2131
fn complete(&self, key: Self::Key, value: Self::Value, index: DepNodeIndex);
2232

2333
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex));
2434
}
2535

36+
/// In-memory cache for queries whose keys aren't suitable for any of the
37+
/// more specialized kinds of cache. Backed by a sharded hashmap.
2638
pub struct DefaultCache<K, V> {
2739
cache: Sharded<FxHashMap<K, (V, DepNodeIndex)>>,
2840
}
@@ -67,6 +79,8 @@ where
6779
}
6880
}
6981

82+
/// In-memory cache for queries whose key type only has one value (e.g. `()`).
83+
/// The cache therefore only needs to store one query return value.
7084
pub struct SingleCache<V> {
7185
cache: OnceLock<(V, DepNodeIndex)>,
7286
}
@@ -101,6 +115,10 @@ where
101115
}
102116
}
103117

118+
/// In-memory cache for queries whose key is a [`DefId`].
119+
///
120+
/// Selects between one of two internal caches, depending on whether the key
121+
/// is a local ID or foreign-crate ID.
104122
pub struct DefIdCache<V> {
105123
/// Stores the local DefIds in a dense map. Local queries are much more often dense, so this is
106124
/// a win over hashing query keys at marginal memory cost (~5% at most) compared to FxHashMap.

Diff for: compiler/rustc_query_system/src/query/plumbing.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ pub struct CycleError {
222222
pub cycle: Vec<QueryInfo>,
223223
}
224224

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

0 commit comments

Comments
 (0)