Skip to content

Commit 68e9415

Browse files
committed
Reduce lock depth.
1 parent 07ee56e commit 68e9415

File tree

2 files changed

+38
-41
lines changed

2 files changed

+38
-41
lines changed

compiler/rustc_query_system/src/dep_graph/graph.rs

+27-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
33
use rustc_data_structures::profiling::{EventId, 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;
@@ -195,8 +194,11 @@ impl DepGraph {
195194
}
196195

197196
pub fn with_query(&self, f: impl Fn(&DepGraphQuery)) {
198-
if let Some(data) = &self.data {
199-
data.current.encoder.borrow().with_query(f)
197+
if let Some(data) = &self.data
198+
&& let Some(record_graph) = &data.current.record_graph
199+
{
200+
let record_graph = record_graph.lock();
201+
f(&*record_graph)
200202
}
201203
}
202204

@@ -984,7 +986,7 @@ impl DepGraph {
984986

985987
pub fn finish_encoding(&self, profiler: &SelfProfilerRef) -> FileEncodeResult {
986988
if let Some(data) = &self.data {
987-
data.current.encoder.steal().finish(profiler)
989+
data.current.encoder.lock().finish(profiler)
988990
} else {
989991
Ok(0)
990992
}
@@ -1070,7 +1072,8 @@ rustc_index::newtype_index! {
10701072
/// manipulating both, we acquire `new_node_to_index` or `prev_index_to_index`
10711073
/// first, and `data` second.
10721074
pub(super) struct CurrentDepGraph {
1073-
encoder: Steal<GraphEncoder>,
1075+
encoder: Lock<GraphEncoder>,
1076+
record_graph: Option<Lock<DepGraphQuery>>,
10741077
new_node_to_index: Sharded<FxHashMap<DepNode, DepNodeIndex>>,
10751078
prev_index_to_index: Lock<IndexVec<SerializedDepNodeIndex, Option<DepNodeIndex>>>,
10761079

@@ -1146,12 +1149,9 @@ impl CurrentDepGraph {
11461149
.map(EventId::from_label);
11471150

11481151
CurrentDepGraph {
1149-
encoder: Steal::new(GraphEncoder::new::<D>(
1150-
encoder,
1151-
prev_graph_node_count,
1152-
record_graph,
1153-
record_stats,
1154-
)),
1152+
encoder: Lock::new(GraphEncoder::new::<D>(encoder, record_stats)),
1153+
record_graph: record_graph
1154+
.then(|| Lock::new(DepGraphQuery::new(prev_graph_node_count))),
11551155
new_node_to_index: Sharded::new(|| {
11561156
FxHashMap::with_capacity_and_hasher(
11571157
new_node_count_estimate / sharded::shards(),
@@ -1192,8 +1192,13 @@ impl CurrentDepGraph {
11921192
let dep_node_index = match self.new_node_to_index.lock_shard_by_value(&key).entry(key) {
11931193
Entry::Occupied(entry) => *entry.get(),
11941194
Entry::Vacant(entry) => {
1195-
let dep_node_index =
1196-
self.encoder.borrow().send(profiler, key, current_fingerprint, edges);
1195+
let dep_node_index = self.encoder.lock().send(
1196+
profiler,
1197+
key,
1198+
current_fingerprint,
1199+
edges,
1200+
&self.record_graph,
1201+
);
11971202
entry.insert(dep_node_index);
11981203
dep_node_index
11991204
}
@@ -1224,8 +1229,13 @@ impl CurrentDepGraph {
12241229
let dep_node_index = match prev_index_to_index[prev_index] {
12251230
Some(dep_node_index) => dep_node_index,
12261231
None => {
1227-
let dep_node_index =
1228-
self.encoder.borrow().send(profiler, key, fingerprint, edges);
1232+
let dep_node_index = self.encoder.lock().send(
1233+
profiler,
1234+
key,
1235+
fingerprint,
1236+
edges,
1237+
&self.record_graph,
1238+
);
12291239
prev_index_to_index[prev_index] = Some(dep_node_index);
12301240
dep_node_index
12311241
}
@@ -1287,7 +1297,8 @@ impl CurrentDepGraph {
12871297
.map(|i| prev_index_to_index[i].unwrap())
12881298
.collect();
12891299
let fingerprint = prev_graph.fingerprint_by_index(prev_index);
1290-
let dep_node_index = self.encoder.borrow().send(profiler, key, fingerprint, edges);
1300+
let dep_node_index =
1301+
self.encoder.lock().send(profiler, key, fingerprint, edges, &self.record_graph);
12911302
prev_index_to_index[prev_index] = Some(dep_node_index);
12921303
#[cfg(debug_assertions)]
12931304
self.record_edge(dep_node_index, key, fingerprint);

compiler/rustc_query_system/src/dep_graph/serialized.rs

+11-25
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,9 @@ impl<D: Deps> NodeEncoder for EncoderState<D> {
482482
let edge_count = node.edges.len();
483483
self.total_edge_count += edge_count;
484484

485-
if let Some(record_graph) = &record_graph {
485+
if let Some(record_graph) = record_graph {
486486
// Do not ICE when a query is called from within `with_query`.
487-
if let Some(record_graph) = &mut record_graph.try_lock() {
487+
if let Some(mut record_graph) = record_graph.try_lock() {
488488
record_graph.push(index, node.node, &node.edges);
489489
}
490490
}
@@ -537,36 +537,21 @@ impl<D: Deps> NodeEncoder for EncoderState<D> {
537537
}
538538

539539
pub struct GraphEncoder {
540-
status: Lock<Box<dyn NodeEncoder>>,
541-
record_graph: Option<Lock<DepGraphQuery>>,
540+
status: Box<dyn NodeEncoder>,
542541
}
543542

544543
impl GraphEncoder {
545-
pub fn new<D: Deps>(
546-
encoder: FileEncoder,
547-
prev_node_count: usize,
548-
record_graph: bool,
549-
record_stats: bool,
550-
) -> Self {
551-
let record_graph = record_graph.then(|| Lock::new(DepGraphQuery::new(prev_node_count)));
544+
pub fn new<D: Deps>(encoder: FileEncoder, record_stats: bool) -> Self {
552545
let status = Box::new(EncoderState::<D>::new(encoder, record_stats)) as _;
553-
let status = Lock::new(status);
554-
GraphEncoder { status, record_graph }
555-
}
556-
557-
pub(crate) fn with_query(&self, f: impl Fn(&DepGraphQuery)) {
558-
if let Some(record_graph) = &self.record_graph {
559-
f(&record_graph.lock())
560-
}
546+
GraphEncoder { status }
561547
}
562548

563549
pub(crate) fn print_incremental_info(
564550
&self,
565551
total_read_count: u64,
566552
total_duplicate_read_count: u64,
567553
) {
568-
let status = self.status.lock();
569-
if let Some((total_node_count, total_edge_count, record_stats)) = status.get_stats() {
554+
if let Some((total_node_count, total_edge_count, record_stats)) = self.status.get_stats() {
570555
let mut stats: Vec<_> = record_stats.values().collect();
571556
stats.sort_by_key(|s| -(s.node_counter as i64));
572557

@@ -613,19 +598,20 @@ impl GraphEncoder {
613598
}
614599

615600
pub(crate) fn send(
616-
&self,
601+
&mut self,
617602
profiler: &SelfProfilerRef,
618603
node: DepNode,
619604
fingerprint: Fingerprint,
620605
edges: EdgesVec,
606+
record_graph: &Option<Lock<DepGraphQuery>>,
621607
) -> DepNodeIndex {
622608
let _prof_timer = profiler.generic_activity("incr_comp_encode_dep_graph");
623609
let node = NodeInfo { node, fingerprint, edges };
624-
self.status.lock().encode_node(&node, &self.record_graph)
610+
self.status.encode_node(&node, record_graph)
625611
}
626612

627-
pub fn finish(self, profiler: &SelfProfilerRef) -> FileEncodeResult {
613+
pub fn finish(&mut self, profiler: &SelfProfilerRef) -> FileEncodeResult {
628614
let _prof_timer = profiler.generic_activity("incr_comp_encode_dep_graph");
629-
self.status.into_inner().finish(profiler)
615+
self.status.finish(profiler)
630616
}
631617
}

0 commit comments

Comments
 (0)