Skip to content

Commit 094a620

Browse files
committed
Auto merge of rust-lang#122064 - Zoxc:dep-graph-encode-tweaks, r=cjgillot
Dep node encoding cleanups This does some cleanups around dep node encoding. Performance change with `-Zthreads=2`: <table><tr><td rowspan="2">Benchmark</td><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th></tr><tr><td align="right">Time</td><td align="right">Time</td><td align="right">%</th><td align="right">Memory</td><td align="right">Memory</td><td align="right">%</th></tr><tr><td>🟣 <b>clap</b>:check:unchanged</td><td align="right">0.4337s</td><td align="right">0.4306s</td><td align="right"> -0.72%</td><td align="right">88.90 MiB</td><td align="right">89.04 MiB</td><td align="right"> 0.15%</td></tr><tr><td>🟣 <b>hyper</b>:check:unchanged</td><td align="right">0.1541s</td><td align="right">0.1528s</td><td align="right"> -0.86%</td><td align="right">51.99 MiB</td><td align="right">52.03 MiB</td><td align="right"> 0.07%</td></tr><tr><td>🟣 <b>regex</b>:check:unchanged</td><td align="right">0.3286s</td><td align="right">0.3248s</td><td align="right">💚 -1.15%</td><td align="right">71.89 MiB</td><td align="right">71.74 MiB</td><td align="right"> -0.21%</td></tr><tr><td>🟣 <b>syn</b>:check:unchanged</td><td align="right">0.6118s</td><td align="right">0.6057s</td><td align="right">💚 -1.01%</td><td align="right">106.59 MiB</td><td align="right">106.66 MiB</td><td align="right"> 0.06%</td></tr><tr><td>🟣 <b>syntex_syntax</b>:check:unchanged</td><td align="right">1.4570s</td><td align="right">1.4463s</td><td align="right"> -0.74%</td><td align="right">197.29 MiB</td><td align="right">197.33 MiB</td><td align="right"> 0.02%</td></tr><tr><td>Total</td><td align="right">2.9852s</td><td align="right">2.9601s</td><td align="right"> -0.84%</td><td align="right">516.66 MiB</td><td align="right">516.80 MiB</td><td align="right"> 0.03%</td></tr><tr><td>Summary</td><td align="right">1.0000s</td><td align="right">0.9911s</td><td align="right"> -0.89%</td><td align="right">1 byte</td><td align="right">1.00 bytes</td><td align="right"> 0.02%</td></tr></table> r? `@cjgillot`
2 parents 5bc7b9a + 9707e10 commit 094a620

File tree

3 files changed

+34
-75
lines changed

3 files changed

+34
-75
lines changed

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ impl<'tcx> GlobalCtxt<'tcx> {
709709
}
710710

711711
pub fn finish(&self) -> FileEncodeResult {
712-
self.dep_graph.finish_encoding(&self.sess.prof)
712+
self.dep_graph.finish_encoding()
713713
}
714714
}
715715

compiler/rustc_query_system/src/dep_graph/graph.rs

+20-64
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use rustc_data_structures::fingerprint::Fingerprint;
22
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3-
use rustc_data_structures::profiling::{EventId, QueryInvocationId, SelfProfilerRef};
3+
use rustc_data_structures::profiling::{QueryInvocationId, SelfProfilerRef};
44
use rustc_data_structures::sharded::{self, Sharded};
55
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
6-
use rustc_data_structures::steal::Steal;
76
use rustc_data_structures::sync::{AtomicU32, AtomicU64, Lock, Lrc};
87
use rustc_data_structures::unord::UnordMap;
98
use rustc_index::IndexVec;
@@ -134,7 +133,6 @@ impl<D: Deps> DepGraph<D> {
134133

135134
// Instantiate a dependy-less node only once for anonymous queries.
136135
let _green_node_index = current.intern_new_node(
137-
profiler,
138136
DepNode { kind: D::DEP_KIND_NULL, hash: current.anon_id_seed.into() },
139137
EdgesVec::new(),
140138
Fingerprint::ZERO,
@@ -143,7 +141,6 @@ impl<D: Deps> DepGraph<D> {
143141

144142
// Instantiate a dependy-less red node only once for anonymous queries.
145143
let (red_node_index, red_node_prev_index_and_color) = current.intern_node(
146-
profiler,
147144
&prev_graph,
148145
DepNode { kind: D::DEP_KIND_RED, hash: Fingerprint::ZERO.into() },
149146
EdgesVec::new(),
@@ -196,7 +193,7 @@ impl<D: Deps> DepGraph<D> {
196193

197194
pub fn with_query(&self, f: impl Fn(&DepGraphQuery)) {
198195
if let Some(data) = &self.data {
199-
data.current.encoder.borrow().with_query(f)
196+
data.current.encoder.with_query(f)
200197
}
201198
}
202199

@@ -372,13 +369,8 @@ impl<D: Deps> DepGraphData<D> {
372369
hash_result.map(|f| dcx.with_stable_hashing_context(|mut hcx| f(&mut hcx, &result)));
373370

374371
// Intern the new `DepNode`.
375-
let (dep_node_index, prev_and_color) = self.current.intern_node(
376-
dcx.profiler(),
377-
&self.previous,
378-
key,
379-
edges,
380-
current_fingerprint,
381-
);
372+
let (dep_node_index, prev_and_color) =
373+
self.current.intern_node(&self.previous, key, edges, current_fingerprint);
382374

383375
hashing_timer.finish_with_query_invocation_id(dep_node_index.into());
384376

@@ -443,12 +435,7 @@ impl<D: Deps> DepGraphData<D> {
443435
hash: self.current.anon_id_seed.combine(hasher.finish()).into(),
444436
};
445437

446-
self.current.intern_new_node(
447-
cx.profiler(),
448-
target_dep_node,
449-
task_deps,
450-
Fingerprint::ZERO,
451-
)
438+
self.current.intern_new_node(target_dep_node, task_deps, Fingerprint::ZERO)
452439
}
453440
};
454441

@@ -585,13 +572,8 @@ impl<D: Deps> DepGraph<D> {
585572
});
586573

587574
// Intern the new `DepNode` with the dependencies up-to-now.
588-
let (dep_node_index, prev_and_color) = data.current.intern_node(
589-
cx.profiler(),
590-
&data.previous,
591-
node,
592-
edges,
593-
current_fingerprint,
594-
);
575+
let (dep_node_index, prev_and_color) =
576+
data.current.intern_node(&data.previous, node, edges, current_fingerprint);
595577

596578
hashing_timer.finish_with_query_invocation_id(dep_node_index.into());
597579

@@ -871,11 +853,8 @@ impl<D: Deps> DepGraphData<D> {
871853

872854
// We allocating an entry for the node in the current dependency graph and
873855
// adding all the appropriate edges imported from the previous graph
874-
let dep_node_index = self.current.promote_node_and_deps_to_current(
875-
qcx.dep_context().profiler(),
876-
&self.previous,
877-
prev_dep_node_index,
878-
);
856+
let dep_node_index =
857+
self.current.promote_node_and_deps_to_current(&self.previous, prev_dep_node_index);
879858

880859
// ... emitting any stored diagnostic ...
881860

@@ -974,19 +953,15 @@ impl<D: Deps> DepGraph<D> {
974953

975954
pub fn print_incremental_info(&self) {
976955
if let Some(data) = &self.data {
977-
data.current.encoder.borrow().print_incremental_info(
956+
data.current.encoder.print_incremental_info(
978957
data.current.total_read_count.load(Ordering::Relaxed),
979958
data.current.total_duplicate_read_count.load(Ordering::Relaxed),
980959
)
981960
}
982961
}
983962

984-
pub fn finish_encoding(&self, profiler: &SelfProfilerRef) -> FileEncodeResult {
985-
if let Some(data) = &self.data {
986-
data.current.encoder.steal().finish(profiler)
987-
} else {
988-
Ok(0)
989-
}
963+
pub fn finish_encoding(&self) -> FileEncodeResult {
964+
if let Some(data) = &self.data { data.current.encoder.finish() } else { Ok(0) }
990965
}
991966

992967
pub(crate) fn next_virtual_depnode_index(&self) -> DepNodeIndex {
@@ -1069,7 +1044,7 @@ rustc_index::newtype_index! {
10691044
/// manipulating both, we acquire `new_node_to_index` or `prev_index_to_index`
10701045
/// first, and `data` second.
10711046
pub(super) struct CurrentDepGraph<D: Deps> {
1072-
encoder: Steal<GraphEncoder<D>>,
1047+
encoder: GraphEncoder<D>,
10731048
new_node_to_index: Sharded<FxHashMap<DepNode, DepNodeIndex>>,
10741049
prev_index_to_index: Lock<IndexVec<SerializedDepNodeIndex, Option<DepNodeIndex>>>,
10751050

@@ -1100,12 +1075,6 @@ pub(super) struct CurrentDepGraph<D: Deps> {
11001075
/// debugging and only active with `debug_assertions`.
11011076
total_read_count: AtomicU64,
11021077
total_duplicate_read_count: AtomicU64,
1103-
1104-
/// The cached event id for profiling node interning. This saves us
1105-
/// from having to look up the event id every time we intern a node
1106-
/// which may incur too much overhead.
1107-
/// This will be None if self-profiling is disabled.
1108-
node_intern_event_id: Option<EventId>,
11091078
}
11101079

11111080
impl<D: Deps> CurrentDepGraph<D> {
@@ -1140,17 +1109,14 @@ impl<D: Deps> CurrentDepGraph<D> {
11401109

11411110
let new_node_count_estimate = 102 * prev_graph_node_count / 100 + 200;
11421111

1143-
let node_intern_event_id = profiler
1144-
.get_or_alloc_cached_string("incr_comp_intern_dep_graph_node")
1145-
.map(EventId::from_label);
1146-
11471112
CurrentDepGraph {
1148-
encoder: Steal::new(GraphEncoder::new(
1113+
encoder: GraphEncoder::new(
11491114
encoder,
11501115
prev_graph_node_count,
11511116
record_graph,
11521117
record_stats,
1153-
)),
1118+
profiler,
1119+
),
11541120
new_node_to_index: Sharded::new(|| {
11551121
FxHashMap::with_capacity_and_hasher(
11561122
new_node_count_estimate / sharded::shards(),
@@ -1165,7 +1131,6 @@ impl<D: Deps> CurrentDepGraph<D> {
11651131
fingerprints: Lock::new(IndexVec::from_elem_n(None, new_node_count_estimate)),
11661132
total_read_count: AtomicU64::new(0),
11671133
total_duplicate_read_count: AtomicU64::new(0),
1168-
node_intern_event_id,
11691134
}
11701135
}
11711136

@@ -1183,16 +1148,14 @@ impl<D: Deps> CurrentDepGraph<D> {
11831148
#[inline(always)]
11841149
fn intern_new_node(
11851150
&self,
1186-
profiler: &SelfProfilerRef,
11871151
key: DepNode,
11881152
edges: EdgesVec,
11891153
current_fingerprint: Fingerprint,
11901154
) -> DepNodeIndex {
11911155
let dep_node_index = match self.new_node_to_index.lock_shard_by_value(&key).entry(key) {
11921156
Entry::Occupied(entry) => *entry.get(),
11931157
Entry::Vacant(entry) => {
1194-
let dep_node_index =
1195-
self.encoder.borrow().send(profiler, key, current_fingerprint, edges);
1158+
let dep_node_index = self.encoder.send(key, current_fingerprint, edges);
11961159
entry.insert(dep_node_index);
11971160
dep_node_index
11981161
}
@@ -1206,25 +1169,19 @@ impl<D: Deps> CurrentDepGraph<D> {
12061169

12071170
fn intern_node(
12081171
&self,
1209-
profiler: &SelfProfilerRef,
12101172
prev_graph: &SerializedDepGraph,
12111173
key: DepNode,
12121174
edges: EdgesVec,
12131175
fingerprint: Option<Fingerprint>,
12141176
) -> (DepNodeIndex, Option<(SerializedDepNodeIndex, DepNodeColor)>) {
1215-
// Get timer for profiling `DepNode` interning
1216-
let _node_intern_timer =
1217-
self.node_intern_event_id.map(|eid| profiler.generic_activity_with_event_id(eid));
1218-
12191177
if let Some(prev_index) = prev_graph.node_to_index_opt(&key) {
12201178
let get_dep_node_index = |fingerprint| {
12211179
let mut prev_index_to_index = self.prev_index_to_index.lock();
12221180

12231181
let dep_node_index = match prev_index_to_index[prev_index] {
12241182
Some(dep_node_index) => dep_node_index,
12251183
None => {
1226-
let dep_node_index =
1227-
self.encoder.borrow().send(profiler, key, fingerprint, edges);
1184+
let dep_node_index = self.encoder.send(key, fingerprint, edges);
12281185
prev_index_to_index[prev_index] = Some(dep_node_index);
12291186
dep_node_index
12301187
}
@@ -1261,15 +1218,14 @@ impl<D: Deps> CurrentDepGraph<D> {
12611218
let fingerprint = fingerprint.unwrap_or(Fingerprint::ZERO);
12621219

12631220
// This is a new node: it didn't exist in the previous compilation session.
1264-
let dep_node_index = self.intern_new_node(profiler, key, edges, fingerprint);
1221+
let dep_node_index = self.intern_new_node(key, edges, fingerprint);
12651222

12661223
(dep_node_index, None)
12671224
}
12681225
}
12691226

12701227
fn promote_node_and_deps_to_current(
12711228
&self,
1272-
profiler: &SelfProfilerRef,
12731229
prev_graph: &SerializedDepGraph,
12741230
prev_index: SerializedDepNodeIndex,
12751231
) -> DepNodeIndex {
@@ -1286,7 +1242,7 @@ impl<D: Deps> CurrentDepGraph<D> {
12861242
.map(|i| prev_index_to_index[i].unwrap())
12871243
.collect();
12881244
let fingerprint = prev_graph.fingerprint_by_index(prev_index);
1289-
let dep_node_index = self.encoder.borrow().send(profiler, key, fingerprint, edges);
1245+
let dep_node_index = self.encoder.send(key, fingerprint, edges);
12901246
prev_index_to_index[prev_index] = Some(dep_node_index);
12911247
#[cfg(debug_assertions)]
12921248
self.record_edge(dep_node_index, key, fingerprint);

compiler/rustc_query_system/src/dep_graph/serialized.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,8 @@ impl<D: Deps> EncoderState<D> {
504504
}
505505

506506
pub struct GraphEncoder<D: Deps> {
507-
status: Lock<EncoderState<D>>,
507+
profiler: SelfProfilerRef,
508+
status: Lock<Option<EncoderState<D>>>,
508509
record_graph: Option<Lock<DepGraphQuery>>,
509510
}
510511

@@ -514,10 +515,11 @@ impl<D: Deps> GraphEncoder<D> {
514515
prev_node_count: usize,
515516
record_graph: bool,
516517
record_stats: bool,
518+
profiler: &SelfProfilerRef,
517519
) -> Self {
518520
let record_graph = record_graph.then(|| Lock::new(DepGraphQuery::new(prev_node_count)));
519-
let status = Lock::new(EncoderState::new(encoder, record_stats));
520-
GraphEncoder { status, record_graph }
521+
let status = Lock::new(Some(EncoderState::new(encoder, record_stats)));
522+
GraphEncoder { status, record_graph, profiler: profiler.clone() }
521523
}
522524

523525
pub(crate) fn with_query(&self, f: impl Fn(&DepGraphQuery)) {
@@ -531,7 +533,8 @@ impl<D: Deps> GraphEncoder<D> {
531533
total_read_count: u64,
532534
total_duplicate_read_count: u64,
533535
) {
534-
let status = self.status.lock();
536+
let mut status = self.status.lock();
537+
let status = status.as_mut().unwrap();
535538
if let Some(record_stats) = &status.stats {
536539
let mut stats: Vec<_> = record_stats.values().collect();
537540
stats.sort_by_key(|s| -(s.node_counter as i64));
@@ -580,18 +583,18 @@ impl<D: Deps> GraphEncoder<D> {
580583

581584
pub(crate) fn send(
582585
&self,
583-
profiler: &SelfProfilerRef,
584586
node: DepNode,
585587
fingerprint: Fingerprint,
586588
edges: EdgesVec,
587589
) -> DepNodeIndex {
588-
let _prof_timer = profiler.generic_activity("incr_comp_encode_dep_graph");
590+
let _prof_timer = self.profiler.generic_activity("incr_comp_encode_dep_graph");
589591
let node = NodeInfo { node, fingerprint, edges };
590-
self.status.lock().encode_node(&node, &self.record_graph)
592+
self.status.lock().as_mut().unwrap().encode_node(&node, &self.record_graph)
591593
}
592594

593-
pub fn finish(self, profiler: &SelfProfilerRef) -> FileEncodeResult {
594-
let _prof_timer = profiler.generic_activity("incr_comp_encode_dep_graph");
595-
self.status.into_inner().finish(profiler)
595+
pub fn finish(&self) -> FileEncodeResult {
596+
let _prof_timer = self.profiler.generic_activity("incr_comp_encode_dep_graph_finish");
597+
598+
self.status.lock().take().unwrap().finish(&self.profiler)
596599
}
597600
}

0 commit comments

Comments
 (0)