X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=compiler%2Frustc_query_system%2Fsrc%2Fquery%2Fcaches.rs;h=c5fa4c9ee6fc1e2d25d344a00c19ecb87f219a72;hb=75ef06892089e0cf53b4a86419c7ff3b3c1f3c4c;hp=011c2ceebb7148797d92260cc89576b8c5ccd0b5;hpb=46ad16b70f16795f419eb04b823f4c6485867b32;p=rust.git diff --git a/compiler/rustc_query_system/src/query/caches.rs b/compiler/rustc_query_system/src/query/caches.rs index 011c2ceebb7..c5fa4c9ee6f 100644 --- a/compiler/rustc_query_system/src/query/caches.rs +++ b/compiler/rustc_query_system/src/query/caches.rs @@ -1,9 +1,8 @@ use crate::dep_graph::DepNodeIndex; -use crate::query::plumbing::{QueryCacheStore, QueryLookup}; use rustc_arena::TypedArena; use rustc_data_structures::fx::FxHashMap; -use rustc_data_structures::sharded::Sharded; +use rustc_data_structures::sharded::{self, Sharded}; use rustc_data_structures::sync::WorkerLocal; use std::default::Default; use std::fmt::Debug; @@ -25,35 +24,23 @@ pub trait QueryStorage { pub trait QueryCache: QueryStorage + Sized { type Key: Hash + Eq + Clone + Debug; - type Sharded: Default; /// Checks if the query is already computed and in the cache. /// It returns the shard index and a lock guard to the shard, /// which will be used if the query is not in the cache and we need /// to compute it. - fn lookup<'s, R, OnHit>( + fn lookup( &self, - state: &'s QueryCacheStore, key: &Self::Key, // `on_hit` can be called while holding a lock to the query state shard. on_hit: OnHit, - ) -> Result + ) -> Result where OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R; - fn complete( - &self, - lock_sharded_storage: &mut Self::Sharded, - key: Self::Key, - value: Self::Value, - index: DepNodeIndex, - ) -> Self::Stored; + fn complete(&self, key: Self::Key, value: Self::Value, index: DepNodeIndex) -> Self::Stored; - fn iter( - &self, - shards: &Sharded, - f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex), - ); + fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)); } pub struct DefaultCacheSelector; @@ -62,11 +49,13 @@ impl CacheSelector for DefaultCacheSelector { type Cache = DefaultCache; } -pub struct DefaultCache(PhantomData<(K, V)>); +pub struct DefaultCache { + shards: Sharded>, +} impl Default for DefaultCache { fn default() -> Self { - DefaultCache(PhantomData) + DefaultCache { shards: Default::default() } } } @@ -87,47 +76,33 @@ impl QueryCache for DefaultCache V: Clone + Debug, { type Key = K; - type Sharded = FxHashMap; #[inline(always)] - fn lookup<'s, R, OnHit>( - &self, - state: &'s QueryCacheStore, - key: &K, - on_hit: OnHit, - ) -> Result + fn lookup(&self, key: &K, on_hit: OnHit) -> Result where OnHit: FnOnce(&V, DepNodeIndex) -> R, { - let (lookup, lock) = state.get_lookup(key); - let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key); + let key_hash = sharded::make_hash(key); + let shard = sharded::get_shard_index_by_hash(key_hash); + let lock = self.shards.get_shard_by_index(shard).lock(); + let result = lock.raw_entry().from_key_hashed_nocheck(key_hash, key); if let Some((_, value)) = result { let hit_result = on_hit(&value.0, value.1); Ok(hit_result) } else { - Err(lookup) + Err(()) } } #[inline] - fn complete( - &self, - lock_sharded_storage: &mut Self::Sharded, - key: K, - value: V, - index: DepNodeIndex, - ) -> Self::Stored { - lock_sharded_storage.insert(key, (value.clone(), index)); + fn complete(&self, key: K, value: V, index: DepNodeIndex) -> Self::Stored { + self.shards.get_shard_by_value(&key).lock().insert(key, (value.clone(), index)); value } - fn iter( - &self, - shards: &Sharded, - f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex), - ) { - let shards = shards.lock_shards(); + fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) { + let shards = self.shards.lock_shards(); for shard in shards.iter() { for (k, v) in shard.iter() { f(k, &v.0, v.1); @@ -144,12 +119,15 @@ impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector for ArenaCacheSelector<'tc pub struct ArenaCache<'tcx, K, V> { arena: WorkerLocal>, - phantom: PhantomData<(K, &'tcx V)>, + shards: Sharded>, } impl<'tcx, K, V> Default for ArenaCache<'tcx, K, V> { fn default() -> Self { - ArenaCache { arena: WorkerLocal::new(|_| TypedArena::default()), phantom: PhantomData } + ArenaCache { + arena: WorkerLocal::new(|_| TypedArena::default()), + shards: Default::default(), + } } } @@ -171,49 +149,35 @@ impl<'tcx, K, V: 'tcx> QueryCache for ArenaCache<'tcx, K, V> V: Debug, { type Key = K; - type Sharded = FxHashMap; #[inline(always)] - fn lookup<'s, R, OnHit>( - &self, - state: &'s QueryCacheStore, - key: &K, - on_hit: OnHit, - ) -> Result + fn lookup(&self, key: &K, on_hit: OnHit) -> Result where OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R, { - let (lookup, lock) = state.get_lookup(key); - let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key); + let key_hash = sharded::make_hash(key); + let shard = sharded::get_shard_index_by_hash(key_hash); + let lock = self.shards.get_shard_by_index(shard).lock(); + let result = lock.raw_entry().from_key_hashed_nocheck(key_hash, key); if let Some((_, value)) = result { let hit_result = on_hit(&&value.0, value.1); Ok(hit_result) } else { - Err(lookup) + Err(()) } } #[inline] - fn complete( - &self, - lock_sharded_storage: &mut Self::Sharded, - key: K, - value: V, - index: DepNodeIndex, - ) -> Self::Stored { + fn complete(&self, key: K, value: V, index: DepNodeIndex) -> Self::Stored { let value = self.arena.alloc((value, index)); let value = unsafe { &*(value as *const _) }; - lock_sharded_storage.insert(key, value); + self.shards.get_shard_by_value(&key).lock().insert(key, value); &value.0 } - fn iter( - &self, - shards: &Sharded, - f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex), - ) { - let shards = shards.lock_shards(); + fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) { + let shards = self.shards.lock_shards(); for shard in shards.iter() { for (k, v) in shard.iter() { f(k, &v.0, v.1);