docs/examples-fast-instantiation.md
Before a WebAssembly module can begin execution, it must first be compiled and then instantiated. Compilation can happen ahead of time, which removes compilation from the critical path. That leaves just instantiation on the critical path. This page documents methods for tuning Wasmtime for fast instantiation.
By enabling the pooling allocator, you are configuring Wasmtime to up-front and ahead-of-time allocate a large pool containing all the resources necessary to run the configured maximum number of concurrent instances. Creating a new instance doesn't require allocating new Wasm memories and tables on demand, it just takes pre-allocated memories and tables from the pool, which is generally much faster. Deallocating an instance returns its memories and tables to the pool.
See
wasmtime::PoolingAllocationConfig,
wasmtime::InstanceAllocationStrategy,
and
wasmtime::Config::allocation_strategy
for more details.
Initializing a WebAssembly linear memory via a copy-on-write mapping can drastically improve instantiation costs because copying memory is deferred from instantiation time to when the data is first mutated. When the Wasm module only reads the initial data, and never overwrites it, then the copying is completely avoided.
See the API docs for
wasmtime::Config::memory_init_cow
for more details.
InstancePreTo instantiate a WebAssembly module or component, Wasmtime must look up each of
the module's imports and check that they are of the expected types. If the
imports are always the same, this work can be done ahead of time, before
instantiation. A wasmtime::InstancePre represents an instance just before it
is instantiated, after all type-checking and imports have been resolved. The
only thing left to do for this instance is to actually allocate its memories,
tables, and internal runtime context, initialize its state, and run its start
function, if any.
See the API docs for
wasmtime::InstancePre,
wasmtime::Linker::instantiate_pre,
wasmtime::component::InstancePre,
and
wasmtime::component::Linker::instantiate_pre
for more details.
{{#include ../examples/fast_instantiation.rs}}