2020-02-08 06:38:00 +00:00
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2023-08-16 12:16:05 +00:00
|
|
|
use rustc_data_structures::sharded::{self, Sharded};
|
2024-05-05 23:57:24 +00:00
|
|
|
use rustc_data_structures::sync::OnceLock;
|
|
|
|
pub use rustc_data_structures::vec_cache::VecCache;
|
2024-01-14 23:08:02 +00:00
|
|
|
use rustc_hir::def_id::LOCAL_CRATE;
|
2024-05-05 23:57:24 +00:00
|
|
|
use rustc_index::Idx;
|
2024-01-14 23:08:02 +00:00
|
|
|
use rustc_span::def_id::{DefId, DefIndex};
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2020-10-12 14:29:41 +00:00
|
|
|
use crate::dep_graph::DepNodeIndex;
|
2020-02-08 06:38:00 +00:00
|
|
|
|
2025-02-03 10:31:06 +00:00
|
|
|
/// 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.
|
2023-02-07 07:32:30 +00:00
|
|
|
pub trait QueryCache: Sized {
|
2023-02-17 16:33:05 +00:00
|
|
|
type Key: Hash + Eq + Copy + Debug;
|
2023-03-26 09:00:26 +00:00
|
|
|
type Value: Copy;
|
2020-02-08 06:38:00 +00:00
|
|
|
|
2025-02-03 10:31:06 +00:00
|
|
|
/// Returns the cached value (and other information) associated with the
|
|
|
|
/// given key, if it is present in the cache.
|
2023-02-08 18:53:48 +00:00
|
|
|
fn lookup(&self, key: &Self::Key) -> Option<(Self::Value, DepNodeIndex)>;
|
2020-02-08 06:38:00 +00:00
|
|
|
|
2025-02-03 10:31:06 +00:00
|
|
|
/// 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.
|
2023-02-08 18:53:48 +00:00
|
|
|
fn complete(&self, key: Self::Key, value: Self::Value, index: DepNodeIndex);
|
2020-02-08 06:38:00 +00:00
|
|
|
|
2022-02-20 03:44:19 +00:00
|
|
|
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex));
|
2020-02-08 06:38:00 +00:00
|
|
|
}
|
|
|
|
|
2025-02-03 10:31:06 +00:00
|
|
|
/// In-memory cache for queries whose keys aren't suitable for any of the
|
|
|
|
/// more specialized kinds of cache. Backed by a sharded hashmap.
|
2022-02-20 03:44:19 +00:00
|
|
|
pub struct DefaultCache<K, V> {
|
2022-02-20 17:57:52 +00:00
|
|
|
cache: Sharded<FxHashMap<K, (V, DepNodeIndex)>>,
|
2022-02-20 03:44:19 +00:00
|
|
|
}
|
2020-03-07 17:36:24 +00:00
|
|
|
|
|
|
|
impl<K, V> Default for DefaultCache<K, V> {
|
|
|
|
fn default() -> Self {
|
2022-02-20 17:57:52 +00:00
|
|
|
DefaultCache { cache: Default::default() }
|
2020-03-07 17:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-08 06:38:00 +00:00
|
|
|
|
2020-10-12 14:29:41 +00:00
|
|
|
impl<K, V> QueryCache for DefaultCache<K, V>
|
|
|
|
where
|
2023-02-17 16:33:05 +00:00
|
|
|
K: Eq + Hash + Copy + Debug,
|
2023-03-26 09:00:26 +00:00
|
|
|
V: Copy,
|
2020-10-12 14:29:41 +00:00
|
|
|
{
|
2020-03-07 17:36:24 +00:00
|
|
|
type Key = K;
|
2023-02-07 07:32:30 +00:00
|
|
|
type Value = V;
|
2020-02-08 06:38:00 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2023-02-04 15:16:59 +00:00
|
|
|
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {
|
2022-02-20 03:44:19 +00:00
|
|
|
let key_hash = sharded::make_hash(key);
|
2023-08-16 11:50:31 +00:00
|
|
|
let lock = self.cache.lock_shard_by_hash(key_hash);
|
2022-02-20 03:56:56 +00:00
|
|
|
let result = lock.raw_entry().from_key_hashed_nocheck(key_hash, key);
|
2020-02-08 06:38:00 +00:00
|
|
|
|
2023-02-04 15:16:59 +00:00
|
|
|
if let Some((_, value)) = result { Some(*value) } else { None }
|
2020-02-08 06:38:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-02-08 18:53:48 +00:00
|
|
|
fn complete(&self, key: K, value: V, index: DepNodeIndex) {
|
2023-08-16 11:50:31 +00:00
|
|
|
let mut lock = self.cache.lock_shard_by_value(&key);
|
2022-11-16 20:34:16 +00:00
|
|
|
// We may be overwriting another value. This is all right, since the dep-graph
|
2022-05-08 12:42:12 +00:00
|
|
|
// will check that the fingerprint matches.
|
2023-02-15 22:18:40 +00:00
|
|
|
lock.insert(key, (value, index));
|
2020-02-08 06:38:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-20 03:44:19 +00:00
|
|
|
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
|
2023-08-17 09:07:50 +00:00
|
|
|
for shard in self.cache.lock_shards() {
|
2023-08-16 12:16:05 +00:00
|
|
|
for (k, v) in shard.iter() {
|
2021-04-29 14:23:17 +00:00
|
|
|
f(k, &v.0, v.1);
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 06:38:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 17:46:25 +00:00
|
|
|
|
2025-02-03 10:31:06 +00:00
|
|
|
/// 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.
|
2023-02-03 17:39:31 +00:00
|
|
|
pub struct SingleCache<V> {
|
2023-08-31 23:14:33 +00:00
|
|
|
cache: OnceLock<(V, DepNodeIndex)>,
|
2023-02-03 17:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<V> Default for SingleCache<V> {
|
|
|
|
fn default() -> Self {
|
2023-08-31 23:14:33 +00:00
|
|
|
SingleCache { cache: OnceLock::new() }
|
2023-02-03 17:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V> QueryCache for SingleCache<V>
|
|
|
|
where
|
2023-03-26 09:00:26 +00:00
|
|
|
V: Copy,
|
2023-02-03 17:39:31 +00:00
|
|
|
{
|
|
|
|
type Key = ();
|
2023-02-07 07:32:30 +00:00
|
|
|
type Value = V;
|
2023-02-03 17:39:31 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn lookup(&self, _key: &()) -> Option<(V, DepNodeIndex)> {
|
2023-08-31 23:14:33 +00:00
|
|
|
self.cache.get().copied()
|
2023-02-03 17:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-02-08 18:53:48 +00:00
|
|
|
fn complete(&self, _key: (), value: V, index: DepNodeIndex) {
|
2023-08-31 23:14:33 +00:00
|
|
|
self.cache.set((value, index)).ok();
|
2023-02-03 17:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
|
2023-08-31 23:14:33 +00:00
|
|
|
if let Some(value) = self.cache.get() {
|
2023-04-09 21:07:18 +00:00
|
|
|
f(&(), &value.0, value.1)
|
|
|
|
}
|
2023-02-03 17:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-03 10:31:06 +00:00
|
|
|
/// 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.
|
2024-01-14 23:08:02 +00:00
|
|
|
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.
|
2024-05-05 23:57:24 +00:00
|
|
|
local: VecCache<DefIndex, V, DepNodeIndex>,
|
2024-01-14 23:08:02 +00:00
|
|
|
foreign: DefaultCache<DefId, V>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V> Default for DefIdCache<V> {
|
|
|
|
fn default() -> Self {
|
|
|
|
DefIdCache { local: Default::default(), foreign: Default::default() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V> QueryCache for DefIdCache<V>
|
|
|
|
where
|
|
|
|
V: Copy,
|
|
|
|
{
|
|
|
|
type Key = DefId;
|
|
|
|
type Value = V;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn lookup(&self, key: &DefId) -> Option<(V, DepNodeIndex)> {
|
|
|
|
if key.krate == LOCAL_CRATE {
|
2024-05-05 23:57:24 +00:00
|
|
|
self.local.lookup(&key.index)
|
2024-01-14 23:08:02 +00:00
|
|
|
} else {
|
|
|
|
self.foreign.lookup(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn complete(&self, key: DefId, value: V, index: DepNodeIndex) {
|
|
|
|
if key.krate == LOCAL_CRATE {
|
2024-05-05 23:57:24 +00:00
|
|
|
self.local.complete(key.index, value, index)
|
2024-01-14 23:08:02 +00:00
|
|
|
} else {
|
|
|
|
self.foreign.complete(key, value, index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
|
2024-05-05 23:57:24 +00:00
|
|
|
self.local.iter(&mut |key, value, index| {
|
|
|
|
f(&DefId { krate: LOCAL_CRATE, index: *key }, value, index);
|
|
|
|
});
|
2024-01-14 23:08:02 +00:00
|
|
|
self.foreign.iter(f);
|
|
|
|
}
|
|
|
|
}
|
2024-05-05 23:57:24 +00:00
|
|
|
|
|
|
|
impl<K, V> QueryCache for VecCache<K, V, DepNodeIndex>
|
|
|
|
where
|
|
|
|
K: Idx + Eq + Hash + Copy + Debug,
|
|
|
|
V: Copy,
|
|
|
|
{
|
|
|
|
type Key = K;
|
|
|
|
type Value = V;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {
|
|
|
|
self.lookup(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn complete(&self, key: K, value: V, index: DepNodeIndex) {
|
|
|
|
self.complete(key, value, index)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
|
|
|
|
self.iter(f)
|
|
|
|
}
|
|
|
|
}
|