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};
|
2022-02-20 17:57:52 +00:00
|
|
|
use rustc_data_structures::sync::Lock;
|
2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::{Idx, IndexVec};
|
2020-10-12 14:29:41 +00:00
|
|
|
use std::fmt::Debug;
|
2020-02-08 06:38:00 +00:00
|
|
|
use std::hash::Hash;
|
2022-10-31 23:16:24 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
pub trait CacheSelector<'tcx, V> {
|
|
|
|
type Cache
|
|
|
|
where
|
2023-02-04 15:16:59 +00:00
|
|
|
V: Copy;
|
2022-10-31 23:16:24 +00:00
|
|
|
}
|
2020-02-08 06:38:00 +00:00
|
|
|
|
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-10-31 23:16:24 +00:00
|
|
|
pub struct DefaultCacheSelector<K>(PhantomData<K>);
|
|
|
|
|
|
|
|
impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<'tcx, V> for DefaultCacheSelector<K> {
|
|
|
|
type Cache = DefaultCache<K, V>
|
|
|
|
where
|
2023-02-04 15:16:59 +00:00
|
|
|
V: Copy;
|
2022-10-31 23:16:24 +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);
|
2022-02-20 17:57:52 +00:00
|
|
|
let lock = self.cache.get_shard_by_hash(key_hash).lock();
|
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) {
|
2022-02-20 17:57:52 +00:00
|
|
|
let mut lock = self.cache.get_shard_by_value(&key).lock();
|
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-16 12:16:05 +00:00
|
|
|
let shards = self.cache.lock_shards();
|
|
|
|
for shard in shards.iter() {
|
|
|
|
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 SingleCacheSelector;
|
|
|
|
|
|
|
|
impl<'tcx, V: 'tcx> CacheSelector<'tcx, V> for SingleCacheSelector {
|
|
|
|
type Cache = SingleCache<V>
|
|
|
|
where
|
|
|
|
V: Copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SingleCache<V> {
|
|
|
|
cache: Lock<Option<(V, DepNodeIndex)>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V> Default for SingleCache<V> {
|
|
|
|
fn default() -> Self {
|
|
|
|
SingleCache { cache: Lock::new(None) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)> {
|
|
|
|
*self.cache.lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-02-08 18:53:48 +00:00
|
|
|
fn complete(&self, _key: (), value: V, index: DepNodeIndex) {
|
2023-02-15 22:18:40 +00:00
|
|
|
*self.cache.lock() = Some((value, index));
|
2023-02-03 17:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
|
2023-04-09 21:07:18 +00:00
|
|
|
if let Some(value) = self.cache.lock().as_ref() {
|
|
|
|
f(&(), &value.0, value.1)
|
|
|
|
}
|
2023-02-03 17:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 23:16:24 +00:00
|
|
|
pub struct VecCacheSelector<K>(PhantomData<K>);
|
|
|
|
|
|
|
|
impl<'tcx, K: Idx, V: 'tcx> CacheSelector<'tcx, V> for VecCacheSelector<K> {
|
|
|
|
type Cache = VecCache<K, V>
|
|
|
|
where
|
2023-02-04 15:16:59 +00:00
|
|
|
V: Copy;
|
2022-10-31 23:16:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 17:20:08 +00:00
|
|
|
pub struct VecCache<K: Idx, V> {
|
|
|
|
cache: Sharded<IndexVec<K, Option<(V, DepNodeIndex)>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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)> {
|
2022-10-30 17:20:08 +00:00
|
|
|
let lock = self.cache.get_shard_by_hash(key.index() as u64).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) {
|
2022-10-30 17:20:08 +00:00
|
|
|
let mut lock = self.cache.get_shard_by_hash(key.index() as u64).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)) {
|
2023-08-16 12:16:05 +00:00
|
|
|
let shards = self.cache.lock_shards();
|
|
|
|
for shard in shards.iter() {
|
|
|
|
for (k, v) in shard.iter_enumerated() {
|
2022-10-30 17:20:08 +00:00
|
|
|
if let Some(v) = v {
|
|
|
|
f(&k, &v.0, v.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|