Skip to content

Commit eb38d42

Browse files
authored
Auto merge of #36973 - nnethercote:dep_graph, r=nikomatsakis
Don't enqueue onto a disabled dep_graph. This commit guards all calls to `DepGraphThreadData::enqueue` with a check to make sure it is enabled. This avoids some useless allocation and vector manipulations when it is disabled (i.e. when incremental compilation is off) which improves speed by 1--2% on most of the rustc-benchmarks. This commit has an observable functional change: when the dep_graph is disabled its `shadow_graph` will no longer receive messages. This should be ok because these message are only used when debug assertions are enabled. r? @nikomatsakis
2 parents dfd98eb + cde42cd commit eb38d42

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

src/librustc/dep_graph/graph.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,15 @@ impl DepGraph {
5151
}
5252
}
5353

54-
/// True if we are actually building a dep-graph. If this returns false,
55-
/// then the other methods on this `DepGraph` will have no net effect.
56-
#[inline]
57-
pub fn enabled(&self) -> bool {
58-
self.data.thread.enabled()
59-
}
60-
6154
pub fn query(&self) -> DepGraphQuery<DefId> {
6255
self.data.thread.query()
6356
}
6457

65-
pub fn in_ignore<'graph>(&'graph self) -> raii::IgnoreTask<'graph> {
58+
pub fn in_ignore<'graph>(&'graph self) -> Option<raii::IgnoreTask<'graph>> {
6659
raii::IgnoreTask::new(&self.data.thread)
6760
}
6861

69-
pub fn in_task<'graph>(&'graph self, key: DepNode<DefId>) -> raii::DepTask<'graph> {
62+
pub fn in_task<'graph>(&'graph self, key: DepNode<DefId>) -> Option<raii::DepTask<'graph>> {
7063
raii::DepTask::new(&self.data.thread, key)
7164
}
7265

@@ -85,11 +78,15 @@ impl DepGraph {
8578
}
8679

8780
pub fn read(&self, v: DepNode<DefId>) {
88-
self.data.thread.enqueue(DepMessage::Read(v));
81+
if self.data.thread.is_enqueue_enabled() {
82+
self.data.thread.enqueue(DepMessage::Read(v));
83+
}
8984
}
9085

9186
pub fn write(&self, v: DepNode<DefId>) {
92-
self.data.thread.enqueue(DepMessage::Write(v));
87+
if self.data.thread.is_enqueue_enabled() {
88+
self.data.thread.enqueue(DepMessage::Write(v));
89+
}
9390
}
9491

9592
/// Indicates that a previous work product exists for `v`. This is

src/librustc/dep_graph/raii.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ pub struct DepTask<'graph> {
1919

2020
impl<'graph> DepTask<'graph> {
2121
pub fn new(data: &'graph DepGraphThreadData, key: DepNode<DefId>)
22-
-> DepTask<'graph> {
23-
data.enqueue(DepMessage::PushTask(key.clone()));
24-
DepTask { data: data, key: Some(key) }
22+
-> Option<DepTask<'graph>> {
23+
if data.is_enqueue_enabled() {
24+
data.enqueue(DepMessage::PushTask(key.clone()));
25+
Some(DepTask { data: data, key: Some(key) })
26+
} else {
27+
None
28+
}
2529
}
2630
}
2731

2832
impl<'graph> Drop for DepTask<'graph> {
2933
fn drop(&mut self) {
30-
self.data.enqueue(DepMessage::PopTask(self.key.take().unwrap()));
34+
if self.data.is_enqueue_enabled() {
35+
self.data.enqueue(DepMessage::PopTask(self.key.take().unwrap()));
36+
}
3137
}
3238
}
3339

@@ -36,15 +42,21 @@ pub struct IgnoreTask<'graph> {
3642
}
3743

3844
impl<'graph> IgnoreTask<'graph> {
39-
pub fn new(data: &'graph DepGraphThreadData) -> IgnoreTask<'graph> {
40-
data.enqueue(DepMessage::PushIgnore);
41-
IgnoreTask { data: data }
45+
pub fn new(data: &'graph DepGraphThreadData) -> Option<IgnoreTask<'graph>> {
46+
if data.is_enqueue_enabled() {
47+
data.enqueue(DepMessage::PushIgnore);
48+
Some(IgnoreTask { data: data })
49+
} else {
50+
None
51+
}
4252
}
4353
}
4454

4555
impl<'graph> Drop for IgnoreTask<'graph> {
4656
fn drop(&mut self) {
47-
self.data.enqueue(DepMessage::PopIgnore);
57+
if self.data.is_enqueue_enabled() {
58+
self.data.enqueue(DepMessage::PopIgnore);
59+
}
4860
}
4961
}
5062

src/librustc/dep_graph/shadow.rs

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ impl ShadowGraph {
6464
}
6565
}
6666

67+
#[inline]
68+
pub fn enabled(&self) -> bool {
69+
ENABLED
70+
}
71+
6772
pub fn enqueue(&self, message: &DepMessage) {
6873
if ENABLED {
6974
match self.stack.borrow_state() {

src/librustc/dep_graph/thread.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,24 @@ impl DepGraphThreadData {
8888
}
8989
}
9090

91+
/// True if we are actually building the full dep-graph.
9192
#[inline]
92-
pub fn enabled(&self) -> bool {
93+
pub fn is_fully_enabled(&self) -> bool {
9394
self.enabled
9495
}
9596

97+
/// True if (a) we are actually building the full dep-graph, or (b) we are
98+
/// only enqueuing messages in order to sanity-check them (which happens
99+
/// when debug assertions are enabled).
100+
#[inline]
101+
pub fn is_enqueue_enabled(&self) -> bool {
102+
self.is_fully_enabled() || self.shadow_graph.enabled()
103+
}
104+
96105
/// Sends the current batch of messages to the thread. Installs a
97106
/// new vector of messages.
98107
fn swap(&self) {
99-
assert!(self.enabled, "should never swap if not enabled");
108+
assert!(self.is_fully_enabled(), "should never swap if not fully enabled");
100109

101110
// should be a buffer waiting for us (though of course we may
102111
// have to wait for depgraph thread to finish processing the
@@ -112,7 +121,7 @@ impl DepGraphThreadData {
112121
}
113122

114123
pub fn query(&self) -> DepGraphQuery<DefId> {
115-
assert!(self.enabled, "cannot query if dep graph construction not enabled");
124+
assert!(self.is_fully_enabled(), "should never query if not fully enabled");
116125
self.enqueue(DepMessage::Query);
117126
self.swap();
118127
self.query_in.recv().unwrap()
@@ -122,9 +131,9 @@ impl DepGraphThreadData {
122131
/// the buffer is full, this may swap.)
123132
#[inline]
124133
pub fn enqueue(&self, message: DepMessage) {
134+
assert!(self.is_enqueue_enabled(), "should never enqueue if not enqueue-enabled");
125135
self.shadow_graph.enqueue(&message);
126-
127-
if self.enabled {
136+
if self.is_fully_enabled() {
128137
self.enqueue_enabled(message);
129138
}
130139
}

0 commit comments

Comments
 (0)