Skip to content

Commit

Permalink
Optimize Cursor::look_ahead
Browse files Browse the repository at this point in the history
Cloning a tt is cheap, but not free (there's Arc inside).
  • Loading branch information
matklad committed Sep 3, 2020
1 parent 850c321 commit 09d3db2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_ast/src/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
),
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 09d3db2

Please sign in to comment.