Fast Compilation - Wasmtime

## Keyboard shortcuts

Press `←` or `→` to navigate between chapters

Press `S` or `/` to search in the book

Press `?` to show this help

Press `Esc` to hide this help

# Tuning Wasmtime for Fast Compilation

Wasmtime must compile a Wasm program before executing it. This means that, by default, Wasm compilation is on your critical path. In most scenarios, you can completely remove Wasm compilation from the critical path by pre-compiling Wasm programs. That option is not always available, however, and this page documents how to tune Wasmtime for fast compilation in these alternative scenarios.

## Enable the Compilation Cache

Wasmtime can be configured to use a cache, so that if you attempt to compile a Wasm program that has already been compiled previously, it just grabs the cached result rather than performing compilation all over again.

See these API docs for more details:

- `wasmtime::Config::cache`
- `wasmtime::CacheStore`

## Enable Winch

Winch is Wasmtime's "baseline" compiler: for each Wasm opcode, it emits a canned sequence of machine instructions to implement that opcode. This makes compilation fast: it performs only a single, quick pass over the Wasm code. However, it does not perform optimizations, so the machine code it emits will run Wasm programs slower than Cranelift, Wasmtime's optimizing compiler.

See the API docs for `wasmtime::Strategy` and `wasmtime::Config::strategy` for more details.

## Enable Parallel Compilation

Wasmtime can compile Wasm programs in parallel, speeding up the compilation process more or less depending on how many cores your machine has and the exact shape of the Wasm program. Wasmtime will generally enable parallel compilation by default, but it does depend on the host platform and cargo features enabled when building Wasmtime itself. You can double check that parallel compilation is enabled via the setting the `wasmtime::Config::parallel_compilation` configuration option.

## Putting It All Together

    //! Tuning Wasmtime for fast compilation.
    //!
    //! If your application design is compatible with pre-compiling Wasm programs,
    //! prefer doing that.

    use wasmtime::{Cache, Config, Engine, Result, Strategy};

    fn main() -> Result<()> {
        let mut config = Config::new();

        // Enable the compilation cache, using the default cache configuration
        // settings.
        config.cache(Some(Cache::from_file(None)?));

        // Enable Winch, Wasmtime's baseline compiler.
        config.strategy(Strategy::Winch);

        // Enable parallel compilation.
        config.parallel_compilation(true);

        // Build an `Engine` with our `Config` that is tuned for fast Wasm
        // compilation.
        let engine = Engine::new(&config)?;

        // Now we can use `engine` to compile and/or run some Wasm programs...

        let _ = engine;
        Ok(())
    }

## See Also

- Pre-Compiling Wasm Programs
- Tuning Wasmtime for Fast Wasm Instantiation
- Tuning Wasmtime for Fast Wasm Execution