Skip to content

Commit fd4bf23

Browse files
committed
Rollup merge of rust-lang#50365 - nnethercote:nearest_common_ancestor-two-vecs, r=nikomatsakis
Use two vectors in nearest_common_ancestor. When looking at any scope in scope chain A, we only need to look for matches among scopes previously seen in scope chain B, and vice versa. This halves the number of "seen before?" comparisons, speeding up some runs of style-servo, clap-rs, and syn by 1--2%. Thanks to @kirillkh for the suggestion. r? @nikomatsakis
2 parents 06a47dc + 94c5d38 commit fd4bf23

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/librustc/middle/region.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -690,21 +690,22 @@ impl<'tcx> ScopeTree {
690690
// the start. So this algorithm is faster.
691691
let mut ma = Some(scope_a);
692692
let mut mb = Some(scope_b);
693-
let mut seen: SmallVec<[Scope; 32]> = SmallVec::new();
693+
let mut seen_a: SmallVec<[Scope; 32]> = SmallVec::new();
694+
let mut seen_b: SmallVec<[Scope; 32]> = SmallVec::new();
694695
loop {
695696
if let Some(a) = ma {
696-
if seen.iter().position(|s| *s == a).is_some() {
697+
if seen_b.iter().position(|s| *s == a).is_some() {
697698
return a;
698699
}
699-
seen.push(a);
700+
seen_a.push(a);
700701
ma = self.parent_map.get(&a).map(|s| *s);
701702
}
702703

703704
if let Some(b) = mb {
704-
if seen.iter().position(|s| *s == b).is_some() {
705+
if seen_a.iter().position(|s| *s == b).is_some() {
705706
return b;
706707
}
707-
seen.push(b);
708+
seen_b.push(b);
708709
mb = self.parent_map.get(&b).map(|s| *s);
709710
}
710711

0 commit comments

Comments
 (0)