1# Tuning Wasmtime for Fast Wasm Execution
2
3To tune Wasmtime for faster Wasm execution, consider the following tips.
4
5## Enable Cranelift
6
7[Cranelift](https://cranelift.dev/) is an optimizing compiler. Compared to
8alternative strategies like [the Winch "baseline"
9compiler](./examples-fast-compilation.md), it translates Wasm into faster
10machine code, but compilation is slower. Cranelift is similar to the optimizing
11tier of browsers' just-in-time Wasm engines, such as SpiderMonkey's Ion tier or
12V8's TurboFan tier.
13
14See the API docs for
15[`wasmtime::Strategy`](https://docs.rs/wasmtime/latest/wasmtime/enum.Strategy.html)
16and
17[`wasmtime::Config::strategy`](https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.strategy)
18for more details.
19
20## Configure Wasmtime to Elide Explicit Bounds Checks
21
22Wasm programs are sandboxed and may only access their linear memories. Attempts
23to access beyond the bounds of a linear memory results in a trap, and this
24prevents the Wasm guest from stealing data from host memory, or from another
25concurrently running Wasm instance. Explicitly bounds-checking every linear
26memory operation performed by a Wasm guest is expensive: it has been measured to
27create between a 1.2x to 1.8x slow down, depending on a number of
28factors. Luckily, Wasmtime can usually omit explicit bounds checks by relying on
29virtual memory guard pages. This requires enabling signals-based traps (on by
30default for non-bare-metal builds), running Wasm on a 64-bit host architecture,
31and ensuring that memory reservations and guard pages are appropriately sized
32(again, configured by default for 64-bit architectures).
33
34To elide any explicit bounds checks, Wasm linear memories must have at least a
354GiB (`1 << 32` bytes) reservation. If a memory instruction has an additional
36static offset immediate, then the bounds check can only be elided when there is
37a memory guard of at least that offset's size. Using a 4GiB guard region allows
38Wasmtime to elide explicit bounds checks regardless of the static memory offset
39immediate. While small static offset immediate values are common, very large
40values are exceedingly rare, so you can get almost all of the benefits while
41consuming less virtual memory address space by using, for example, 32MiB guards.
42
43See the API docs for
44[`wasmtime::Config::signals_based_traps`](https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.signals_based_traps),
45[`wasmtime::Config::memory_reservation`](https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.memory_reservation),
46and
47[`wasmtime::Config::memory_guard_size`](https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.memory_guard_size)
48for more details.
49
50## Force-Enable ISA Extensions
51
52This section can be ignored if you are compiling and running Wasm programs on
53the same machine. In this scenario, Wasmtime will automatically detect which ISA
54extensions (such as AVX on x86\_64) are available, and you do not need to
55configure anything yourself.
56
57However, if you are compiling a Wasm program on one machine and then running
58that pre-compiled Wasm program on another machine, then during compilation
59Wasmtime cannot automatically detect which ISA extensions will be available on
60the machine on which you actually execute the pre-compiled Wasm
61program. Configuring which ISA extensions are available on the target
62architecture that will run the pre-compiled Wasm programs can have a large
63impact for certain Wasm programs, particularly those using SIMD instructions.
64
65See the API docs for
66[`wasmtime::Config::cranelift_flag_enable`](https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.cranelift_flag_enable)
67for more details.
68
69## Putting It All Together
70
71```rust,ignore
72{{#include ../examples/fast_execution.rs}}
73```
74
75## See Also
76
77* [Tuning Wasmtime for Fast Wasm Compilation](./examples-fast-compilation.md)
78* [Tuning Wasmtime for Fast Wasm Instantiation](./examples-fast-instantiation.md)
79* [Pre-Compiling Wasm Programs](./examples-pre-compiling-wasm.md)
80