]> git.lizzy.rs Git - rust.git/commitdiff
Introduced `Cursor::next_with_spacing_ref`.
authorNicholas Nethercote <n.nethercote@gmail.com>
Thu, 21 Apr 2022 03:49:40 +0000 (13:49 +1000)
committerNicholas Nethercote <n.nethercote@gmail.com>
Thu, 21 Apr 2022 03:49:40 +0000 (13:49 +1000)
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`.

compiler/rustc_ast/src/tokenstream.rs
compiler/rustc_parse/src/parser/mod.rs

index 3321d3bf380ec5ad3e7665dc556203dd1d39b3a1..d609fa6720502c5ca1b679ad2cd0b0c99c813be7 100644 (file)
@@ -583,6 +583,14 @@ pub fn next_with_spacing(&mut self) -> Option<TreeAndSpacing> {
         })
     }
 
+    #[inline]
+    pub fn next_with_spacing_ref(&mut self) -> Option<&TreeAndSpacing> {
+        self.stream.0.get(self.index).map(|tree| {
+            self.index += 1;
+            tree
+        })
+    }
+
     pub fn index(&self) -> usize {
         self.index
     }
index a620266247a88573622603ec93a176ee7fa3738e..1686c5873e1833f4b5627fd627300aad055e950d 100644 (file)
@@ -267,17 +267,17 @@ fn inlined_next(&mut self, desugar_doc_comments: bool) -> (Token, Spacing) {
             // FIXME: we currently don't return `NoDelim` open/close delims. To fix #67062 we will
             // need to, whereupon the `delim != DelimToken::NoDelim` conditions below can be
             // removed, as well as the loop.
-            if let Some((tree, spacing)) = self.frame.tree_cursor.next_with_spacing() {
+            if let Some((tree, spacing)) = self.frame.tree_cursor.next_with_spacing_ref() {
                 match tree {
-                    TokenTree::Token(token) => match (desugar_doc_comments, &token) {
+                    &TokenTree::Token(ref token) => match (desugar_doc_comments, token) {
                         (true, &Token { kind: token::DocComment(_, attr_style, data), span }) => {
                             return self.desugar(attr_style, data, span);
                         }
-                        _ => return (tokenspacing),
+                        _ => return (token.clone(), *spacing),
                     },
-                    TokenTree::Delimited(sp, delim, tts) => {
+                    &TokenTree::Delimited(sp, delim, ref tts) => {
                         // Set `open_delim` to true here because we deal with it immediately.
-                        let frame = TokenCursorFrame::new(sp, delim, tts);
+                        let frame = TokenCursorFrame::new(sp, delim, tts.clone());
                         self.stack.push(mem::replace(&mut self.frame, frame));
                         if delim != DelimToken::NoDelim {
                             return (Token::new(token::OpenDelim(delim), sp.open), Spacing::Alone);