Skip to content

Commit 354eb16

Browse files
committed
Auto merge of #45312 - theotherjimmy:refactor-ensure, r=michaelwoerister
Refactor `ensure` and `try_get_with` There was a bit of code shared between `try_get_with` and `ensure`, after I added `ensure`. I refactored that shared code into a query-agnostic method called `read_node_index`. The new method `read_node_index` will attempt to find the node index (`DepNodeIndex`) of a query. When `read_node_index` finds the `DepNodeIndex`, it marks the current query as a reader of the node it's requesting the index of. This is used by `try_get_with` and `ensure` as it elides the unimportant (to them) details of if the query is invalidated by previous changed computation (Red) or new and if they had to mark the query green. For both `try_get_with` and `ensure`, they just need to know if they can lookup the results or have to reevaluate. @nikomatsakis this is the [refactor we discussed](#45228 (comment)) in the comment thread of #45228
2 parents a651106 + 229bee3 commit 354eb16

File tree

1 file changed

+44
-49
lines changed

1 file changed

+44
-49
lines changed

src/librustc/ty/maps/plumbing.rs

+44-49
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! that generate the actual methods on tcx which find and execute the
1313
//! provider, manage the caches, and so forth.
1414
15-
use dep_graph::{DepNodeIndex, DepNode, DepKind};
15+
use dep_graph::{DepNodeIndex, DepNode, DepKind, DepNodeColor};
1616
use errors::{Diagnostic, DiagnosticBuilder};
1717
use ty::{TyCtxt};
1818
use ty::maps::Query; // NB: actually generated by the macros in this file
@@ -133,6 +133,40 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
133133

134134
Ok(result)
135135
}
136+
137+
/// Try to read a node index for the node dep_node.
138+
/// A node will have an index, when it's already been marked green, or when we can mark it
139+
/// green. This function will mark the current task as a reader of the specified node, when
140+
/// the a node index can be found for that node.
141+
pub(super) fn try_mark_green_and_read(self, dep_node: &DepNode) -> Option<DepNodeIndex> {
142+
match self.dep_graph.node_color(dep_node) {
143+
Some(DepNodeColor::Green(dep_node_index)) => {
144+
self.dep_graph.read_index(dep_node_index);
145+
Some(dep_node_index)
146+
}
147+
Some(DepNodeColor::Red) => {
148+
None
149+
}
150+
None => {
151+
// try_mark_green (called below) will panic when full incremental
152+
// compilation is disabled. If that's the case, we can't try to mark nodes
153+
// as green anyway, so we can safely return None here.
154+
if !self.dep_graph.is_fully_enabled() {
155+
return None;
156+
}
157+
match self.dep_graph.try_mark_green(self, &dep_node) {
158+
Some(dep_node_index) => {
159+
debug_assert!(self.dep_graph.is_green(dep_node_index));
160+
self.dep_graph.read_index(dep_node_index);
161+
Some(dep_node_index)
162+
}
163+
None => {
164+
None
165+
}
166+
}
167+
}
168+
}
169+
}
136170
}
137171

138172
// If enabled, send a message to the profile-queries thread
@@ -309,25 +343,8 @@ macro_rules! define_maps {
309343
}
310344

311345
if !dep_node.kind.is_input() {
312-
use dep_graph::DepNodeColor;
313-
if let Some(DepNodeColor::Green(dep_node_index)) = tcx.dep_graph
314-
.node_color(&dep_node) {
346+
if let Some(dep_node_index) = tcx.try_mark_green_and_read(&dep_node) {
315347
profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
316-
tcx.dep_graph.read_index(dep_node_index);
317-
return Self::load_from_disk_and_cache_in_memory(tcx,
318-
key,
319-
span,
320-
dep_node_index)
321-
}
322-
323-
debug!("ty::queries::{}::try_get_with(key={:?}) - running try_mark_green",
324-
stringify!($name),
325-
key);
326-
327-
if let Some(dep_node_index) = tcx.dep_graph.try_mark_green(tcx, &dep_node) {
328-
debug_assert!(tcx.dep_graph.is_green(dep_node_index));
329-
profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
330-
tcx.dep_graph.read_index(dep_node_index);
331348
return Self::load_from_disk_and_cache_in_memory(tcx,
332349
key,
333350
span,
@@ -357,36 +374,14 @@ macro_rules! define_maps {
357374
// Ensuring an "input" or anonymous query makes no sense
358375
assert!(!dep_node.kind.is_anon());
359376
assert!(!dep_node.kind.is_input());
360-
use dep_graph::DepNodeColor;
361-
match tcx.dep_graph.node_color(&dep_node) {
362-
Some(DepNodeColor::Green(dep_node_index)) => {
363-
tcx.dep_graph.read_index(dep_node_index);
364-
}
365-
Some(DepNodeColor::Red) => {
366-
// A DepNodeColor::Red DepNode means that this query was executed
367-
// before. We can not call `dep_graph.read()` here as we don't have
368-
// the DepNodeIndex. Instead, We call the query again to issue the
369-
// appropriate `dep_graph.read()` call. The performance cost this
370-
// introduces should be negligible as we'll immediately hit the
371-
// in-memory cache.
372-
let _ = tcx.$name(key);
373-
}
374-
None => {
375-
// Huh
376-
if !tcx.dep_graph.is_fully_enabled() {
377-
let _ = tcx.$name(key);
378-
return;
379-
}
380-
match tcx.dep_graph.try_mark_green(tcx, &dep_node) {
381-
Some(dep_node_index) => {
382-
debug_assert!(tcx.dep_graph.is_green(dep_node_index));
383-
tcx.dep_graph.read_index(dep_node_index);
384-
}
385-
None => {
386-
let _ = tcx.$name(key);
387-
}
388-
}
389-
}
377+
if tcx.try_mark_green_and_read(&dep_node).is_none() {
378+
// A None return from `try_mark_green_and_read` means that this is either
379+
// a new dep node or that the dep node has already been marked red.
380+
// Either way, we can't call `dep_graph.read()` as we don't have the
381+
// DepNodeIndex. We must invoke the query itself. The performance cost
382+
// this introduces should be negligible as we'll immediately hit the
383+
// in-memory cache, or another query down the line will.
384+
let _ = tcx.$name(key);
390385
}
391386
}
392387

0 commit comments

Comments
 (0)