1 use crate::dep_graph::DepNodeIndex;
2 use crate::query::plumbing::{QueryCacheStore, QueryLookup};
4 use rustc_arena::TypedArena;
5 use rustc_data_structures::fx::FxHashMap;
6 use rustc_data_structures::sharded::Sharded;
7 use rustc_data_structures::sync::WorkerLocal;
8 use std::default::Default;
11 use std::marker::PhantomData;
13 pub trait CacheSelector<K, V> {
17 pub trait QueryStorage {
21 /// Store a value without putting it in the cache.
22 /// This is meant to be used with cycle errors.
23 fn store_nocache(&self, value: Self::Value) -> Self::Stored;
26 pub trait QueryCache: QueryStorage + Sized {
27 type Key: Hash + Eq + Clone + Debug;
28 type Sharded: Default;
30 /// Checks if the query is already computed and in the cache.
31 /// It returns the shard index and a lock guard to the shard,
32 /// which will be used if the query is not in the cache and we need
34 fn lookup<'s, R, OnHit>(
36 state: &'s QueryCacheStore<Self>,
38 // `on_hit` can be called while holding a lock to the query state shard.
40 ) -> Result<R, QueryLookup>
42 OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R;
46 lock_sharded_storage: &mut Self::Sharded,
54 shards: &Sharded<Self::Sharded>,
55 f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex),
59 pub struct DefaultCacheSelector;
61 impl<K: Eq + Hash, V: Clone> CacheSelector<K, V> for DefaultCacheSelector {
62 type Cache = DefaultCache<K, V>;
65 pub struct DefaultCache<K, V>(PhantomData<(K, V)>);
67 impl<K, V> Default for DefaultCache<K, V> {
68 fn default() -> Self {
69 DefaultCache(PhantomData)
73 impl<K: Eq + Hash, V: Clone + Debug> QueryStorage for DefaultCache<K, V> {
78 fn store_nocache(&self, value: Self::Value) -> Self::Stored {
79 // We have no dedicated storage
84 impl<K, V> QueryCache for DefaultCache<K, V>
86 K: Eq + Hash + Clone + Debug,
90 type Sharded = FxHashMap<K, (V, DepNodeIndex)>;
93 fn lookup<'s, R, OnHit>(
95 state: &'s QueryCacheStore<Self>,
98 ) -> Result<R, QueryLookup>
100 OnHit: FnOnce(&V, DepNodeIndex) -> R,
102 let (lookup, lock) = state.get_lookup(key);
103 let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
105 if let Some((_, value)) = result {
106 let hit_result = on_hit(&value.0, value.1);
116 lock_sharded_storage: &mut Self::Sharded,
121 lock_sharded_storage.insert(key, (value.clone(), index));
127 shards: &Sharded<Self::Sharded>,
128 f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex),
130 let shards = shards.lock_shards();
131 for shard in shards.iter() {
132 for (k, v) in shard.iter() {
139 pub struct ArenaCacheSelector<'tcx>(PhantomData<&'tcx ()>);
141 impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<K, V> for ArenaCacheSelector<'tcx> {
142 type Cache = ArenaCache<'tcx, K, V>;
145 pub struct ArenaCache<'tcx, K, V> {
146 arena: WorkerLocal<TypedArena<(V, DepNodeIndex)>>,
147 phantom: PhantomData<(K, &'tcx V)>,
150 impl<'tcx, K, V> Default for ArenaCache<'tcx, K, V> {
151 fn default() -> Self {
152 ArenaCache { arena: WorkerLocal::new(|_| TypedArena::default()), phantom: PhantomData }
156 impl<'tcx, K: Eq + Hash, V: Debug + 'tcx> QueryStorage for ArenaCache<'tcx, K, V> {
158 type Stored = &'tcx V;
161 fn store_nocache(&self, value: Self::Value) -> Self::Stored {
162 let value = self.arena.alloc((value, DepNodeIndex::INVALID));
163 let value = unsafe { &*(&value.0 as *const _) };
168 impl<'tcx, K, V: 'tcx> QueryCache for ArenaCache<'tcx, K, V>
170 K: Eq + Hash + Clone + Debug,
174 type Sharded = FxHashMap<K, &'tcx (V, DepNodeIndex)>;
177 fn lookup<'s, R, OnHit>(
179 state: &'s QueryCacheStore<Self>,
182 ) -> Result<R, QueryLookup>
184 OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
186 let (lookup, lock) = state.get_lookup(key);
187 let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
189 if let Some((_, value)) = result {
190 let hit_result = on_hit(&&value.0, value.1);
200 lock_sharded_storage: &mut Self::Sharded,
205 let value = self.arena.alloc((value, index));
206 let value = unsafe { &*(value as *const _) };
207 lock_sharded_storage.insert(key, value);
213 shards: &Sharded<Self::Sharded>,
214 f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex),
216 let shards = shards.lock_shards();
217 for shard in shards.iter() {
218 for (k, v) in shard.iter() {