Skip to content

Commit 44723c5

Browse files
committed
Auto merge of #91043 - camsteffen:descendant-eq, r=petrochenkov
Add fast path to `is_descendant_of`
2 parents 9981e56 + ac8d514 commit 44723c5

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

compiler/rustc_span/src/hygiene.rs

+20-2
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 {
@@ -1223,6 +1240,7 @@ pub fn register_expn_id(
12231240
data: ExpnData,
12241241
hash: ExpnHash,
12251242
) -> ExpnId {
1243+
debug_assert!(data.parent == ExpnId::root() || krate == data.parent.krate);
12261244
let expn_id = ExpnId { krate, local_id };
12271245
HygieneData::with(|hygiene_data| {
12281246
let _old_data = hygiene_data.foreign_expn_data.insert(expn_id, data);

0 commit comments

Comments
 (0)