From 768cfac705b9ce4a410531003811b6b8cfbfbaeb Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Fri, 19 Nov 2021 09:43:03 -0600 Subject: [PATCH 1/2] Add fast path to is_descendant_of --- compiler/rustc_span/src/hygiene.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index d590776676bef..23f5bfe59c5ef 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -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)) } @@ -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 { From ac8d514376c70cbb78e42354ddddc3f2534961ae Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Sun, 28 Nov 2021 16:35:31 -0600 Subject: [PATCH 2/2] Add parent crate assert to register_expn_id --- compiler/rustc_span/src/hygiene.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 23f5bfe59c5ef..315b706fbc44d 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -1240,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);