Skip to content

Commit 09d3db2

Browse files
committed
Optimize Cursor::look_ahead
Cloning a tt is cheap, but not free (there's Arc inside).
1 parent 850c321 commit 09d3db2

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

compiler/rustc_ast/src/tokenstream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ impl Cursor {
403403
self.index = index;
404404
}
405405

406-
pub fn look_ahead(&self, n: usize) -> Option<TokenTree> {
407-
self.stream.0[self.index..].get(n).map(|(tree, _)| tree.clone())
406+
pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
407+
self.stream.0[self.index..].get(n).map(|(tree, _)| tree)
408408
}
409409
}
410410

compiler/rustc_expand/src/proc_macro_server.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,18 @@ impl ToInternal<token::DelimToken> for Delimiter {
4747
}
4848
}
4949

50-
impl FromInternal<(TreeAndJoint, Option<tokenstream::TokenTree>, &'_ ParseSess, &'_ mut Vec<Self>)>
51-
for TokenTree<Group, Punct, Ident, Literal>
50+
impl
51+
FromInternal<(
52+
TreeAndJoint,
53+
Option<&'_ tokenstream::TokenTree>,
54+
&'_ ParseSess,
55+
&'_ mut Vec<Self>,
56+
)> for TokenTree<Group, Punct, Ident, Literal>
5257
{
5358
fn from_internal(
5459
((tree, is_joint), look_ahead, sess, stack): (
5560
TreeAndJoint,
56-
Option<tokenstream::TokenTree>,
61+
Option<&tokenstream::TokenTree>,
5762
&ParseSess,
5863
&mut Vec<Self>,
5964
),

compiler/rustc_parse/src/parser/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -822,15 +822,15 @@ impl<'a> Parser<'a> {
822822
}
823823

824824
let frame = &self.token_cursor.frame;
825-
looker(&match frame.tree_cursor.look_ahead(dist - 1) {
825+
match frame.tree_cursor.look_ahead(dist - 1) {
826826
Some(tree) => match tree {
827-
TokenTree::Token(token) => token,
827+
TokenTree::Token(token) => looker(token),
828828
TokenTree::Delimited(dspan, delim, _) => {
829-
Token::new(token::OpenDelim(delim), dspan.open)
829+
looker(&Token::new(token::OpenDelim(delim.clone()), dspan.open))
830830
}
831831
},
832-
None => Token::new(token::CloseDelim(frame.delim), frame.span.close),
833-
})
832+
None => looker(&Token::new(token::CloseDelim(frame.delim), frame.span.close)),
833+
}
834834
}
835835

836836
/// Returns whether any of the given keywords are `dist` tokens ahead of the current one.

0 commit comments

Comments
 (0)