rhai/doc/src/start/builds/performance.md

61 lines
2.3 KiB
Markdown
Raw Normal View History

2020-06-20 06:06:17 +02:00
Performance Build
=================
{{#include ../../links.md}}
2020-08-04 12:39:24 +02:00
Some features are for performance. For example, using [`only_i32`] or [`only_i64`] disables all other integer types (such as `u16`).
2020-06-20 06:06:17 +02:00
Use Only One Integer Type
------------------------
If only a single integer type is needed in scripts - most of the time this is the case - it is best to avoid registering
2020-06-22 16:02:49 +02:00
lots of functions related to other integer types that will never be used. As a result, [`Engine`] creation will be faster
because fewer functions need to be loaded.
2020-06-20 06:06:17 +02:00
2020-09-19 06:14:02 +02:00
The [`only_i32`] and [`only_i64`] features disable all integer types except `i32` or `i64` respectively.
2020-06-20 06:06:17 +02:00
Use Only 32-Bit Numbers
----------------------
2020-09-19 06:14:02 +02:00
If only 32-bit integers are needed - again, most of the time this is the case - turn on [`only_i32`].
Under this feature, only `i32` is supported as a built-in integer type and no others.
2020-06-22 16:02:49 +02:00
2020-09-19 06:14:02 +02:00
On 64-bit targets this may not gain much, but on certain 32-bit targets this improves performance
due to 64-bit arithmetic requiring more CPU cycles to complete.
2020-06-20 06:06:17 +02:00
2020-08-04 12:39:24 +02:00
Minimize Size of `Dynamic`
-------------------------
2020-06-20 06:06:17 +02:00
2020-09-19 06:14:02 +02:00
Turning on [`no_float`] and [`only_i32`] on 32-bit targets makes the critical [`Dynamic`] data type only 8 bytes long.
Normally [`Dynamic`] can be up to 16 bytes (e.g. on x86/x64 CPU's) in order to hold an `i64` or `f64`.
2020-06-22 16:02:49 +02:00
2020-09-19 06:14:02 +02:00
A small [`Dynamic`] helps performance due to better cache efficiency.
2020-07-13 07:41:01 +02:00
Use `ImmutableString`
--------------------
Internally, Rhai uses _immutable_ [strings] instead of the Rust `String` type. This is mainly to avoid excessive
cloning when passing function arguments.
2020-07-26 04:07:40 +02:00
Rhai's internal string type is `ImmutableString` (basically `Rc<String>` or `Arc<String>` depending on the [`sync`] feature).
It is cheap to clone, but expensive to modify (a new copy of the string must be made in order to change it).
2020-07-13 07:41:01 +02:00
2020-09-19 06:14:02 +02:00
Therefore, functions taking `String` parameters should use `ImmutableString` or `&str` (both map to `ImmutableString`)
2020-07-13 07:41:01 +02:00
for the best performance with Rhai.
2020-08-04 12:39:24 +02:00
Disable Closures
----------------
2020-09-19 06:14:02 +02:00
Support for [closures] that capture shared variables adds material overhead to script evaluation.
2020-08-04 12:39:24 +02:00
2020-09-19 06:14:02 +02:00
This is because every data access must be checked whether it is a shared value and, if so, take a read
lock before reading it.
2020-08-04 12:39:24 +02:00
2020-09-19 06:14:02 +02:00
Use [`no_closure`] to disable closure and capturing support to optimize the hot path
2020-08-04 12:39:24 +02:00
because there is no need to take locks for shared data.