Skip to content

Commit 27c4335

Browse files
committed
Auto merge of #56983 - ljedrz:parallel_query_tweaks, r=Zoxc
Parallel query tweaks - faster stack reversal in `remove_cycle` - insert visited queries more eagerly - simplify stack trimming in `cycle_check` - minor refactoring in 2 spots
2 parents 73dcb52 + dfe187d commit 27c4335

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

src/librustc/ty/query/job.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,12 @@ fn cycle_check<'tcx>(query: Lrc<QueryJob<'tcx>>,
303303
stack: &mut Vec<(Span, Lrc<QueryJob<'tcx>>)>,
304304
visited: &mut FxHashSet<*const QueryJob<'tcx>>
305305
) -> Option<Option<Waiter<'tcx>>> {
306-
if visited.contains(&query.as_ptr()) {
306+
if !visited.insert(query.as_ptr()) {
307307
return if let Some(p) = stack.iter().position(|q| q.1.as_ptr() == query.as_ptr()) {
308308
// We detected a query cycle, fix up the initial span and return Some
309309

310310
// Remove previous stack entries
311-
stack.splice(0..p, iter::empty());
311+
stack.drain(0..p);
312312
// Replace the span for the first query with the cycle cause
313313
stack[0].0 = span;
314314
Some(None)
@@ -317,8 +317,7 @@ fn cycle_check<'tcx>(query: Lrc<QueryJob<'tcx>>,
317317
}
318318
}
319319

320-
// Mark this query is visited and add it to the stack
321-
visited.insert(query.as_ptr());
320+
// Query marked as visited is added it to the stack
322321
stack.push((span, query.clone()));
323322

324323
// Visit all the waiters
@@ -343,7 +342,7 @@ fn connected_to_root<'tcx>(
343342
visited: &mut FxHashSet<*const QueryJob<'tcx>>
344343
) -> bool {
345344
// We already visited this or we're deliberately ignoring it
346-
if visited.contains(&query.as_ptr()) {
345+
if !visited.insert(query.as_ptr()) {
347346
return false;
348347
}
349348

@@ -352,8 +351,6 @@ fn connected_to_root<'tcx>(
352351
return true;
353352
}
354353

355-
visited.insert(query.as_ptr());
356-
357354
visit_waiters(query, |_, successor| {
358355
if connected_to_root(successor, visited) {
359356
Some(None)
@@ -403,11 +400,9 @@ fn remove_cycle<'tcx>(
403400
DUMMY_SP,
404401
&mut stack,
405402
&mut visited) {
406-
// Reverse the stack so earlier entries require later entries
407-
stack.reverse();
408-
409-
// The stack is a vector of pairs of spans and queries
410-
let (mut spans, queries): (Vec<_>, Vec<_>) = stack.into_iter().unzip();
403+
// The stack is a vector of pairs of spans and queries; reverse it so that
404+
// the earlier entries require later entries
405+
let (mut spans, queries): (Vec<_>, Vec<_>) = stack.into_iter().rev().unzip();
411406

412407
// Shift the spans so that queries are matched with the span for their waitee
413408
spans.rotate_right(1);
@@ -424,7 +419,7 @@ fn remove_cycle<'tcx>(
424419

425420
// Find the queries in the cycle which are
426421
// connected to queries outside the cycle
427-
let entry_points: Vec<_> = stack.iter().filter_map(|(span, query)| {
422+
let entry_points = stack.iter().filter_map(|(span, query)| {
428423
if query.parent.is_none() {
429424
// This query is connected to the root (it has no query parent)
430425
Some((*span, query.clone(), None))
@@ -449,10 +444,7 @@ fn remove_cycle<'tcx>(
449444
Some((*span, query.clone(), Some(waiter)))
450445
}
451446
}
452-
}).collect();
453-
454-
let entry_points: Vec<(Span, Lrc<QueryJob<'tcx>>, Option<(Span, Lrc<QueryJob<'tcx>>)>)>
455-
= entry_points;
447+
}).collect::<Vec<(Span, Lrc<QueryJob<'tcx>>, Option<(Span, Lrc<QueryJob<'tcx>>)>)>>();
456448

457449
// Deterministically pick an entry point
458450
let (_, entry_point, usage) = pick_query(tcx, &entry_points, |e| (e.0, e.1.clone()));

src/librustc/ty/query/on_disk_cache.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,7 @@ impl<'sess> OnDiskCache<'sess> {
398398
-> Option<T>
399399
where T: Decodable
400400
{
401-
let pos = if let Some(&pos) = index.get(&dep_node_index) {
402-
pos
403-
} else {
404-
return None
405-
};
401+
let pos = index.get(&dep_node_index).cloned()?;
406402

407403
// Initialize the cnum_map using the value from the thread which finishes the closure first
408404
self.cnum_map.init_nonlocking_same(|| {

0 commit comments

Comments
 (0)