Skip to content

Commit 94c5d38

Browse files
committed
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%.
1 parent f76f6fb commit 94c5d38

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/librustc/middle/region.rs

+6-5
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)