This repository has been archived on 2025-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
rhaj/_archive/rhai_engine/rhaibook/engine/optimize/rewrite.md
2025-04-04 08:28:07 +02:00

1.1 KiB

Compound Assignment Rewrite

{{#include ../../links.md}}


Arguments passed as value are always cloned.

Usually, a compound assignment (e.g. += for append) takes a mutable first parameter (i.e. &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_](https://en.wikipedia.org/wiki/Common_subexpression_elimination)
is performed by Rhai.

```rust
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
```