From 637a93cb5d2ac75ce683d71bf83695907abc323a Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 3 May 2022 12:40:35 +1000 Subject: [PATCH] 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 1589a882f089..4eb494aeb9b5 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, } }