Skip to content

Commit 768cfac

Browse files
committed
Add fast path to is_descendant_of
1 parent db9d361 commit 768cfac

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

compiler/rustc_span/src/hygiene.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,15 @@ impl ExpnId {
264264
HygieneData::with(|data| data.expn_data(self).clone())
265265
}
266266

267+
#[inline]
267268
pub fn is_descendant_of(self, ancestor: ExpnId) -> bool {
269+
// a few "fast path" cases to avoid locking HygieneData
270+
if ancestor == ExpnId::root() || ancestor == self {
271+
return true;
272+
}
273+
if ancestor.krate != self.krate {
274+
return false;
275+
}
268276
HygieneData::with(|data| data.is_descendant_of(self, ancestor))
269277
}
270278

@@ -376,13 +384,22 @@ impl HygieneData {
376384
}
377385

378386
fn is_descendant_of(&self, mut expn_id: ExpnId, ancestor: ExpnId) -> bool {
379-
while expn_id != ancestor {
387+
// a couple "fast path" cases to avoid traversing parents in the loop below
388+
if ancestor == ExpnId::root() {
389+
return true;
390+
}
391+
if expn_id.krate != ancestor.krate {
392+
return false;
393+
}
394+
loop {
395+
if expn_id == ancestor {
396+
return true;
397+
}
380398
if expn_id == ExpnId::root() {
381399
return false;
382400
}
383401
expn_id = self.expn_data(expn_id).parent;
384402
}
385-
true
386403
}
387404

388405
fn normalize_to_macros_2_0(&self, ctxt: SyntaxContext) -> SyntaxContext {

0 commit comments

Comments
 (0)