Reduce size of Expr and Stmt by Boxing strings.

This commit is contained in:
Stephen Chung 2020-05-01 17:32:39 +08:00
parent 5c5e1db61e
commit c7c7fe3dfc
3 changed files with 140 additions and 85 deletions

View File

@ -806,7 +806,7 @@ impl Engine {
} else { } else {
match rhs { match rhs {
// xxx.fn_name(arg_expr_list) // xxx.fn_name(arg_expr_list)
Expr::FunctionCall(fn_name, _, def_val, pos) => { Expr::FnCall(fn_name, _, def_val, pos) => {
let mut args: Vec<_> = once(obj) let mut args: Vec<_> = once(obj)
.chain(idx_val.downcast_mut::<Array>().unwrap().iter_mut()) .chain(idx_val.downcast_mut::<Array>().unwrap().iter_mut())
.collect(); .collect();
@ -976,7 +976,7 @@ impl Engine {
level: usize, level: usize,
) -> Result<(), Box<EvalAltResult>> { ) -> Result<(), Box<EvalAltResult>> {
match expr { match expr {
Expr::FunctionCall(_, arg_exprs, _, _) => { Expr::FnCall(_, arg_exprs, _, _) => {
let arg_values = arg_exprs let arg_values = arg_exprs
.iter() .iter()
.map(|arg_expr| self.eval_expr(scope, state, fn_lib, arg_expr, level)) .map(|arg_expr| self.eval_expr(scope, state, fn_lib, arg_expr, level))
@ -1233,7 +1233,7 @@ impl Engine {
.collect::<Result<HashMap<_, _>, _>>()?, .collect::<Result<HashMap<_, _>, _>>()?,
)))), )))),
Expr::FunctionCall(fn_name, arg_exprs, def_val, pos) => { Expr::FnCall(fn_name, arg_exprs, def_val, pos) => {
let mut arg_values = arg_exprs let mut arg_values = arg_exprs
.iter() .iter()
.map(|expr| self.eval_expr(scope, state, fn_lib, expr, level)) .map(|expr| self.eval_expr(scope, state, fn_lib, expr, level))
@ -1242,7 +1242,7 @@ impl Engine {
let mut args: Vec<_> = arg_values.iter_mut().collect(); let mut args: Vec<_> = arg_values.iter_mut().collect();
// eval - only in function call style // eval - only in function call style
if fn_name == KEYWORD_EVAL if fn_name.as_ref() == KEYWORD_EVAL
&& args.len() == 1 && args.len() == 1
&& !self.has_override(fn_lib, KEYWORD_EVAL) && !self.has_override(fn_lib, KEYWORD_EVAL)
{ {
@ -1409,7 +1409,8 @@ impl Engine {
.and_then(|pkg| pkg.type_iterators.get(&tid)) .and_then(|pkg| pkg.type_iterators.get(&tid))
}) { }) {
// Add the loop variable // Add the loop variable
scope.push(name.clone(), ()); let var_name = name.as_ref().clone();
scope.push(var_name, ());
let index = scope.len() - 1; let index = scope.len() - 1;
for a in iter_fn(arr) { for a in iter_fn(arr) {
@ -1466,13 +1467,15 @@ impl Engine {
Stmt::Let(name, Some(expr), _) => { Stmt::Let(name, Some(expr), _) => {
let val = self.eval_expr(scope, state, fn_lib, expr, level)?; let val = self.eval_expr(scope, state, fn_lib, expr, level)?;
// TODO - avoid copying variable name in inner block? // TODO - avoid copying variable name in inner block?
scope.push_dynamic_value(name.clone(), ScopeEntryType::Normal, val, false); let var_name = name.as_ref().clone();
scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false);
Ok(Default::default()) Ok(Default::default())
} }
Stmt::Let(name, None, _) => { Stmt::Let(name, None, _) => {
// TODO - avoid copying variable name in inner block? // TODO - avoid copying variable name in inner block?
scope.push(name.clone(), ()); let var_name = name.as_ref().clone();
scope.push(var_name, ());
Ok(Default::default()) Ok(Default::default())
} }
@ -1480,7 +1483,8 @@ impl Engine {
Stmt::Const(name, expr, _) if expr.is_constant() => { Stmt::Const(name, expr, _) if expr.is_constant() => {
let val = self.eval_expr(scope, state, fn_lib, expr, level)?; let val = self.eval_expr(scope, state, fn_lib, expr, level)?;
// TODO - avoid copying variable name in inner block? // TODO - avoid copying variable name in inner block?
scope.push_dynamic_value(name.clone(), ScopeEntryType::Constant, val, true); let var_name = name.as_ref().clone();
scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true);
Ok(Default::default()) Ok(Default::default())
} }

View File

@ -558,18 +558,18 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr {
}, },
// Do not call some special keywords // Do not call some special keywords
Expr::FunctionCall(id, args, def_value, pos) if DONT_EVAL_KEYWORDS.contains(&id.as_ref())=> Expr::FnCall(id, args, def_value, pos) if DONT_EVAL_KEYWORDS.contains(&id.as_ref().as_ref())=>
Expr::FunctionCall(id, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos), Expr::FnCall(id, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos),
// Eagerly call functions // Eagerly call functions
Expr::FunctionCall(id, args, def_value, pos) Expr::FnCall(id, args, def_value, pos)
if state.optimization_level == OptimizationLevel::Full // full optimizations if state.optimization_level == OptimizationLevel::Full // full optimizations
&& args.iter().all(|expr| expr.is_constant()) // all arguments are constants && args.iter().all(|expr| expr.is_constant()) // all arguments are constants
=> { => {
// First search in script-defined functions (can override built-in) // First search in script-defined functions (can override built-in)
if state.fn_lib.iter().find(|(name, len)| name == &id && *len == args.len()).is_some() { if state.fn_lib.iter().find(|(name, len)| name == id.as_ref() && *len == args.len()).is_some() {
// A script-defined function overrides the built-in function - do not make the call // A script-defined function overrides the built-in function - do not make the call
return Expr::FunctionCall(id, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos); return Expr::FnCall(id, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos);
} }
let mut arg_values: Vec<_> = args.iter().map(Expr::get_constant_value).collect(); let mut arg_values: Vec<_> = args.iter().map(Expr::get_constant_value).collect();
@ -577,7 +577,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr {
// Save the typename of the first argument if it is `type_of()` // Save the typename of the first argument if it is `type_of()`
// This is to avoid `call_args` being passed into the closure // This is to avoid `call_args` being passed into the closure
let arg_for_type_of = if id == KEYWORD_TYPE_OF && call_args.len() == 1 { let arg_for_type_of = if *id == KEYWORD_TYPE_OF && call_args.len() == 1 {
state.engine.map_type_name(call_args[0].type_name()) state.engine.map_type_name(call_args[0].type_name())
} else { } else {
"" ""
@ -600,13 +600,13 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr {
}) })
).unwrap_or_else(|| ).unwrap_or_else(||
// Optimize function call arguments // Optimize function call arguments
Expr::FunctionCall(id, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos) Expr::FnCall(id, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos)
) )
} }
// id(args ..) -> optimize function call arguments // id(args ..) -> optimize function call arguments
Expr::FunctionCall(id, args, def_value, pos) => Expr::FnCall(id, args, def_value, pos) =>
Expr::FunctionCall(id, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos), Expr::FnCall(id, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos),
// constant-name // constant-name
Expr::Variable(name, _, pos) if state.contains_constant(&name) => { Expr::Variable(name, _, pos) if state.contains_constant(&name) => {

View File

@ -231,11 +231,11 @@ pub enum Stmt {
/// loop { stmt } /// loop { stmt }
Loop(Box<Stmt>), Loop(Box<Stmt>),
/// for id in expr { stmt } /// for id in expr { stmt }
For(String, Box<Expr>, Box<Stmt>), For(Box<String>, Box<Expr>, Box<Stmt>),
/// let id = expr /// let id = expr
Let(String, Option<Box<Expr>>, Position), Let(Box<String>, Option<Box<Expr>>, Position),
/// const id = expr /// const id = expr
Const(String, Box<Expr>, Position), Const(Box<String>, Box<Expr>, Position),
/// { stmt; ... } /// { stmt; ... }
Block(Vec<Stmt>, Position), Block(Vec<Stmt>, Position),
/// { stmt } /// { stmt }
@ -317,7 +317,7 @@ pub enum Expr {
/// String constant. /// String constant.
StringConstant(String, Position), StringConstant(String, Position),
/// Variable access. /// Variable access.
Variable(String, Option<NonZeroUsize>, Position), Variable(Box<String>, Option<NonZeroUsize>, Position),
/// Property access. /// Property access.
Property(String, Position), Property(String, Position),
/// { stmt } /// { stmt }
@ -325,8 +325,8 @@ pub enum Expr {
/// func(expr, ... ) /// func(expr, ... )
/// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls /// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls
/// and the function names are predictable, so no need to allocate a new `String`. /// and the function names are predictable, so no need to allocate a new `String`.
FunctionCall( FnCall(
Cow<'static, str>, Box<Cow<'static, str>>,
Box<Vec<Expr>>, Box<Vec<Expr>>,
Option<Box<Dynamic>>, Option<Box<Dynamic>>,
Position, Position,
@ -427,7 +427,7 @@ impl Expr {
| Self::Variable(_, _, pos) | Self::Variable(_, _, pos)
| Self::Property(_, pos) | Self::Property(_, pos)
| Self::Stmt(_, pos) | Self::Stmt(_, pos)
| Self::FunctionCall(_, _, _, pos) | Self::FnCall(_, _, _, pos)
| Self::And(_, _, pos) | Self::And(_, _, pos)
| Self::Or(_, _, pos) | Self::Or(_, _, pos)
| Self::In(_, _, pos) | Self::In(_, _, pos)
@ -453,7 +453,7 @@ impl Expr {
| Self::Variable(_, _, pos) | Self::Variable(_, _, pos)
| Self::Property(_, pos) | Self::Property(_, pos)
| Self::Stmt(_, pos) | Self::Stmt(_, pos)
| Self::FunctionCall(_, _, _, pos) | Self::FnCall(_, _, _, pos)
| Self::And(_, _, pos) | Self::And(_, _, pos)
| Self::Or(_, _, pos) | Self::Or(_, _, pos)
| Self::In(_, _, pos) | Self::In(_, _, pos)
@ -530,7 +530,7 @@ impl Expr {
Self::StringConstant(_, _) Self::StringConstant(_, _)
| Self::Stmt(_, _) | Self::Stmt(_, _)
| Self::FunctionCall(_, _, _, _) | Self::FnCall(_, _, _, _)
| Self::Assignment(_, _, _) | Self::Assignment(_, _, _)
| Self::Dot(_, _, _) | Self::Dot(_, _, _)
| Self::Index(_, _, _) | Self::Index(_, _, _)
@ -550,7 +550,7 @@ impl Expr {
/// Convert a `Variable` into a `Property`. All other variants are untouched. /// Convert a `Variable` into a `Property`. All other variants are untouched.
pub(crate) fn into_property(self) -> Self { pub(crate) fn into_property(self) -> Self {
match self { match self {
Self::Variable(id, _, pos) => Self::Property(id, pos), Self::Variable(id, _, pos) => Self::Property(*id, pos),
_ => self, _ => self,
} }
} }
@ -633,7 +633,12 @@ fn parse_call_expr<'a>(
// id() // id()
(Token::RightParen, _) => { (Token::RightParen, _) => {
eat_token(input, Token::RightParen); eat_token(input, Token::RightParen);
return Ok(Expr::FunctionCall(id.into(), Box::new(args), None, begin)); return Ok(Expr::FnCall(
Box::new(id.into()),
Box::new(args),
None,
begin,
));
} }
// id... // id...
_ => (), _ => (),
@ -645,7 +650,12 @@ fn parse_call_expr<'a>(
match input.peek().unwrap() { match input.peek().unwrap() {
(Token::RightParen, _) => { (Token::RightParen, _) => {
eat_token(input, Token::RightParen); eat_token(input, Token::RightParen);
return Ok(Expr::FunctionCall(id.into(), Box::new(args), None, begin)); return Ok(Expr::FnCall(
Box::new(id.into()),
Box::new(args),
None,
begin,
));
} }
(Token::Comma, _) => { (Token::Comma, _) => {
eat_token(input, Token::Comma); eat_token(input, Token::Comma);
@ -968,7 +978,7 @@ fn parse_primary<'a>(
Token::StringConst(s) => Expr::StringConstant(s, pos), Token::StringConst(s) => Expr::StringConstant(s, pos),
Token::Identifier(s) => { Token::Identifier(s) => {
let index = stack.find(&s); let index = stack.find(&s);
Expr::Variable(s, index, pos) Expr::Variable(Box::new(s), index, pos)
} }
Token::LeftParen => parse_paren_expr(input, stack, pos, allow_stmt_expr)?, Token::LeftParen => parse_paren_expr(input, stack, pos, allow_stmt_expr)?,
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
@ -995,8 +1005,10 @@ fn parse_primary<'a>(
root_expr = match (root_expr, token) { root_expr = match (root_expr, token) {
// Function call // Function call
(Expr::Variable(id, _, pos), Token::LeftParen) (Expr::Variable(id, _, pos), Token::LeftParen) => {
| (Expr::Property(id, pos), Token::LeftParen) => { parse_call_expr(input, stack, *id, pos, allow_stmt_expr)?
}
(Expr::Property(id, pos), Token::LeftParen) => {
parse_call_expr(input, stack, id, pos, allow_stmt_expr)? parse_call_expr(input, stack, id, pos, allow_stmt_expr)?
} }
// Indexing // Indexing
@ -1055,7 +1067,12 @@ fn parse_unary<'a>(
Expr::FloatConstant(f, pos) => Ok(Expr::FloatConstant(-f, pos)), Expr::FloatConstant(f, pos) => Ok(Expr::FloatConstant(-f, pos)),
// Call negative function // Call negative function
e => Ok(Expr::FunctionCall("-".into(), Box::new(vec![e]), None, pos)), e => Ok(Expr::FnCall(
Box::new("-".into()),
Box::new(vec![e]),
None,
pos,
)),
} }
} }
// +expr // +expr
@ -1066,8 +1083,8 @@ fn parse_unary<'a>(
// !expr // !expr
(Token::Bang, _) => { (Token::Bang, _) => {
let pos = eat_token(input, Token::Bang); let pos = eat_token(input, Token::Bang);
Ok(Expr::FunctionCall( Ok(Expr::FnCall(
"!".into(), Box::new("!".into()),
Box::new(vec![parse_primary(input, stack, allow_stmt_expr)?]), Box::new(vec![parse_primary(input, stack, allow_stmt_expr)?]),
Some(Box::new(false.into())), // NOT operator, when operating on invalid operand, defaults to false Some(Box::new(false.into())), // NOT operator, when operating on invalid operand, defaults to false
pos, pos,
@ -1120,7 +1137,8 @@ fn parse_op_assignment_stmt<'a>(
let rhs = parse_expr(input, stack, allow_stmt_expr)?; let rhs = parse_expr(input, stack, allow_stmt_expr)?;
// lhs op= rhs -> lhs = op(lhs, rhs) // lhs op= rhs -> lhs = op(lhs, rhs)
let rhs_expr = Expr::FunctionCall(op.into(), Box::new(vec![lhs_copy, rhs]), None, pos); let args = vec![lhs_copy, rhs];
let rhs_expr = Expr::FnCall(Box::new(op.into()), Box::new(args), None, pos);
Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs_expr), pos)) Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs_expr), pos))
} }
@ -1337,65 +1355,89 @@ fn parse_binary_op<'a>(
let cmp_default = Some(Box::new(false.into())); let cmp_default = Some(Box::new(false.into()));
current_lhs = match op_token { current_lhs = match op_token {
Token::Plus => { Token::Plus => Expr::FnCall(
Expr::FunctionCall("+".into(), Box::new(vec![current_lhs, rhs]), None, pos) Box::new("+".into()),
} Box::new(vec![current_lhs, rhs]),
Token::Minus => { None,
Expr::FunctionCall("-".into(), Box::new(vec![current_lhs, rhs]), None, pos) pos,
} ),
Token::Multiply => { Token::Minus => Expr::FnCall(
Expr::FunctionCall("*".into(), Box::new(vec![current_lhs, rhs]), None, pos) Box::new("-".into()),
} Box::new(vec![current_lhs, rhs]),
Token::Divide => { None,
Expr::FunctionCall("/".into(), Box::new(vec![current_lhs, rhs]), None, pos) pos,
} ),
Token::Multiply => Expr::FnCall(
Box::new("*".into()),
Box::new(vec![current_lhs, rhs]),
None,
pos,
),
Token::Divide => Expr::FnCall(
Box::new("/".into()),
Box::new(vec![current_lhs, rhs]),
None,
pos,
),
Token::LeftShift => { Token::LeftShift => Expr::FnCall(
Expr::FunctionCall("<<".into(), Box::new(vec![current_lhs, rhs]), None, pos) Box::new("<<".into()),
} Box::new(vec![current_lhs, rhs]),
Token::RightShift => { None,
Expr::FunctionCall(">>".into(), Box::new(vec![current_lhs, rhs]), None, pos) pos,
} ),
Token::Modulo => { Token::RightShift => Expr::FnCall(
Expr::FunctionCall("%".into(), Box::new(vec![current_lhs, rhs]), None, pos) Box::new(">>".into()),
} Box::new(vec![current_lhs, rhs]),
Token::PowerOf => { None,
Expr::FunctionCall("~".into(), Box::new(vec![current_lhs, rhs]), None, pos) pos,
} ),
Token::Modulo => Expr::FnCall(
Box::new("%".into()),
Box::new(vec![current_lhs, rhs]),
None,
pos,
),
Token::PowerOf => Expr::FnCall(
Box::new("~".into()),
Box::new(vec![current_lhs, rhs]),
None,
pos,
),
// Comparison operators default to false when passed invalid operands // Comparison operators default to false when passed invalid operands
Token::EqualsTo => Expr::FunctionCall( Token::EqualsTo => Expr::FnCall(
"==".into(), Box::new("==".into()),
Box::new(vec![current_lhs, rhs]), Box::new(vec![current_lhs, rhs]),
cmp_default, cmp_default,
pos, pos,
), ),
Token::NotEqualsTo => Expr::FunctionCall( Token::NotEqualsTo => Expr::FnCall(
"!=".into(), Box::new("!=".into()),
Box::new(vec![current_lhs, rhs]), Box::new(vec![current_lhs, rhs]),
cmp_default, cmp_default,
pos, pos,
), ),
Token::LessThan => Expr::FunctionCall( Token::LessThan => Expr::FnCall(
"<".into(), Box::new("<".into()),
Box::new(vec![current_lhs, rhs]), Box::new(vec![current_lhs, rhs]),
cmp_default, cmp_default,
pos, pos,
), ),
Token::LessThanEqualsTo => Expr::FunctionCall( Token::LessThanEqualsTo => Expr::FnCall(
"<=".into(), Box::new("<=".into()),
Box::new(vec![current_lhs, rhs]), Box::new(vec![current_lhs, rhs]),
cmp_default, cmp_default,
pos, pos,
), ),
Token::GreaterThan => Expr::FunctionCall( Token::GreaterThan => Expr::FnCall(
">".into(), Box::new(">".into()),
Box::new(vec![current_lhs, rhs]), Box::new(vec![current_lhs, rhs]),
cmp_default, cmp_default,
pos, pos,
), ),
Token::GreaterThanEqualsTo => Expr::FunctionCall( Token::GreaterThanEqualsTo => Expr::FnCall(
">=".into(), Box::new(">=".into()),
Box::new(vec![current_lhs, rhs]), Box::new(vec![current_lhs, rhs]),
cmp_default, cmp_default,
pos, pos,
@ -1403,15 +1445,24 @@ fn parse_binary_op<'a>(
Token::Or => Expr::Or(Box::new(current_lhs), Box::new(rhs), pos), Token::Or => Expr::Or(Box::new(current_lhs), Box::new(rhs), pos),
Token::And => Expr::And(Box::new(current_lhs), Box::new(rhs), pos), Token::And => Expr::And(Box::new(current_lhs), Box::new(rhs), pos),
Token::Ampersand => { Token::Ampersand => Expr::FnCall(
Expr::FunctionCall("&".into(), Box::new(vec![current_lhs, rhs]), None, pos) Box::new("&".into()),
} Box::new(vec![current_lhs, rhs]),
Token::Pipe => { None,
Expr::FunctionCall("|".into(), Box::new(vec![current_lhs, rhs]), None, pos) pos,
} ),
Token::XOr => { Token::Pipe => Expr::FnCall(
Expr::FunctionCall("^".into(), Box::new(vec![current_lhs, rhs]), None, pos) Box::new("|".into()),
} Box::new(vec![current_lhs, rhs]),
None,
pos,
),
Token::XOr => Expr::FnCall(
Box::new("^".into()),
Box::new(vec![current_lhs, rhs]),
None,
pos,
),
Token::In => make_in_expr(current_lhs, rhs, pos)?, Token::In => make_in_expr(current_lhs, rhs, pos)?,
@ -1590,7 +1641,7 @@ fn parse_for<'a>(
stack.rewind(prev_len); stack.rewind(prev_len);
Ok(Stmt::For(name, Box::new(expr), Box::new(body))) Ok(Stmt::For(Box::new(name), Box::new(expr), Box::new(body)))
} }
/// Parse a variable definition statement. /// Parse a variable definition statement.
@ -1619,12 +1670,12 @@ fn parse_let<'a>(
// let name = expr // let name = expr
ScopeEntryType::Normal => { ScopeEntryType::Normal => {
stack.push(name.clone()); stack.push(name.clone());
Ok(Stmt::Let(name, Some(Box::new(init_value)), pos)) Ok(Stmt::Let(Box::new(name), Some(Box::new(init_value)), pos))
} }
// const name = { expr:constant } // const name = { expr:constant }
ScopeEntryType::Constant if init_value.is_constant() => { ScopeEntryType::Constant if init_value.is_constant() => {
stack.push(name.clone()); stack.push(name.clone());
Ok(Stmt::Const(name, Box::new(init_value), pos)) Ok(Stmt::Const(Box::new(name), Box::new(init_value), pos))
} }
// const name = expr - error // const name = expr - error
ScopeEntryType::Constant => { ScopeEntryType::Constant => {
@ -1633,7 +1684,7 @@ fn parse_let<'a>(
} }
} else { } else {
// let name // let name
Ok(Stmt::Let(name, None, pos)) Ok(Stmt::Let(Box::new(name), None, pos))
} }
} }