Skip to content

Commit 643e9f7

Browse files
committedApr 21, 2022
Introduced Cursor::next_with_spacing_ref.
This lets us clone just the parts within a `TokenTree` that need cloning, rather than the entire thing. This is a surprisingly large performance win, up to 4% on `async-std-1.10.0`.
1 parent cc4e344 commit 643e9f7

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed
 

‎compiler/rustc_ast/src/tokenstream.rs

+8
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,14 @@ impl Cursor {
583583
})
584584
}
585585

586+
#[inline]
587+
pub fn next_with_spacing_ref(&mut self) -> Option<&TreeAndSpacing> {
588+
self.stream.0.get(self.index).map(|tree| {
589+
self.index += 1;
590+
tree
591+
})
592+
}
593+
586594
pub fn index(&self) -> usize {
587595
self.index
588596
}

‎compiler/rustc_parse/src/parser/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,17 @@ impl TokenCursor {
267267
// FIXME: we currently don't return `NoDelim` open/close delims. To fix #67062 we will
268268
// need to, whereupon the `delim != DelimToken::NoDelim` conditions below can be
269269
// removed, as well as the loop.
270-
if let Some((tree, spacing)) = self.frame.tree_cursor.next_with_spacing() {
270+
if let Some((tree, spacing)) = self.frame.tree_cursor.next_with_spacing_ref() {
271271
match tree {
272-
TokenTree::Token(token) => match (desugar_doc_comments, &token) {
272+
&TokenTree::Token(ref token) => match (desugar_doc_comments, token) {
273273
(true, &Token { kind: token::DocComment(_, attr_style, data), span }) => {
274274
return self.desugar(attr_style, data, span);
275275
}
276-
_ => return (token, spacing),
276+
_ => return (token.clone(), *spacing),
277277
},
278-
TokenTree::Delimited(sp, delim, tts) => {
278+
&TokenTree::Delimited(sp, delim, ref tts) => {
279279
// Set `open_delim` to true here because we deal with it immediately.
280-
let frame = TokenCursorFrame::new(sp, delim, tts);
280+
let frame = TokenCursorFrame::new(sp, delim, tts.clone());
281281
self.stack.push(mem::replace(&mut self.frame, frame));
282282
if delim != DelimToken::NoDelim {
283283
return (Token::new(token::OpenDelim(delim), sp.open), Spacing::Alone);

0 commit comments

Comments
 (0)
Please sign in to comment.