]> git.lizzy.rs Git - rust.git/commitdiff
Special-case literals in `parse_bottom_expr`.
authorNicholas Nethercote <nnethercote@mozilla.com>
Fri, 7 Jun 2019 02:08:38 +0000 (12:08 +1000)
committerNicholas Nethercote <nnethercote@mozilla.com>
Mon, 10 Jun 2019 00:04:25 +0000 (10:04 +1000)
This makes parsing faster, particularly for code with large constants,
for two reasons:
- it skips all the keyword comparisons for literals;
- it replaces the unnecessary `parse_literal_maybe_minus` call with
  `parse_lit`, avoiding an unnecessary allocation via `mk_expr`.

src/libsyntax/parse/parser.rs

index 43e7c9330e418ead1b52b47b1d22de011e33bcc6..9b9954859be4949005e7577ac4c5c255cd9eb36e 100644 (file)
@@ -2002,8 +2002,29 @@ fn parse_bottom_expr(&mut self) -> PResult<'a, P<Expr>> {
 
         let ex: ExprKind;
 
+        macro_rules! parse_lit {
+            () => {
+                match self.parse_lit() {
+                    Ok(literal) => {
+                        hi = self.prev_span;
+                        ex = ExprKind::Lit(literal);
+                    }
+                    Err(mut err) => {
+                        self.cancel(&mut err);
+                        return Err(self.expected_expression_found());
+                    }
+                }
+            }
+        }
+
         // Note: when adding new syntax here, don't forget to adjust TokenKind::can_begin_expr().
         match self.token.kind {
+            // This match arm is a special-case of the `_` match arm below and
+            // could be removed without changing functionality, but it's faster
+            // to have it here, especially for programs with large constants.
+            token::Literal(_) => {
+                parse_lit!()
+            }
             token::OpenDelim(token::Paren) => {
                 self.bump();
 
@@ -2249,16 +2270,7 @@ fn parse_bottom_expr(&mut self) -> PResult<'a, P<Expr>> {
                         self.bump();
                         return Ok(self.mk_expr(self.span, ExprKind::Err, ThinVec::new()));
                     }
-                    match self.parse_literal_maybe_minus() {
-                        Ok(expr) => {
-                            hi = expr.span;
-                            ex = expr.node.clone();
-                        }
-                        Err(mut err) => {
-                            self.cancel(&mut err);
-                            return Err(self.expected_expression_found());
-                        }
-                    }
+                    parse_lit!()
                 }
             }
         }