rhai/doc/src/safety/max-operations.md

45 lines
1.9 KiB
Markdown
Raw Normal View History

2020-06-20 06:06:17 +02:00
Maximum Number of Operations
===========================
{{#include ../links.md}}
2020-10-11 15:58:11 +02:00
2020-07-16 06:09:40 +02:00
Limit How Long a Script Can Run
------------------------------
2020-06-20 06:06:17 +02:00
Rhai by default does not limit how much time or CPU a script consumes.
This can be changed via the `Engine::set_max_operations` method, with zero being unlimited (the default).
The _operations count_ is intended to be a very course-grained measurement of the amount of CPU that a script
has consumed, allowing the system to impose a hard upper limit on computing resources.
A script exceeding the maximum operations count terminates with an error result.
This can be disabled via the [`unchecked`] feature for higher performance (but higher risks as well).
```rust
let mut engine = Engine::new();
engine.set_max_operations(500); // allow only up to 500 operations for this script
engine.set_max_operations(0); // allow unlimited operations
```
What Does One _Operation_ Mean
-----------------------------
2020-12-29 16:01:34 +01:00
The concept of one single _operation_ in Rhai is volatile – it roughly equals one expression node,
2020-06-20 06:06:17 +02:00
loading one variable/constant, one operator call, one iteration of a loop, or one function call etc.
with sub-expressions, statements and function calls executed inside these contexts accumulated on top.
A good rule-of-thumb is that one simple non-trivial expression consumes on average 5-10 operations.
One _operation_ can take an unspecified amount of time and real CPU cycles, depending on the particulars.
For example, loading a constant consumes very few CPU cycles, while calling an external Rust function,
though also counted as only one operation, may consume much more computing resources.
To help visualize, think of an _operation_ as roughly equals to one _instruction_ of a hypothetical CPU
which includes _specialized_ instructions, such as _function call_, _load module_ etc., each taking up
one CPU cycle to execute.