From a4463b431904c0307135cc311674f0698b3d0946 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 28 Apr 2024 19:06:26 +0200 Subject: rebel-parse: box expressions in statements The size of the statement type has a noticeable effect on performance. --- crates/rebel-lang/src/scope.rs | 6 +++--- crates/rebel-parse/src/ast.rs | 22 +++++++++++++++------- crates/rebel-parse/src/grammar/recipe.rs | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/crates/rebel-lang/src/scope.rs b/crates/rebel-lang/src/scope.rs index 7542a1e..d57a1f4 100644 --- a/crates/rebel-lang/src/scope.rs +++ b/crates/rebel-lang/src/scope.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, ops::Deref}; use rebel_parse::ast; @@ -24,7 +24,7 @@ impl Context { let ast::TypedExpr { expr: dest_expr, typ: _, - } = dest; + } = dest.deref(); // TODO: Handle other assignable expressions let dest_path = match dest_expr { @@ -58,7 +58,7 @@ impl Context { let ast::TypedExpr { expr: dest_expr, typ: _, - } = dest; + } = dest.deref(); // TODO: Handle other assignable expressions let dest_path = match dest_expr { diff --git a/crates/rebel-parse/src/ast.rs b/crates/rebel-parse/src/ast.rs index 490c2f1..8be015b 100644 --- a/crates/rebel-parse/src/ast.rs +++ b/crates/rebel-parse/src/ast.rs @@ -49,8 +49,13 @@ impl<'a> Body<'a> { #[derive(Debug, Clone, PartialEq, Eq)] pub enum BodyStmt<'a> { - Assign { dest: TypedExpr<'a>, expr: Expr<'a> }, - Expr { expr: Expr<'a> }, + Assign { + dest: Box>, + expr: Box>, + }, + Expr { + expr: Box>, + }, Empty, } @@ -61,17 +66,20 @@ impl<'a> BodyStmt<'a> { swapped: bool, expr: Expr<'a>, ) -> Self { - match op { + let expr = match op { Some(op) => { let dest_expr = dest.expr.clone(); - let expr = if swapped { + if swapped { Expr::binary(expr, op, dest_expr) } else { Expr::binary(dest_expr, op, expr) - }; - BodyStmt::Assign { dest, expr } + } } - None => BodyStmt::Assign { dest, expr }, + None => expr, + }; + BodyStmt::Assign { + dest: Box::new(dest), + expr: Box::new(expr), } } diff --git a/crates/rebel-parse/src/grammar/recipe.rs b/crates/rebel-parse/src/grammar/recipe.rs index c23786b..fedc8a8 100644 --- a/crates/rebel-parse/src/grammar/recipe.rs +++ b/crates/rebel-parse/src/grammar/recipe.rs @@ -34,7 +34,7 @@ peg::parser! { ast::BodyStmt::assign(dest, op, false, expr) } / expr:expr() { - ast::BodyStmt::Expr { expr } + ast::BodyStmt::Expr { expr: Box::new(expr) } } / { ast::BodyStmt::Empty } -- cgit v1.2.3