From 09d3db2e590030de8ae7d00589f8a174e5f51f03 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 3 Sep 2020 23:26:59 +0200 Subject: [PATCH] Optimize Cursor::look_ahead Cloning a tt is cheap, but not free (there's Arc inside). --- compiler/rustc_ast/src/tokenstream.rs | 4 ++-- compiler/rustc_expand/src/proc_macro_server.rs | 11 ++++++++--- compiler/rustc_parse/src/parser/mod.rs | 10 +++++----- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_ast/src/tokenstream.rs b/compiler/rustc_ast/src/tokenstream.rs index 151acddae840e..fb98f55a2154a 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 { - 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 19c87d08a1379..765871a6396f3 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 for Delimiter { } } -impl FromInternal<(TreeAndJoint, Option, &'_ ParseSess, &'_ mut Vec)> - for TokenTree +impl + FromInternal<( + TreeAndJoint, + Option<&'_ tokenstream::TokenTree>, + &'_ ParseSess, + &'_ mut Vec, + )> for TokenTree { fn from_internal( ((tree, is_joint), look_ahead, sess, stack): ( TreeAndJoint, - Option, + Option<&tokenstream::TokenTree>, &ParseSess, &mut Vec, ), diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 84edfecad192f..1b2067f8f256b 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.