Use iterators.
This commit is contained in:
parent
faa48f78c3
commit
ca02d46a49
@ -229,7 +229,7 @@ fn optimize_stmt_block(
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Optimize each statement in the block
|
// Optimize each statement in the block
|
||||||
for stmt in &mut statements {
|
statements.iter_mut().for_each(|stmt| {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::Var(x, options, ..) => {
|
Stmt::Var(x, options, ..) => {
|
||||||
if options.contains(ASTFlags::CONSTANT) {
|
if options.contains(ASTFlags::CONSTANT) {
|
||||||
@ -252,7 +252,7 @@ fn optimize_stmt_block(
|
|||||||
// Optimize the statement
|
// Optimize the statement
|
||||||
_ => optimize_stmt(stmt, state, preserve_result),
|
_ => optimize_stmt(stmt, state, preserve_result),
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// Remove all pure statements except the last one
|
// Remove all pure statements except the last one
|
||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
@ -626,11 +626,11 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: b
|
|||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
for r in &*ranges {
|
ranges.iter().for_each(|r| {
|
||||||
let b = &mut expressions[r.index()];
|
let b = &mut expressions[r.index()];
|
||||||
optimize_expr(&mut b.condition, state, false);
|
optimize_expr(&mut b.condition, state, false);
|
||||||
optimize_expr(&mut b.expr, state, false);
|
optimize_expr(&mut b.expr, state, false);
|
||||||
}
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -663,7 +663,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: b
|
|||||||
optimize_expr(match_expr, state, false);
|
optimize_expr(match_expr, state, false);
|
||||||
|
|
||||||
// Optimize blocks
|
// Optimize blocks
|
||||||
for b in expressions.as_mut() {
|
expressions.iter_mut().for_each(|b| {
|
||||||
optimize_expr(&mut b.condition, state, false);
|
optimize_expr(&mut b.condition, state, false);
|
||||||
optimize_expr(&mut b.expr, state, false);
|
optimize_expr(&mut b.expr, state, false);
|
||||||
|
|
||||||
@ -671,7 +671,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: b
|
|||||||
b.expr = Expr::Unit(b.expr.position());
|
b.expr = Expr::Unit(b.expr.position());
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// Remove false cases
|
// Remove false cases
|
||||||
cases.retain(|_, list| {
|
cases.retain(|_, list| {
|
||||||
@ -718,12 +718,12 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: b
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove unused block statements
|
// Remove unused block statements
|
||||||
for index in 0..expressions.len() {
|
(0..expressions.len()).into_iter().for_each(|index| {
|
||||||
if *def_case == Some(index)
|
if *def_case == Some(index)
|
||||||
|| cases.values().flat_map(|c| c.iter()).any(|&n| n == index)
|
|| cases.values().flat_map(|c| c.iter()).any(|&n| n == index)
|
||||||
|| ranges.iter().any(|r| r.index() == index)
|
|| ranges.iter().any(|r| r.index() == index)
|
||||||
{
|
{
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let b = &mut expressions[index];
|
let b = &mut expressions[index];
|
||||||
@ -732,7 +732,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: b
|
|||||||
b.expr = Expr::Unit(b.expr.position());
|
b.expr = Expr::Unit(b.expr.position());
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// while false { block } -> Noop
|
// while false { block } -> Noop
|
||||||
@ -1159,8 +1159,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
|
|||||||
x.args.iter_mut().for_each(|a| optimize_expr(a, state, false));
|
x.args.iter_mut().for_each(|a| optimize_expr(a, state, false));
|
||||||
|
|
||||||
// Move constant arguments
|
// Move constant arguments
|
||||||
for arg in &mut x.args {
|
x.args.iter_mut().for_each(|arg| match arg {
|
||||||
match arg {
|
|
||||||
Expr::DynamicConstant(..) | Expr::Unit(..)
|
Expr::DynamicConstant(..) | Expr::Unit(..)
|
||||||
| Expr::StringConstant(..) | Expr::CharConstant(..)
|
| Expr::StringConstant(..) | Expr::CharConstant(..)
|
||||||
| Expr::BoolConstant(..) | Expr::IntegerConstant(..) => (),
|
| Expr::BoolConstant(..) | Expr::IntegerConstant(..) => (),
|
||||||
@ -1172,8 +1171,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
|
|||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
*arg = Expr::DynamicConstant(value.into(), arg.start_position());
|
*arg = Expr::DynamicConstant(value.into(), arg.start_position());
|
||||||
},
|
},
|
||||||
}
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Eagerly call functions
|
// Eagerly call functions
|
||||||
@ -1209,7 +1207,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// id(args ..) or xxx.id(args ..) -> optimize function call arguments
|
// id(args ..) or xxx.id(args ..) -> optimize function call arguments
|
||||||
Expr::FnCall(x, ..) | Expr::MethodCall(x, ..) => for arg in &mut x.args {
|
Expr::FnCall(x, ..) | Expr::MethodCall(x, ..) => x.args.iter_mut().for_each(|arg| {
|
||||||
optimize_expr(arg, state, false);
|
optimize_expr(arg, state, false);
|
||||||
|
|
||||||
// Move constant arguments
|
// Move constant arguments
|
||||||
@ -1226,7 +1224,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
|
|||||||
*arg = Expr::DynamicConstant(value.into(), arg.start_position());
|
*arg = Expr::DynamicConstant(value.into(), arg.start_position());
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
}),
|
||||||
|
|
||||||
// constant-name
|
// constant-name
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
@ -1304,20 +1302,25 @@ impl Engine {
|
|||||||
let mut state = OptimizerState::new(self, lib, optimization_level);
|
let mut state = OptimizerState::new(self, lib, optimization_level);
|
||||||
|
|
||||||
// Add constants from global modules
|
// Add constants from global modules
|
||||||
for (name, value) in self.global_modules.iter().rev().flat_map(|m| m.iter_var()) {
|
self.global_modules
|
||||||
state.push_var(name, AccessMode::ReadOnly, Some(value.clone()));
|
.iter()
|
||||||
}
|
.rev()
|
||||||
|
.flat_map(|m| m.iter_var())
|
||||||
|
.for_each(|(name, value)| {
|
||||||
|
state.push_var(name, AccessMode::ReadOnly, Some(value.clone()))
|
||||||
|
});
|
||||||
|
|
||||||
// Add constants and variables from the scope
|
// Add constants and variables from the scope
|
||||||
if let Some(scope) = scope {
|
scope
|
||||||
for (name, constant, value) in scope.iter() {
|
.into_iter()
|
||||||
|
.flat_map(Scope::iter)
|
||||||
|
.for_each(|(name, constant, value)| {
|
||||||
if constant {
|
if constant {
|
||||||
state.push_var(name, AccessMode::ReadOnly, Some(value));
|
state.push_var(name, AccessMode::ReadOnly, Some(value));
|
||||||
} else {
|
} else {
|
||||||
state.push_var(name, AccessMode::ReadWrite, None);
|
state.push_var(name, AccessMode::ReadWrite, None);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
optimize_stmt_block(statements, &mut state, true, false, true)
|
optimize_stmt_block(statements, &mut state, true, false, true)
|
||||||
}
|
}
|
||||||
@ -1339,15 +1342,16 @@ impl Engine {
|
|||||||
let mut module = crate::Module::new();
|
let mut module = crate::Module::new();
|
||||||
|
|
||||||
if optimization_level == OptimizationLevel::None {
|
if optimization_level == OptimizationLevel::None {
|
||||||
for fn_def in functions {
|
functions.into_iter().for_each(|fn_def| {
|
||||||
module.set_script_fn(fn_def);
|
module.set_script_fn(fn_def);
|
||||||
}
|
});
|
||||||
} else {
|
} else {
|
||||||
// We only need the script library's signatures for optimization purposes
|
// We only need the script library's signatures for optimization purposes
|
||||||
let mut lib2 = crate::Module::new();
|
let mut lib2 = crate::Module::new();
|
||||||
|
|
||||||
for fn_def in &functions {
|
functions
|
||||||
lib2.set_script_fn(crate::ast::ScriptFnDef {
|
.iter()
|
||||||
|
.map(|fn_def| crate::ast::ScriptFnDef {
|
||||||
name: fn_def.name.clone(),
|
name: fn_def.name.clone(),
|
||||||
access: fn_def.access,
|
access: fn_def.access,
|
||||||
body: crate::ast::StmtBlock::NONE,
|
body: crate::ast::StmtBlock::NONE,
|
||||||
@ -1355,12 +1359,14 @@ impl Engine {
|
|||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
comments: Box::default(),
|
comments: Box::default(),
|
||||||
|
})
|
||||||
|
.for_each(|script_def| {
|
||||||
|
lib2.set_script_fn(script_def);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
let lib2 = &[lib2.into()];
|
let lib2 = &[lib2.into()];
|
||||||
|
|
||||||
for fn_def in functions {
|
functions.into_iter().for_each(|fn_def| {
|
||||||
let mut fn_def = crate::func::shared_take_or_clone(fn_def);
|
let mut fn_def = crate::func::shared_take_or_clone(fn_def);
|
||||||
|
|
||||||
// Optimize the function body
|
// Optimize the function body
|
||||||
@ -1369,7 +1375,7 @@ impl Engine {
|
|||||||
*fn_def.body = self.optimize_top_level(body, scope, lib2, optimization_level);
|
*fn_def.body = self.optimize_top_level(body, scope, lib2, optimization_level);
|
||||||
|
|
||||||
module.set_script_fn(fn_def);
|
module.set_script_fn(fn_def);
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.into()
|
module.into()
|
||||||
|
@ -1244,14 +1244,14 @@ impl Engine {
|
|||||||
if !has_condition && ranges.is_empty() && r.len() <= SMALL_SWITCH_RANGE
|
if !has_condition && ranges.is_empty() && r.len() <= SMALL_SWITCH_RANGE
|
||||||
{
|
{
|
||||||
// Unroll small range
|
// Unroll small range
|
||||||
for n in r {
|
r.into_iter().for_each(|n| {
|
||||||
let hasher = &mut get_hasher();
|
let hasher = &mut get_hasher();
|
||||||
Dynamic::from_int(n).hash(hasher);
|
Dynamic::from_int(n).hash(hasher);
|
||||||
cases
|
cases
|
||||||
.entry(hasher.finish())
|
.entry(hasher.finish())
|
||||||
.and_modify(|cases| cases.push(index))
|
.and_modify(|cases| cases.push(index))
|
||||||
.or_insert_with(|| [index].into());
|
.or_insert_with(|| [index].into());
|
||||||
}
|
});
|
||||||
} else {
|
} else {
|
||||||
// Other range
|
// Other range
|
||||||
r.set_index(index);
|
r.set_index(index);
|
||||||
@ -3983,9 +3983,9 @@ impl Engine {
|
|||||||
{
|
{
|
||||||
let mut m = crate::Module::new();
|
let mut m = crate::Module::new();
|
||||||
|
|
||||||
for fn_def in _lib {
|
_lib.into_iter().for_each(|fn_def| {
|
||||||
m.set_script_fn(fn_def);
|
m.set_script_fn(fn_def);
|
||||||
}
|
});
|
||||||
|
|
||||||
return Ok(AST::new(statements, m));
|
return Ok(AST::new(statements, m));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user