From 637a93cb5d2ac75ce683d71bf83695907abc323a Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 3 May 2022 12:40:35 +1000 Subject: [PATCH 1/3] Speed up `Token::{ident,lifetime}`. They're hot enough that the repeated matching done by `uninterpolate` has a measurable effect. --- compiler/rustc_ast/src/token.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index 1589a882f0892..4eb494aeb9b5a 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -475,19 +475,29 @@ impl Token { } /// Returns an identifier if this token is an identifier. + #[inline] pub fn ident(&self) -> Option<(Ident, /* is_raw */ bool)> { - let token = self.uninterpolate(); - match token.kind { - Ident(name, is_raw) => Some((Ident::new(name, token.span), is_raw)), + // We avoid using `Token::uninterpolate` here because it's slow. + match &self.kind { + &Ident(name, is_raw) => Some((Ident::new(name, self.span), is_raw)), + Interpolated(nt) => match **nt { + NtIdent(ident, is_raw) => Some((ident, is_raw)), + _ => None, + }, _ => None, } } /// Returns a lifetime identifier if this token is a lifetime. + #[inline] pub fn lifetime(&self) -> Option { - let token = self.uninterpolate(); - match token.kind { - Lifetime(name) => Some(Ident::new(name, token.span)), + // We avoid using `Token::uninterpolate` here because it's slow. + match &self.kind { + &Lifetime(name) => Some(Ident::new(name, self.span)), + Interpolated(nt) => match **nt { + NtLifetime(ident) => Some(ident), + _ => None, + }, _ => None, } } From bbef9756ea5a0b30f737aa038ea8622159cff08a Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 3 May 2022 14:23:19 +1000 Subject: [PATCH 2/3] Fix spelling of an identifier. --- compiler/rustc_expand/src/mbe/transcribe.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index 94b6c3153ca30..fdd8dc93fc1a5 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -217,10 +217,10 @@ pub(super) fn transcribe<'a>( } // Replace the meta-var with the matched token tree from the invocation. - mbe::TokenTree::MetaVar(mut sp, mut orignal_ident) => { + mbe::TokenTree::MetaVar(mut sp, mut original_ident) => { // Find the matched nonterminal from the macro invocation, and use it to replace // the meta-var. - let ident = MacroRulesNormalizedIdent::new(orignal_ident); + let ident = MacroRulesNormalizedIdent::new(original_ident); if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) { match cur_matched { MatchedTokenTree(ref tt) => { @@ -249,9 +249,9 @@ pub(super) fn transcribe<'a>( // If we aren't able to match the meta-var, we push it back into the result but // with modified syntax context. (I believe this supports nested macros). marker.visit_span(&mut sp); - marker.visit_ident(&mut orignal_ident); + marker.visit_ident(&mut original_ident); result.push(TokenTree::token(token::Dollar, sp).into()); - result.push(TokenTree::Token(Token::from_ast_ident(orignal_ident)).into()); + result.push(TokenTree::Token(Token::from_ast_ident(original_ident)).into()); } } From 1d2e172935a90ecc146ac4b9e82ab1485f4d7fdd Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 3 May 2022 15:12:43 +1000 Subject: [PATCH 3/3] Remove unnecessary `NtIdent` in `Token::is_whole_expr`. The comment on this function explains that it's a specialized version of `maybe_whole_expr`. But `maybe_whole_expr` doesn't do anything with `NtIdent`, so `is_whole_expr` also doesn't need to. --- compiler/rustc_ast/src/token.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index 4eb494aeb9b5a..1c366bc8e9a73 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -531,7 +531,7 @@ impl Token { /// (which happens while parsing the result of macro expansion)? pub fn is_whole_expr(&self) -> bool { if let Interpolated(ref nt) = self.kind - && let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtIdent(..) | NtBlock(_) = **nt + && let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = **nt { return true; }