Skip to content

Commit 13d4eb9

Browse files
committed
Do not compute the dep_node twice.
1 parent 283a8e1 commit 13d4eb9

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

compiler/rustc_query_system/src/query/plumbing.rs

+21-33
Original file line numberDiff line numberDiff line change
@@ -685,30 +685,6 @@ where
685685
(result, dep_node_index)
686686
}
687687

688-
#[inline(never)]
689-
fn get_query_impl<CTX, C>(
690-
tcx: CTX,
691-
state: &QueryState<CTX::DepKind, C::Key>,
692-
cache: &QueryCacheStore<C>,
693-
span: Span,
694-
key: C::Key,
695-
lookup: QueryLookup,
696-
query: &QueryVtable<CTX, C::Key, C::Value>,
697-
compute: fn(CTX::DepContext, C::Key) -> C::Value,
698-
) -> C::Stored
699-
where
700-
CTX: QueryContext,
701-
C: QueryCache,
702-
C::Key: DepNodeParams<CTX::DepContext>,
703-
{
704-
let (result, dep_node_index) =
705-
try_execute_query(tcx, state, cache, span, key, lookup, None, query, compute);
706-
if let Some(dep_node_index) = dep_node_index {
707-
tcx.dep_context().dep_graph().read_index(dep_node_index)
708-
}
709-
result
710-
}
711-
712688
/// Ensure that either this query has all green inputs or been executed.
713689
/// Executing `query::ensure(D)` is considered a read of the dep-node `D`.
714690
/// Returns true if the query should still run.
@@ -718,13 +694,17 @@ where
718694
///
719695
/// Note: The optimization is only available during incr. comp.
720696
#[inline(never)]
721-
fn ensure_must_run<CTX, K, V>(tcx: CTX, key: &K, query: &QueryVtable<CTX, K, V>) -> bool
697+
fn ensure_must_run<CTX, K, V>(
698+
tcx: CTX,
699+
key: &K,
700+
query: &QueryVtable<CTX, K, V>,
701+
) -> (bool, Option<DepNode<CTX::DepKind>>)
722702
where
723703
K: crate::dep_graph::DepNodeParams<CTX::DepContext>,
724704
CTX: QueryContext,
725705
{
726706
if query.eval_always {
727-
return true;
707+
return (true, None);
728708
}
729709

730710
// Ensuring an anonymous query makes no sense
@@ -741,12 +721,12 @@ where
741721
// DepNodeIndex. We must invoke the query itself. The performance cost
742722
// this introduces should be negligible as we'll immediately hit the
743723
// in-memory cache, or another query down the line will.
744-
true
724+
(true, Some(dep_node))
745725
}
746726
Some((_, dep_node_index)) => {
747727
dep_graph.read_index(dep_node_index);
748728
tcx.dep_context().profiler().query_cache_hit(dep_node_index.into());
749-
false
729+
(false, None)
750730
}
751731
}
752732
}
@@ -808,25 +788,33 @@ where
808788
CTX: QueryContext,
809789
{
810790
let query = &Q::VTABLE;
811-
if let QueryMode::Ensure = mode {
812-
if !ensure_must_run(tcx, &key, query) {
791+
let dep_node = if let QueryMode::Ensure = mode {
792+
let (must_run, dep_node) = ensure_must_run(tcx, &key, query);
793+
if !must_run {
813794
return None;
814795
}
815-
}
796+
dep_node
797+
} else {
798+
None
799+
};
816800

817801
debug!("ty::query::get_query<{}>(key={:?}, span={:?})", Q::NAME, key, span);
818802
let compute = Q::compute_fn(tcx, &key);
819-
let value = get_query_impl(
803+
let (result, dep_node_index) = try_execute_query(
820804
tcx,
821805
Q::query_state(tcx),
822806
Q::query_cache(tcx),
823807
span,
824808
key,
825809
lookup,
810+
dep_node,
826811
query,
827812
compute,
828813
);
829-
Some(value)
814+
if let Some(dep_node_index) = dep_node_index {
815+
tcx.dep_context().dep_graph().read_index(dep_node_index)
816+
}
817+
Some(result)
830818
}
831819

832820
pub fn force_query<Q, CTX>(tcx: CTX, dep_node: &DepNode<CTX::DepKind>) -> bool

0 commit comments

Comments
 (0)