Skip to content

Commit 9707e10

Browse files
committed
Avoid the double lock around EncoderState
1 parent a2499bd commit 9707e10

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

compiler/rustc_query_system/src/dep_graph/graph.rs

+9-10
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::{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;
@@ -194,7 +193,7 @@ impl<D: Deps> DepGraph<D> {
194193

195194
pub fn with_query(&self, f: impl Fn(&DepGraphQuery)) {
196195
if let Some(data) = &self.data {
197-
data.current.encoder.borrow().with_query(f)
196+
data.current.encoder.with_query(f)
198197
}
199198
}
200199

@@ -954,15 +953,15 @@ impl<D: Deps> DepGraph<D> {
954953

955954
pub fn print_incremental_info(&self) {
956955
if let Some(data) = &self.data {
957-
data.current.encoder.borrow().print_incremental_info(
956+
data.current.encoder.print_incremental_info(
958957
data.current.total_read_count.load(Ordering::Relaxed),
959958
data.current.total_duplicate_read_count.load(Ordering::Relaxed),
960959
)
961960
}
962961
}
963962

964963
pub fn finish_encoding(&self) -> FileEncodeResult {
965-
if let Some(data) = &self.data { data.current.encoder.steal().finish() } else { Ok(0) }
964+
if let Some(data) = &self.data { data.current.encoder.finish() } else { Ok(0) }
966965
}
967966

968967
pub(crate) fn next_virtual_depnode_index(&self) -> DepNodeIndex {
@@ -1045,7 +1044,7 @@ rustc_index::newtype_index! {
10451044
/// manipulating both, we acquire `new_node_to_index` or `prev_index_to_index`
10461045
/// first, and `data` second.
10471046
pub(super) struct CurrentDepGraph<D: Deps> {
1048-
encoder: Steal<GraphEncoder<D>>,
1047+
encoder: GraphEncoder<D>,
10491048
new_node_to_index: Sharded<FxHashMap<DepNode, DepNodeIndex>>,
10501049
prev_index_to_index: Lock<IndexVec<SerializedDepNodeIndex, Option<DepNodeIndex>>>,
10511050

@@ -1111,13 +1110,13 @@ impl<D: Deps> CurrentDepGraph<D> {
11111110
let new_node_count_estimate = 102 * prev_graph_node_count / 100 + 200;
11121111

11131112
CurrentDepGraph {
1114-
encoder: Steal::new(GraphEncoder::new(
1113+
encoder: GraphEncoder::new(
11151114
encoder,
11161115
prev_graph_node_count,
11171116
record_graph,
11181117
record_stats,
11191118
profiler,
1120-
)),
1119+
),
11211120
new_node_to_index: Sharded::new(|| {
11221121
FxHashMap::with_capacity_and_hasher(
11231122
new_node_count_estimate / sharded::shards(),
@@ -1156,7 +1155,7 @@ impl<D: Deps> CurrentDepGraph<D> {
11561155
let dep_node_index = match self.new_node_to_index.lock_shard_by_value(&key).entry(key) {
11571156
Entry::Occupied(entry) => *entry.get(),
11581157
Entry::Vacant(entry) => {
1159-
let dep_node_index = self.encoder.borrow().send(key, current_fingerprint, edges);
1158+
let dep_node_index = self.encoder.send(key, current_fingerprint, edges);
11601159
entry.insert(dep_node_index);
11611160
dep_node_index
11621161
}
@@ -1182,7 +1181,7 @@ impl<D: Deps> CurrentDepGraph<D> {
11821181
let dep_node_index = match prev_index_to_index[prev_index] {
11831182
Some(dep_node_index) => dep_node_index,
11841183
None => {
1185-
let dep_node_index = self.encoder.borrow().send(key, fingerprint, edges);
1184+
let dep_node_index = self.encoder.send(key, fingerprint, edges);
11861185
prev_index_to_index[prev_index] = Some(dep_node_index);
11871186
dep_node_index
11881187
}
@@ -1243,7 +1242,7 @@ impl<D: Deps> CurrentDepGraph<D> {
12431242
.map(|i| prev_index_to_index[i].unwrap())
12441243
.collect();
12451244
let fingerprint = prev_graph.fingerprint_by_index(prev_index);
1246-
let dep_node_index = self.encoder.borrow().send(key, fingerprint, edges);
1245+
let dep_node_index = self.encoder.send(key, fingerprint, edges);
12471246
prev_index_to_index[prev_index] = Some(dep_node_index);
12481247
#[cfg(debug_assertions)]
12491248
self.record_edge(dep_node_index, key, fingerprint);

compiler/rustc_query_system/src/dep_graph/serialized.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ impl<D: Deps> EncoderState<D> {
505505

506506
pub struct GraphEncoder<D: Deps> {
507507
profiler: SelfProfilerRef,
508-
status: Lock<EncoderState<D>>,
508+
status: Lock<Option<EncoderState<D>>>,
509509
record_graph: Option<Lock<DepGraphQuery>>,
510510
}
511511

@@ -518,7 +518,7 @@ impl<D: Deps> GraphEncoder<D> {
518518
profiler: &SelfProfilerRef,
519519
) -> Self {
520520
let record_graph = record_graph.then(|| Lock::new(DepGraphQuery::new(prev_node_count)));
521-
let status = Lock::new(EncoderState::new(encoder, record_stats));
521+
let status = Lock::new(Some(EncoderState::new(encoder, record_stats)));
522522
GraphEncoder { status, record_graph, profiler: profiler.clone() }
523523
}
524524

@@ -533,7 +533,8 @@ impl<D: Deps> GraphEncoder<D> {
533533
total_read_count: u64,
534534
total_duplicate_read_count: u64,
535535
) {
536-
let status = self.status.lock();
536+
let mut status = self.status.lock();
537+
let status = status.as_mut().unwrap();
537538
if let Some(record_stats) = &status.stats {
538539
let mut stats: Vec<_> = record_stats.values().collect();
539540
stats.sort_by_key(|s| -(s.node_counter as i64));
@@ -588,11 +589,12 @@ impl<D: Deps> GraphEncoder<D> {
588589
) -> DepNodeIndex {
589590
let _prof_timer = self.profiler.generic_activity("incr_comp_encode_dep_graph");
590591
let node = NodeInfo { node, fingerprint, edges };
591-
self.status.lock().encode_node(&node, &self.record_graph)
592+
self.status.lock().as_mut().unwrap().encode_node(&node, &self.record_graph)
592593
}
593594

594-
pub fn finish(self) -> FileEncodeResult {
595-
let _prof_timer = self.profiler.generic_activity("incr_comp_encode_dep_graph");
596-
self.status.into_inner().finish(&self.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)
597599
}
598600
}

0 commit comments

Comments
 (0)