2020-02-08 06:38:00 +00:00
|
|
|
use crate::dep_graph::DepNodeIndex;
|
|
|
|
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2023-08-16 12:16:05 +00:00
|
|
|
use rustc_data_structures::sharded::{self, Sharded};
|
2024-01-14 23:08:02 +00:00
|
|
|
use rustc_data_structures::sync::{Lock, OnceLock};
|
|
|
|
use rustc_hir::def_id::LOCAL_CRATE;
|
2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::{Idx, IndexVec};
|
2024-01-14 23:08:02 +00:00
|
|
|
use rustc_span::def_id::DefId;
|
|
|
|
use rustc_span::def_id::DefIndex;
|
2020-10-12 14:29:41 +00:00
|
|
|
use std::fmt::Debug;
|
2020-02-08 06:38:00 +00:00
|
|
|
use std::hash::Hash;
|
|
|
|
|
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
|
|
|
|
|
|
|
/// Checks if the query is already computed and 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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 17:20:08 +00:00
|
|
|
pub struct VecCache<K: Idx, V> {
|
2024-04-06 14:49:31 +00:00
|
|
|
cache: Lock<IndexVec<K, Option<(V, DepNodeIndex)>>>,
|
2022-10-30 17:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: Idx, V> Default for VecCache<K, V> {
|
|
|
|
fn default() -> Self {
|
|
|
|
VecCache { cache: Default::default() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K, V> QueryCache for VecCache<K, V>
|
|
|
|
where
|
2023-02-17 16:33:05 +00:00
|
|
|
K: Eq + Idx + Copy + Debug,
|
2023-03-26 09:00:26 +00:00
|
|
|
V: Copy,
|
2022-10-30 17:20:08 +00:00
|
|
|
{
|
|
|
|
type Key = K;
|
2023-02-07 07:32:30 +00:00
|
|
|
type Value = V;
|
2022-10-30 17:20:08 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2023-02-04 15:16:59 +00:00
|
|
|
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {
|
2024-04-06 14:49:31 +00:00
|
|
|
let lock = self.cache.lock();
|
2023-02-04 15:16:59 +00:00
|
|
|
if let Some(Some(value)) = lock.get(*key) { Some(*value) } else { None }
|
2022-10-30 17:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-02-08 18:53:48 +00:00
|
|
|
fn complete(&self, key: K, value: V, index: DepNodeIndex) {
|
2024-04-06 14:49:31 +00:00
|
|
|
let mut lock = self.cache.lock();
|
2023-02-15 22:18:40 +00:00
|
|
|
lock.insert(key, (value, index));
|
2022-10-30 17:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
|
2024-04-06 14:49:31 +00:00
|
|
|
for (k, v) in self.cache.lock().iter_enumerated() {
|
|
|
|
if let Some(v) = v {
|
|
|
|
f(&k, &v.0, v.1);
|
2022-10-30 17:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
///
|
|
|
|
/// The second element of the tuple is the set of keys actually present in the IndexVec, used
|
|
|
|
/// for faster iteration in `iter()`.
|
|
|
|
local: Lock<(IndexVec<DefIndex, Option<(V, DepNodeIndex)>>, Vec<DefIndex>)>,
|
|
|
|
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 {
|
|
|
|
let cache = self.local.lock();
|
|
|
|
cache.0.get(key.index).and_then(|v| *v)
|
|
|
|
} else {
|
|
|
|
self.foreign.lookup(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn complete(&self, key: DefId, value: V, index: DepNodeIndex) {
|
|
|
|
if key.krate == LOCAL_CRATE {
|
|
|
|
let mut cache = self.local.lock();
|
|
|
|
let (cache, present) = &mut *cache;
|
|
|
|
let slot = cache.ensure_contains_elem(key.index, Default::default);
|
|
|
|
if slot.is_none() {
|
|
|
|
// FIXME: Only store the present set when running in incremental mode. `iter` is not
|
|
|
|
// used outside of saving caches to disk and self-profile.
|
|
|
|
present.push(key.index);
|
|
|
|
}
|
|
|
|
*slot = Some((value, index));
|
|
|
|
} else {
|
|
|
|
self.foreign.complete(key, value, index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
|
|
|
|
let guard = self.local.lock();
|
|
|
|
let (cache, present) = &*guard;
|
|
|
|
for &idx in present.iter() {
|
|
|
|
let value = cache[idx].unwrap();
|
|
|
|
f(&DefId { krate: LOCAL_CRATE, index: idx }, &value.0, value.1);
|
|
|
|
}
|
|
|
|
self.foreign.iter(f);
|
|
|
|
}
|
|
|
|
}
|