From 7da9d265d3ed4b2338d1f9b8530c3510a04f0800 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 4 May 2020 10:41:58 +0800 Subject: [PATCH] Avoid copying parameters for function calls. --- src/engine.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 8480664a..007adf40 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -628,11 +628,14 @@ impl Engine { // Put arguments into scope as variables - variable name is copied scope.extend( - // TODO - avoid copying variable name fn_def .params .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)), ); @@ -659,7 +662,11 @@ impl Engine { fn_def .params .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)), ); @@ -884,6 +891,7 @@ impl Engine { if may_be_changed { if let Expr::Property(id, pos) = dot_lhs.as_ref() { let fn_name = make_setter(id); + // Reuse args because the first &mut parameter will not be consumed args[1] = indexed_val; self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0)?; }