about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2022-05-03 12:40:35 +1000
committerNicholas Nethercote <n.nethercote@gmail.com>2022-05-04 06:15:29 +1000
commit637a93cb5d2ac75ce683d71bf83695907abc323a (patch)
tree44a8066968aff7db2cc8b69d187498438e7c5c88
parentbaaa3b682986879c7784b5733ecea942e9ae7de3 (diff)
downloadrust-637a93cb5d2ac75ce683d71bf83695907abc323a.tar.gz
rust-637a93cb5d2ac75ce683d71bf83695907abc323a.zip
Speed up `Token::{ident,lifetime}`.
They're hot enough that the repeated matching done by `uninterpolate`
has a measurable effect.
-rw-r--r--compiler/rustc_ast/src/token.rs22
1 files changed, 16 insertions, 6 deletions
diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs
index 1589a882f08..4eb494aeb9b 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<Ident> {
-        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,
         }
     }