|
1 | 1 | use std::fmt::Debug;
|
2 | 2 | use std::hash::Hash;
|
3 | 3 |
|
4 |
| -use rustc_data_structures::fx::FxHashMap; |
| 4 | +use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; |
5 | 5 | use rustc_data_structures::sharded::{self, Sharded};
|
6 | 6 | use rustc_data_structures::sync::OnceLock;
|
7 | 7 | pub use rustc_data_structures::vec_cache::VecCache;
|
@@ -187,3 +187,48 @@ where
|
187 | 187 | self.iter(f)
|
188 | 188 | }
|
189 | 189 | }
|
| 190 | + |
| 191 | +pub struct IndexCache<K, V> { |
| 192 | + cache: Sharded<FxIndexMap<K, (V, DepNodeIndex)>>, |
| 193 | +} |
| 194 | + |
| 195 | +impl<K, V> Default for IndexCache<K, V> { |
| 196 | + fn default() -> Self { |
| 197 | + IndexCache { cache: Default::default() } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +impl<K, V> QueryCache for IndexCache<K, V> |
| 202 | +where |
| 203 | + K: Eq + Hash + Copy + Debug, |
| 204 | + V: Copy, |
| 205 | +{ |
| 206 | + type Key = K; |
| 207 | + type Value = V; |
| 208 | + |
| 209 | + #[inline(always)] |
| 210 | + fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> { |
| 211 | + use indexmap::map::raw_entry_v1::RawEntryApiV1; |
| 212 | + let key_hash = sharded::make_hash(key); |
| 213 | + let lock = self.cache.lock_shard_by_hash(key_hash); |
| 214 | + let result = lock.raw_entry_v1().from_key_hashed_nocheck(key_hash, key); |
| 215 | + |
| 216 | + if let Some((_, value)) = result { Some(*value) } else { None } |
| 217 | + } |
| 218 | + |
| 219 | + #[inline] |
| 220 | + fn complete(&self, key: K, value: V, index: DepNodeIndex) { |
| 221 | + let mut lock = self.cache.lock_shard_by_value(&key); |
| 222 | + // We may be overwriting another value. This is all right, since the dep-graph |
| 223 | + // will check that the fingerprint matches. |
| 224 | + lock.insert(key, (value, index)); |
| 225 | + } |
| 226 | + |
| 227 | + fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) { |
| 228 | + for shard in self.cache.lock_shards() { |
| 229 | + for (k, v) in shard.iter() { |
| 230 | + f(k, &v.0, v.1); |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | +} |
0 commit comments