Add fast path to is_descendant_of
This commit is contained in:
parent
db9d361a47
commit
768cfac705
1 changed files with 19 additions and 2 deletions
|
@ -264,7 +264,15 @@ impl ExpnId {
|
||||||
HygieneData::with(|data| data.expn_data(self).clone())
|
HygieneData::with(|data| data.expn_data(self).clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn is_descendant_of(self, ancestor: ExpnId) -> bool {
|
pub fn is_descendant_of(self, ancestor: ExpnId) -> bool {
|
||||||
|
// a few "fast path" cases to avoid locking HygieneData
|
||||||
|
if ancestor == ExpnId::root() || ancestor == self {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ancestor.krate != self.krate {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
HygieneData::with(|data| data.is_descendant_of(self, ancestor))
|
HygieneData::with(|data| data.is_descendant_of(self, ancestor))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -376,13 +384,22 @@ impl HygieneData {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_descendant_of(&self, mut expn_id: ExpnId, ancestor: ExpnId) -> bool {
|
fn is_descendant_of(&self, mut expn_id: ExpnId, ancestor: ExpnId) -> bool {
|
||||||
while expn_id != ancestor {
|
// a couple "fast path" cases to avoid traversing parents in the loop below
|
||||||
|
if ancestor == ExpnId::root() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if expn_id.krate != ancestor.krate {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
if expn_id == ancestor {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if expn_id == ExpnId::root() {
|
if expn_id == ExpnId::root() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
expn_id = self.expn_data(expn_id).parent;
|
expn_id = self.expn_data(expn_id).parent;
|
||||||
}
|
}
|
||||||
true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn normalize_to_macros_2_0(&self, ctxt: SyntaxContext) -> SyntaxContext {
|
fn normalize_to_macros_2_0(&self, ctxt: SyntaxContext) -> SyntaxContext {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue