use crate::dep_graph::DepNodeIndex;
-use crate::query::plumbing::{QueryLookup, QueryState};
+use crate::query::plumbing::{QueryCacheStore, QueryLookup};
use rustc_arena::TypedArena;
use rustc_data_structures::fx::FxHashMap;
/// 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<D, Q, R, OnHit, OnMiss>(
+ fn lookup<'s, R, OnHit>(
&self,
- state: &QueryState<D, Q, Self>,
- key: Self::Key,
+ state: &'s QueryCacheStore<Self>,
+ key: &Self::Key,
// `on_hit` can be called while holding a lock to the query state shard.
on_hit: OnHit,
- on_miss: OnMiss,
- ) -> R
+ ) -> Result<R, QueryLookup>
where
- OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R,
- OnMiss: FnOnce(Self::Key, QueryLookup<'_, D, Q, Self::Key, Self::Sharded>) -> R;
+ OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R;
fn complete(
&self,
index: DepNodeIndex,
) -> Self::Stored;
- fn iter<R, L>(
+ fn iter<R>(
&self,
- shards: &Sharded<L>,
- get_shard: impl Fn(&mut L) -> &mut Self::Sharded,
+ shards: &Sharded<Self::Sharded>,
f: impl for<'a> FnOnce(
- Box<dyn Iterator<Item = (&'a Self::Key, &'a Self::Value, DepNodeIndex)> + 'a>,
+ &'a mut dyn Iterator<Item = (&'a Self::Key, &'a Self::Value, DepNodeIndex)>,
) -> R,
) -> R;
}
type Sharded = FxHashMap<K, (V, DepNodeIndex)>;
#[inline(always)]
- fn lookup<D, Q, R, OnHit, OnMiss>(
+ fn lookup<'s, R, OnHit>(
&self,
- state: &QueryState<D, Q, Self>,
- key: K,
+ state: &'s QueryCacheStore<Self>,
+ key: &K,
on_hit: OnHit,
- on_miss: OnMiss,
- ) -> R
+ ) -> Result<R, QueryLookup>
where
OnHit: FnOnce(&V, DepNodeIndex) -> R,
- OnMiss: FnOnce(K, QueryLookup<'_, D, Q, K, Self::Sharded>) -> R,
{
- let mut lookup = state.get_lookup(&key);
- let lock = &mut *lookup.lock;
+ let (lookup, lock) = state.get_lookup(key);
+ let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
- let result = lock.cache.raw_entry().from_key_hashed_nocheck(lookup.key_hash, &key);
-
- if let Some((_, value)) = result { on_hit(&value.0, value.1) } else { on_miss(key, lookup) }
+ if let Some((_, value)) = result {
+ let hit_result = on_hit(&value.0, value.1);
+ Ok(hit_result)
+ } else {
+ Err(lookup)
+ }
}
#[inline]
value
}
- fn iter<R, L>(
+ fn iter<R>(
&self,
- shards: &Sharded<L>,
- get_shard: impl Fn(&mut L) -> &mut Self::Sharded,
- f: impl for<'a> FnOnce(Box<dyn Iterator<Item = (&'a K, &'a V, DepNodeIndex)> + 'a>) -> R,
+ shards: &Sharded<Self::Sharded>,
+ f: impl for<'a> FnOnce(&'a mut dyn Iterator<Item = (&'a K, &'a V, DepNodeIndex)>) -> R,
) -> R {
- let mut shards = shards.lock_shards();
- let mut shards: Vec<_> = shards.iter_mut().map(|shard| get_shard(shard)).collect();
- let results = shards.iter_mut().flat_map(|shard| shard.iter()).map(|(k, v)| (k, &v.0, v.1));
- f(Box::new(results))
+ let shards = shards.lock_shards();
+ let mut results = shards.iter().flat_map(|shard| shard.iter()).map(|(k, v)| (k, &v.0, v.1));
+ f(&mut results)
}
}
type Sharded = FxHashMap<K, &'tcx (V, DepNodeIndex)>;
#[inline(always)]
- fn lookup<D, Q, R, OnHit, OnMiss>(
+ fn lookup<'s, R, OnHit>(
&self,
- state: &QueryState<D, Q, Self>,
- key: K,
+ state: &'s QueryCacheStore<Self>,
+ key: &K,
on_hit: OnHit,
- on_miss: OnMiss,
- ) -> R
+ ) -> Result<R, QueryLookup>
where
OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
- OnMiss: FnOnce(K, QueryLookup<'_, D, Q, K, Self::Sharded>) -> R,
{
- let mut lookup = state.get_lookup(&key);
- let lock = &mut *lookup.lock;
-
- let result = lock.cache.raw_entry().from_key_hashed_nocheck(lookup.key_hash, &key);
+ let (lookup, lock) = state.get_lookup(key);
+ let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
if let Some((_, value)) = result {
- on_hit(&&value.0, value.1)
+ let hit_result = on_hit(&&value.0, value.1);
+ Ok(hit_result)
} else {
- on_miss(key, lookup)
+ Err(lookup)
}
}
&value.0
}
- fn iter<R, L>(
+ fn iter<R>(
&self,
- shards: &Sharded<L>,
- get_shard: impl Fn(&mut L) -> &mut Self::Sharded,
- f: impl for<'a> FnOnce(Box<dyn Iterator<Item = (&'a K, &'a V, DepNodeIndex)> + 'a>) -> R,
+ shards: &Sharded<Self::Sharded>,
+ f: impl for<'a> FnOnce(&'a mut dyn Iterator<Item = (&'a K, &'a V, DepNodeIndex)>) -> R,
) -> R {
- let mut shards = shards.lock_shards();
- let mut shards: Vec<_> = shards.iter_mut().map(|shard| get_shard(shard)).collect();
- let results = shards.iter_mut().flat_map(|shard| shard.iter()).map(|(k, v)| (k, &v.0, v.1));
- f(Box::new(results))
+ let shards = shards.lock_shards();
+ let mut results = shards.iter().flat_map(|shard| shard.iter()).map(|(k, v)| (k, &v.0, v.1));
+ f(&mut results)
}
}