about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2020-09-03 23:26:59 +0200
committerAleksey Kladov <aleksey.kladov@gmail.com>2020-09-03 23:28:22 +0200
commit09d3db2e590030de8ae7d00589f8a174e5f51f03 (patch)
treecc54fc9fa0bf826c873c3fb060069fd45cfeb58e
parent850c3219fb8659608eb62cd43eaee29e9e354379 (diff)
downloadrust-09d3db2e590030de8ae7d00589f8a174e5f51f03.tar.gz
rust-09d3db2e590030de8ae7d00589f8a174e5f51f03.zip
Optimize Cursor::look_ahead
Cloning a tt is cheap, but not free (there's Arc inside).
-rw-r--r--compiler/rustc_ast/src/tokenstream.rs4
-rw-r--r--compiler/rustc_expand/src/proc_macro_server.rs11
-rw-r--r--compiler/rustc_parse/src/parser/mod.rs10
3 files changed, 15 insertions, 10 deletions
diff --git a/compiler/rustc_ast/src/tokenstream.rs b/compiler/rustc_ast/src/tokenstream.rs
index 151acddae84..fb98f55a215 100644
--- a/compiler/rustc_ast/src/tokenstream.rs
+++ b/compiler/rustc_ast/src/tokenstream.rs
@@ -403,8 +403,8 @@ impl Cursor {
         self.index = index;
     }
 
-    pub fn look_ahead(&self, n: usize) -> Option<TokenTree> {
-        self.stream.0[self.index..].get(n).map(|(tree, _)| tree.clone())
+    pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
+        self.stream.0[self.index..].get(n).map(|(tree, _)| tree)
     }
 }
 
diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs
index 19c87d08a13..765871a6396 100644
--- a/compiler/rustc_expand/src/proc_macro_server.rs
+++ b/compiler/rustc_expand/src/proc_macro_server.rs
@@ -47,13 +47,18 @@ impl ToInternal<token::DelimToken> for Delimiter {
     }
 }
 
-impl FromInternal<(TreeAndJoint, Option<tokenstream::TokenTree>, &'_ ParseSess, &'_ mut Vec<Self>)>
-    for TokenTree<Group, Punct, Ident, Literal>
+impl
+    FromInternal<(
+        TreeAndJoint,
+        Option<&'_ tokenstream::TokenTree>,
+        &'_ ParseSess,
+        &'_ mut Vec<Self>,
+    )> for TokenTree<Group, Punct, Ident, Literal>
 {
     fn from_internal(
         ((tree, is_joint), look_ahead, sess, stack): (
             TreeAndJoint,
-            Option<tokenstream::TokenTree>,
+            Option<&tokenstream::TokenTree>,
             &ParseSess,
             &mut Vec<Self>,
         ),
diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs
index 84edfecad19..1b2067f8f25 100644
--- a/compiler/rustc_parse/src/parser/mod.rs
+++ b/compiler/rustc_parse/src/parser/mod.rs
@@ -822,15 +822,15 @@ impl<'a> Parser<'a> {
         }
 
         let frame = &self.token_cursor.frame;
-        looker(&match frame.tree_cursor.look_ahead(dist - 1) {
+        match frame.tree_cursor.look_ahead(dist - 1) {
             Some(tree) => match tree {
-                TokenTree::Token(token) => token,
+                TokenTree::Token(token) => looker(token),
                 TokenTree::Delimited(dspan, delim, _) => {
-                    Token::new(token::OpenDelim(delim), dspan.open)
+                    looker(&Token::new(token::OpenDelim(delim.clone()), dspan.open))
                 }
             },
-            None => Token::new(token::CloseDelim(frame.delim), frame.span.close),
-        })
+            None => looker(&Token::new(token::CloseDelim(frame.delim), frame.span.close)),
+        }
     }
 
     /// Returns whether any of the given keywords are `dist` tokens ahead of the current one.