Delete QueryLookup

This was largely just caching the shard value at this point, which is not
particularly useful -- in the use sites the key was being hashed nearby anyway.
This commit is contained in:
Mark Rousskov 2022-02-19 22:56:56 -05:00
parent 9deed6f74e
commit 75ef068920
4 changed files with 24 additions and 45 deletions

View File

@ -222,12 +222,12 @@ macro_rules! define_callbacks {
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, noop);
let lookup = match cached {
match cached {
Ok(()) => return,
Err(lookup) => lookup,
};
Err(()) => (),
}
self.tcx.queries.$name(self.tcx, DUMMY_SP, key, lookup, QueryMode::Ensure);
self.tcx.queries.$name(self.tcx, DUMMY_SP, key, QueryMode::Ensure);
})*
}
@ -251,12 +251,12 @@ macro_rules! define_callbacks {
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, copy);
let lookup = match cached {
match cached {
Ok(value) => return value,
Err(lookup) => lookup,
};
Err(()) => (),
}
self.tcx.queries.$name(self.tcx, self.span, key, lookup, QueryMode::Get).unwrap()
self.tcx.queries.$name(self.tcx, self.span, key, QueryMode::Get).unwrap()
})*
}
@ -314,7 +314,6 @@ macro_rules! define_callbacks {
tcx: TyCtxt<$tcx>,
span: Span,
key: query_keys::$name<$tcx>,
lookup: QueryLookup,
mode: QueryMode,
) -> Option<query_stored::$name<$tcx>>;)*
}

View File

@ -538,12 +538,11 @@ macro_rules! define_queries_struct {
tcx: TyCtxt<$tcx>,
span: Span,
key: query_keys::$name<$tcx>,
lookup: QueryLookup,
mode: QueryMode,
) -> Option<query_stored::$name<$tcx>> {
opt_remap_env_constness!([$($modifiers)*][key]);
let qcx = QueryCtxt { tcx, queries: self };
get_query::<queries::$name<$tcx>, _>(qcx, span, key, lookup, mode)
get_query::<queries::$name<$tcx>, _>(qcx, span, key, mode)
})*
}
};

View File

@ -1,5 +1,4 @@
use crate::dep_graph::DepNodeIndex;
use crate::query::plumbing::QueryLookup;
use rustc_arena::TypedArena;
use rustc_data_structures::fx::FxHashMap;
@ -35,7 +34,7 @@ pub trait QueryCache: QueryStorage + Sized {
key: &Self::Key,
// `on_hit` can be called while holding a lock to the query state shard.
on_hit: OnHit,
) -> Result<R, QueryLookup>
) -> Result<R, ()>
where
OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R;
@ -79,21 +78,20 @@ where
type Key = K;
#[inline(always)]
fn lookup<R, OnHit>(&self, key: &K, on_hit: OnHit) -> Result<R, QueryLookup>
fn lookup<R, OnHit>(&self, key: &K, on_hit: OnHit) -> Result<R, ()>
where
OnHit: FnOnce(&V, DepNodeIndex) -> R,
{
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 lookup = QueryLookup { key_hash, shard };
let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
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(())
}
}
@ -153,21 +151,20 @@ where
type Key = K;
#[inline(always)]
fn lookup<R, OnHit>(&self, key: &K, on_hit: OnHit) -> Result<R, QueryLookup>
fn lookup<R, OnHit>(&self, key: &K, on_hit: OnHit) -> Result<R, ()>
where
OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
{
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 lookup = QueryLookup { key_hash, shard };
let result = lock.raw_entry().from_key_hashed_nocheck(lookup.key_hash, key);
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(())
}
}

View File

@ -24,12 +24,6 @@ use std::hash::{Hash, Hasher};
use std::mem;
use std::ptr;
/// Values used when checking a query cache which can be reused on a cache-miss to execute the query.
pub struct QueryLookup {
pub(super) key_hash: u64,
pub(super) shard: usize,
}
// We compute the key's hash once and then use it for both the
// shard lookup and the hashmap lookup. This relies on the fact
// that both of them use `FxHasher`.
@ -147,13 +141,11 @@ where
state: &'b QueryState<K>,
span: Span,
key: K,
lookup: QueryLookup,
) -> TryGetJob<'b, K>
where
CTX: QueryContext,
{
let shard = lookup.shard;
let mut state_lock = state.shards.get_shard_by_index(shard).lock();
let mut state_lock = state.shards.get_shard_by_value(&key).lock();
let lock = &mut *state_lock;
match lock.active.entry(key) {
@ -303,7 +295,7 @@ pub fn try_get_cached<'a, CTX, C, R, OnHit>(
key: &C::Key,
// `on_hit` can be called while holding a lock to the query cache
on_hit: OnHit,
) -> Result<R, QueryLookup>
) -> Result<R, ()>
where
C: QueryCache,
CTX: DepContext,
@ -324,7 +316,6 @@ fn try_execute_query<CTX, C>(
cache: &C,
span: Span,
key: C::Key,
lookup: QueryLookup,
dep_node: Option<DepNode<CTX::DepKind>>,
query: &QueryVtable<CTX, C::Key, C::Value>,
) -> (C::Stored, Option<DepNodeIndex>)
@ -333,7 +324,7 @@ where
C::Key: Clone + DepNodeParams<CTX::DepContext>,
CTX: QueryContext,
{
match JobOwner::<'_, C::Key>::try_start(&tcx, state, span, key.clone(), lookup) {
match JobOwner::<'_, C::Key>::try_start(&tcx, state, span, key.clone()) {
TryGetJob::NotYetStarted(job) => {
let (result, dep_node_index) = execute_job(tcx, key, dep_node, query, job.id);
let result = job.complete(cache, result, dep_node_index);
@ -675,13 +666,7 @@ pub enum QueryMode {
Ensure,
}
pub fn get_query<Q, CTX>(
tcx: CTX,
span: Span,
key: Q::Key,
lookup: QueryLookup,
mode: QueryMode,
) -> Option<Q::Stored>
pub fn get_query<Q, CTX>(tcx: CTX, span: Span, key: Q::Key, mode: QueryMode) -> Option<Q::Stored>
where
Q: QueryDescription<CTX>,
Q::Key: DepNodeParams<CTX::DepContext>,
@ -705,7 +690,6 @@ where
Q::query_cache(tcx),
span,
key,
lookup,
dep_node,
&query,
);
@ -730,14 +714,14 @@ where
}
});
let lookup = match cached {
match cached {
Ok(()) => return,
Err(lookup) => lookup,
};
Err(()) => {}
}
let query = Q::make_vtable(tcx, &key);
let state = Q::query_state(tcx);
debug_assert!(!query.anon);
try_execute_query(tcx, state, cache, DUMMY_SP, key, lookup, Some(dep_node), &query);
try_execute_query(tcx, state, cache, DUMMY_SP, key, Some(dep_node), &query);
}