From decfd704fe17b4da16d57cb133ff3d29f9bcf295 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 19 Mar 2020 17:53:31 +0100 Subject: [PATCH] Generalise try_get_cached. --- src/librustc/ty/query/plumbing.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/librustc/ty/query/plumbing.rs b/src/librustc/ty/query/plumbing.rs index 14c0aea7c8f..dbbfc4af825 100644 --- a/src/librustc/ty/query/plumbing.rs +++ b/src/librustc/ty/query/plumbing.rs @@ -247,7 +247,8 @@ fn try_start<'a, 'b, Q>( return TryGetJob::Cycle(Q::handle_cycle_error(tcx, cycle)); } - let cached = tcx.try_get_cached( + let cached = try_get_cached( + tcx, Q::query_state(tcx), (*key).clone(), |value, index| (value.clone(), index), @@ -500,32 +501,34 @@ pub fn try_print_query_stack(handler: &Handler) { eprintln!("end of query stack"); } +} /// 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. #[inline(always)] - fn try_get_cached( - self, - state: &'tcx QueryState, C>, + fn try_get_cached( + tcx: CTX, + state: &QueryState, key: C::Key, // `on_hit` can be called while holding a lock to the query cache on_hit: OnHit, on_miss: OnMiss, ) -> R where - C: QueryCache>, + C: QueryCache, + CTX: QueryContext, OnHit: FnOnce(&C::Value, DepNodeIndex) -> R, - OnMiss: FnOnce(C::Key, QueryLookup<'_, TyCtxt<'tcx>, C::Key, C::Sharded>) -> R, + OnMiss: FnOnce(C::Key, QueryLookup<'_, CTX, C::Key, C::Sharded>) -> R, { state.cache.lookup( state, - QueryStateShard::, C::Key, C::Sharded>::get_cache, + QueryStateShard::::get_cache, key, |value, index| { - if unlikely!(self.prof.enabled()) { - self.prof.query_cache_hit(index.into()); + if unlikely!(tcx.profiler().enabled()) { + tcx.profiler().query_cache_hit(index.into()); } #[cfg(debug_assertions)] { @@ -537,6 +540,7 @@ fn try_get_cached( ) } +impl<'tcx> TyCtxt<'tcx> { #[inline(never)] pub(super) fn get_query> + 'tcx>( self, @@ -545,7 +549,8 @@ pub(super) fn get_query> + 'tcx>( ) -> Q::Value { debug!("ty::query::get_query<{}>(key={:?}, span={:?})", Q::NAME, key, span); - self.try_get_cached( + try_get_cached( + self, Q::query_state(self), key, |value, index| { @@ -819,7 +824,8 @@ pub(super) fn force_query> + 'tcx>( // We may be concurrently trying both execute and force a query. // Ensure that only one of them runs the query. - self.try_get_cached( + try_get_cached( + self, Q::query_state(self), key, |_, _| { -- 2.44.0