1# Tuning Wasmtime for Fast Instantiation 2 3Before a WebAssembly module can begin execution, it must first be compiled and 4then instantiated. Compilation can happen [ahead of 5time](./examples-pre-compiling-wasm.md), which removes compilation from the 6critical path. That leaves just instantiation on the critical path. This page 7documents methods for tuning Wasmtime for fast instantiation. 8 9## Enable the Pooling Allocator 10 11By enabling the pooling allocator, you are configuring Wasmtime to up-front and 12ahead-of-time allocate a large pool containing all the resources necessary to 13run the configured maximum number of concurrent instances. Creating a new 14instance doesn't require allocating new Wasm memories and tables on demand, it 15just takes pre-allocated memories and tables from the pool, which is generally 16much faster. Deallocating an instance returns its memories and tables to the 17pool. 18 19See 20[`wasmtime::PoolingAllocationConfig`](https://docs.rs/wasmtime/latest/wasmtime/struct.PoolingAllocationConfig.html), 21[`wasmtime::InstanceAllocationStrategy`](https://docs.rs/wasmtime/latest/wasmtime/enum.InstanceAllocationStrategy.html), 22and 23[`wasmtime::Config::allocation_strategy`](https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.allocation_strategy) 24for more details. 25 26## Enable Copy-on-Write Heap Images 27 28Initializing a WebAssembly linear memory via a copy-on-write mapping can 29drastically improve instantiation costs because copying memory is deferred from 30instantiation time to when the data is first mutated. When the Wasm module only 31reads the initial data, and never overwrites it, then the copying is completely 32avoided. 33 34See the API docs for 35[`wasmtime::Config::memory_init_cow`](https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.memory_init_cow) 36for more details. 37 38## Use `InstancePre` 39 40To instantiate a WebAssembly module or component, Wasmtime must look up each of 41the module's imports and check that they are of the expected types. If the 42imports are always the same, this work can be done ahead of time, before 43instantiation. A `wasmtime::InstancePre` represents an instance *just before* it 44is instantiated, after all type-checking and imports have been resolved. The 45only thing left to do for this instance is to actually allocate its memories, 46tables, and internal runtime context, initialize its state, and run its `start` 47function, if any. 48 49See the API docs for 50[`wasmtime::InstancePre`](https://docs.rs/wasmtime/latest/wasmtime/struct.InstancePre.html), 51[`wasmtime::Linker::instantiate_pre`](https://docs.rs/wasmtime/latest/wasmtime/struct.Linker.html#method.instantiate_pre), 52[`wasmtime::component::InstancePre`](https://docs.rs/wasmtime/latest/wasmtime/component/struct.InstancePre.html), 53and 54[`wasmtime::component::Linker::instantiate_pre`](https://docs.rs/wasmtime/latest/wasmtime/component/struct.Linker.html#method.instantiate_pre) 55for more details. 56 57## Putting It All Together 58 59```rust,ignore 60{{#include ../examples/fast_instantiation.rs}} 61``` 62 63## See Also 64 65* [Pre-Compiling Wasm Programs](./examples-pre-compiling-wasm.md) 66* [Tuning Wasmtime for Fast Wasm Compilation](./examples-fast-compilation.md) 67* [Tuning Wasmtime for Fast Wasm Execution](./examples-fast-execution.md) 68* [Wizer, the WebAssembly Pre-Initializer](https://github.com/bytecodealliance/wizer) 69