Usually, a compound assignment (e.g.
+= for append) takes a mutable first parameter
&mut) while the corresponding simple operator (i.e.
+) does not.
The script optimizer rewrites normal assignments into compound assignments wherever possible in order to avoid unnecessary cloning.
let big = create_some_very_big_type(); big = big + 1; // ^ 'big' is cloned here // The above is equivalent to: let temp_value = big + 1; big = temp_value; big += 1; // <- 'big' is NOT cloned
Only simple variable references are optimized.
No common sub-expression elimination is performed by Rhai.
x = x + 1; // <- this statement... x += 1; // <- ... is rewritten to this x[y] = x[y] + 1; // <- but this is not, // so MUCH slower... x[y] += 1; // <- ... than this