Skip to content

Commit

Permalink
Auto merge of #91043 - camsteffen:descendant-eq, r=petrochenkov
Browse files Browse the repository at this point in the history
Add fast path to `is_descendant_of`
  • Loading branch information
bors committed Nov 29, 2021
2 parents 9981e56 + ac8d514 commit 44723c5
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,15 @@ impl ExpnId {
HygieneData::with(|data| data.expn_data(self).clone())
}

#[inline]
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))
}

Expand Down Expand Up @@ -376,13 +384,22 @@ impl HygieneData {
}

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() {
return false;
}
expn_id = self.expn_data(expn_id).parent;
}
true
}

fn normalize_to_macros_2_0(&self, ctxt: SyntaxContext) -> SyntaxContext {
Expand Down Expand Up @@ -1223,6 +1240,7 @@ pub fn register_expn_id(
data: ExpnData,
hash: ExpnHash,
) -> ExpnId {
debug_assert!(data.parent == ExpnId::root() || krate == data.parent.krate);
let expn_id = ExpnId { krate, local_id };
HygieneData::with(|hygiene_data| {
let _old_data = hygiene_data.foreign_expn_data.insert(expn_id, data);
Expand Down

0 comments on commit 44723c5

Please sign in to comment.