Avoid copying parameters for function calls.

This commit is contained in:
Stephen Chung 2020-05-04 10:41:58 +08:00
parent 3d05cc96cd
commit 7da9d265d3

View File

@ -628,11 +628,14 @@ impl Engine {
// Put arguments into scope as variables - variable name is copied // Put arguments into scope as variables - variable name is copied
scope.extend( scope.extend(
// TODO - avoid copying variable name
fn_def fn_def
.params .params
.iter() .iter()
.zip(args.into_iter().map(|v| v.clone())) .zip(
// Actually consume the arguments instead of cloning them
args.into_iter()
.map(|v| mem::replace(*v, Default::default())),
)
.map(|(name, value)| (name.clone(), ScopeEntryType::Normal, value)), .map(|(name, value)| (name.clone(), ScopeEntryType::Normal, value)),
); );
@ -659,7 +662,11 @@ impl Engine {
fn_def fn_def
.params .params
.iter() .iter()
.zip(args.into_iter().map(|v| v.clone())) .zip(
// Actually consume the arguments instead of cloning them
args.into_iter()
.map(|v| mem::replace(*v, Default::default())),
)
.map(|(name, value)| (name, ScopeEntryType::Normal, value)), .map(|(name, value)| (name, ScopeEntryType::Normal, value)),
); );
@ -884,6 +891,7 @@ impl Engine {
if may_be_changed { if may_be_changed {
if let Expr::Property(id, pos) = dot_lhs.as_ref() { if let Expr::Property(id, pos) = dot_lhs.as_ref() {
let fn_name = make_setter(id); let fn_name = make_setter(id);
// Reuse args because the first &mut parameter will not be consumed
args[1] = indexed_val; args[1] = indexed_val;
self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0)?; self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0)?;
} }