Create a single value cache for the () query key

This commit is contained in:
John Kåre Alsaker 2023-02-03 18:39:31 +01:00
parent 2b3f260e74
commit 80d265240b
3 changed files with 50 additions and 4 deletions

View file

@ -5,7 +5,6 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sharded;
#[cfg(parallel_compiler)]
use rustc_data_structures::sharded::Sharded;
#[cfg(not(parallel_compiler))]
use rustc_data_structures::sync::Lock;
use rustc_data_structures::sync::WorkerLocal;
use rustc_index::vec::{Idx, IndexVec};
@ -117,6 +116,52 @@ where
}
}
pub struct SingleCacheSelector;
impl<'tcx, V: 'tcx> CacheSelector<'tcx, V> for SingleCacheSelector {
type Cache = SingleCache<V>
where
V: Copy;
type ArenaCache = ArenaCache<'tcx, (), V>;
}
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: Copy + Debug> QueryStorage for SingleCache<V> {
type Value = V;
type Stored = V;
}
impl<V> QueryCache for SingleCache<V>
where
V: Copy + Debug,
{
type Key = ();
#[inline(always)]
fn lookup(&self, _key: &()) -> Option<(V, DepNodeIndex)> {
*self.cache.lock()
}
#[inline]
fn complete(&self, _key: (), value: V, index: DepNodeIndex) -> Self::Stored {
*self.cache.lock() = Some((value.clone(), index));
value
}
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
self.cache.lock().as_ref().map(|value| f(&(), &value.0, value.1));
}
}
pub struct ArenaCache<'tcx, K, V> {
arena: WorkerLocal<TypedArena<(V, DepNodeIndex)>>,
#[cfg(parallel_compiler)]

View file

@ -8,7 +8,8 @@ pub use self::job::{print_query_stack, QueryInfo, QueryJob, QueryJobId, QueryJob
mod caches;
pub use self::caches::{
CacheSelector, DefaultCacheSelector, QueryCache, QueryStorage, VecCacheSelector,
CacheSelector, DefaultCacheSelector, QueryCache, QueryStorage, SingleCacheSelector,
VecCacheSelector,
};
mod config;