154988febSNick Fitzgerald# Tuning Wasmtime for Fast Compilation
254988febSNick Fitzgerald
354988febSNick FitzgeraldWasmtime must compile a Wasm program before executing it. This means that, by
454988febSNick Fitzgeralddefault, Wasm compilation is on your critical path. In most scenarios, you can
554988febSNick Fitzgeraldcompletely remove Wasm compilation from the critical path by [pre-compiling Wasm
654988febSNick Fitzgeraldprograms](./examples-pre-compiling-wasm.md). That option is not always
754988febSNick Fitzgeraldavailable, however, and this page documents how to tune Wasmtime for fast
854988febSNick Fitzgeraldcompilation in these alternative scenarios.
954988febSNick Fitzgerald
1054988febSNick Fitzgerald## Enable the Compilation Cache
1154988febSNick Fitzgerald
1254988febSNick FitzgeraldWasmtime can be configured to use a cache, so that if you attempt to compile a
1354988febSNick FitzgeraldWasm program that has already been compiled previously, it just grabs the cached
1454988febSNick Fitzgeraldresult rather than performing compilation all over again.
1554988febSNick Fitzgerald
1654988febSNick FitzgeraldSee these API docs for more details:
1754988febSNick Fitzgerald
18*0ffa7455SBen Brandt* [`wasmtime::Config::cache`](https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.cache)
1954988febSNick Fitzgerald* [`wasmtime::CacheStore`](https://docs.rs/wasmtime/latest/wasmtime/trait.CacheStore.html)
2054988febSNick Fitzgerald
2154988febSNick Fitzgerald## Enable Winch
2254988febSNick Fitzgerald
2354988febSNick FitzgeraldWinch is Wasmtime's "baseline" compiler: for each Wasm opcode, it emits a canned
2454988febSNick Fitzgeraldsequence of machine instructions to implement that opcode. This makes
2554988febSNick Fitzgeraldcompilation fast: it performs only a single, quick pass over the Wasm
2654988febSNick Fitzgeraldcode. However, it does not perform optimizations, so the machine code it emits
2754988febSNick Fitzgeraldwill run Wasm programs slower than Cranelift, Wasmtime's optimizing compiler.
2854988febSNick Fitzgerald
2954988febSNick FitzgeraldSee the API docs for
3054988febSNick Fitzgerald[`wasmtime::Strategy`](https://docs.rs/wasmtime/latest/wasmtime/enum.Strategy.html)
3154988febSNick Fitzgeraldand
3254988febSNick Fitzgerald[`wasmtime::Config::strategy`](https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.strategy)
3354988febSNick Fitzgeraldfor more details.
3454988febSNick Fitzgerald
3554988febSNick Fitzgerald## Enable Parallel Compilation
3654988febSNick Fitzgerald
3754988febSNick FitzgeraldWasmtime can compile Wasm programs in parallel, speeding up the compilation
3854988febSNick Fitzgeraldprocess more or less depending on how many cores your machine has and the exact
3954988febSNick Fitzgeraldshape of the Wasm program. Wasmtime will generally enable parallel compilation
4054988febSNick Fitzgeraldby default, but it does depend on the host platform and cargo features enabled
4154988febSNick Fitzgeraldwhen building Wasmtime itself. You can double check that parallel compilation is
4254988febSNick Fitzgeraldenabled via the setting the
4354988febSNick Fitzgerald[`wasmtime::Config::parallel_compilation`](https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.parallel_compilation)
4454988febSNick Fitzgeraldconfiguration option.
4554988febSNick Fitzgerald
4654988febSNick Fitzgerald## Putting It All Together
4754988febSNick Fitzgerald
4854988febSNick Fitzgerald```rust,ignore
4954988febSNick Fitzgerald{{#include ../examples/fast_compilation.rs}}
5054988febSNick Fitzgerald```
5154988febSNick Fitzgerald
5254988febSNick Fitzgerald## See Also
5354988febSNick Fitzgerald
5454988febSNick Fitzgerald* [Pre-Compiling Wasm Programs](./examples-pre-compiling-wasm.md)
5554988febSNick Fitzgerald* [Tuning Wasmtime for Fast Wasm Instantiation](./examples-fast-instantiation.md)
5654988febSNick Fitzgerald* [Tuning Wasmtime for Fast Wasm Execution](./examples-fast-execution.md)
57