Optimize Cursor::look_ahead
Cloning a tt is cheap, but not free (there's Arc inside).
This commit is contained in:
parent
850c3219fb
commit
09d3db2e59
@ -403,8 +403,8 @@ impl Cursor {
|
|||||||
self.index = index;
|
self.index = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn look_ahead(&self, n: usize) -> Option<TokenTree> {
|
pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
|
||||||
self.stream.0[self.index..].get(n).map(|(tree, _)| tree.clone())
|
self.stream.0[self.index..].get(n).map(|(tree, _)| tree)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,13 +47,18 @@ impl ToInternal<token::DelimToken> for Delimiter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromInternal<(TreeAndJoint, Option<tokenstream::TokenTree>, &'_ ParseSess, &'_ mut Vec<Self>)>
|
impl
|
||||||
for TokenTree<Group, Punct, Ident, Literal>
|
FromInternal<(
|
||||||
|
TreeAndJoint,
|
||||||
|
Option<&'_ tokenstream::TokenTree>,
|
||||||
|
&'_ ParseSess,
|
||||||
|
&'_ mut Vec<Self>,
|
||||||
|
)> for TokenTree<Group, Punct, Ident, Literal>
|
||||||
{
|
{
|
||||||
fn from_internal(
|
fn from_internal(
|
||||||
((tree, is_joint), look_ahead, sess, stack): (
|
((tree, is_joint), look_ahead, sess, stack): (
|
||||||
TreeAndJoint,
|
TreeAndJoint,
|
||||||
Option<tokenstream::TokenTree>,
|
Option<&tokenstream::TokenTree>,
|
||||||
&ParseSess,
|
&ParseSess,
|
||||||
&mut Vec<Self>,
|
&mut Vec<Self>,
|
||||||
),
|
),
|
||||||
|
@ -822,15 +822,15 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let frame = &self.token_cursor.frame;
|
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 {
|
Some(tree) => match tree {
|
||||||
TokenTree::Token(token) => token,
|
TokenTree::Token(token) => looker(token),
|
||||||
TokenTree::Delimited(dspan, delim, _) => {
|
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.
|
/// Returns whether any of the given keywords are `dist` tokens ahead of the current one.
|
||||||
|
Loading…
Reference in New Issue
Block a user