Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fast path to is_descendant_of #91043

Merged
merged 2 commits into from
Nov 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
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 {
camsteffen marked this conversation as resolved.
Show resolved Hide resolved
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a regular assert?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine as is, the assert will be removed anyway when root expansions start keeping their crate IDs.

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