From 13d4eb92b8894c54dd3f35c4bc362d3d51008b76 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 30 Dec 2020 22:07:42 +0100 Subject: [PATCH] Do not compute the dep_node twice. --- .../rustc_query_system/src/query/plumbing.rs | 54 ++++++++----------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs index 82392dee165..aabe9973111 100644 --- a/compiler/rustc_query_system/src/query/plumbing.rs +++ b/compiler/rustc_query_system/src/query/plumbing.rs @@ -685,30 +685,6 @@ where (result, dep_node_index) } -#[inline(never)] -fn get_query_impl( - tcx: CTX, - state: &QueryState, - cache: &QueryCacheStore, - span: Span, - key: C::Key, - lookup: QueryLookup, - query: &QueryVtable, - compute: fn(CTX::DepContext, C::Key) -> C::Value, -) -> C::Stored -where - CTX: QueryContext, - C: QueryCache, - C::Key: DepNodeParams, -{ - let (result, dep_node_index) = - try_execute_query(tcx, state, cache, span, key, lookup, None, query, compute); - if let Some(dep_node_index) = dep_node_index { - tcx.dep_context().dep_graph().read_index(dep_node_index) - } - result -} - /// Ensure that either this query has all green inputs or been executed. /// Executing `query::ensure(D)` is considered a read of the dep-node `D`. /// Returns true if the query should still run. @@ -718,13 +694,17 @@ where /// /// Note: The optimization is only available during incr. comp. #[inline(never)] -fn ensure_must_run(tcx: CTX, key: &K, query: &QueryVtable) -> bool +fn ensure_must_run( + tcx: CTX, + key: &K, + query: &QueryVtable, +) -> (bool, Option>) where K: crate::dep_graph::DepNodeParams, CTX: QueryContext, { if query.eval_always { - return true; + return (true, None); } // Ensuring an anonymous query makes no sense @@ -741,12 +721,12 @@ where // DepNodeIndex. We must invoke the query itself. The performance cost // this introduces should be negligible as we'll immediately hit the // in-memory cache, or another query down the line will. - true + (true, Some(dep_node)) } Some((_, dep_node_index)) => { dep_graph.read_index(dep_node_index); tcx.dep_context().profiler().query_cache_hit(dep_node_index.into()); - false + (false, None) } } } @@ -808,25 +788,33 @@ where CTX: QueryContext, { let query = &Q::VTABLE; - if let QueryMode::Ensure = mode { - if !ensure_must_run(tcx, &key, query) { + let dep_node = if let QueryMode::Ensure = mode { + let (must_run, dep_node) = ensure_must_run(tcx, &key, query); + if !must_run { return None; } - } + dep_node + } else { + None + }; debug!("ty::query::get_query<{}>(key={:?}, span={:?})", Q::NAME, key, span); let compute = Q::compute_fn(tcx, &key); - let value = get_query_impl( + let (result, dep_node_index) = try_execute_query( tcx, Q::query_state(tcx), Q::query_cache(tcx), span, key, lookup, + dep_node, query, compute, ); - Some(value) + if let Some(dep_node_index) = dep_node_index { + tcx.dep_context().dep_graph().read_index(dep_node_index) + } + Some(result) } pub fn force_query(tcx: CTX, dep_node: &DepNode) -> bool