154988febSNick Fitzgerald# Pre-Compiling and Cross-Compiling WebAssembly Programs 254988febSNick Fitzgerald 354988febSNick FitzgeraldWasmtime can compile a WebAssembly program to native code on one machine, and 454988febSNick Fitzgeraldthen run it on a different machine. This has a number of benefits: 554988febSNick Fitzgerald 654988febSNick Fitzgerald* **Faster start up:** Compilation is removed from the critical path. When a new 754988febSNick Fitzgerald HTTP request comes into your function-as-a-service platform, for example, you 854988febSNick Fitzgerald do not have to wait for the associated Wasm program to compile before it can 954988febSNick Fitzgerald start handling the request. Similarly, when a new update for your embedded 1054988febSNick Fitzgerald device's Wasm application logic comes in, you do not need to compile the 1154988febSNick Fitzgerald update on the under-powered device before it can begin running new updated 1254988febSNick Fitzgerald logic. 1354988febSNick Fitzgerald 1454988febSNick Fitzgerald* **Less Memory Usage:** Pre-compiled Wasm programs can be lazily `mmap`ed from 1554988febSNick Fitzgerald disk, only paging their code into memory as those code paths are executed. If 1654988febSNick Fitzgerald none of the code on a page is ever executed, the OS will never make the page 1754988febSNick Fitzgerald resident. This means that running pre-compiled Wasm programs lowers overall 1854988febSNick Fitzgerald memory usage in the system. 1954988febSNick Fitzgerald 2054988febSNick Fitzgerald* **Smaller Code Size for Embedded:** Wasmtime can be built such that it can 2154988febSNick Fitzgerald *only* run Wasm programs that were pre-compiled elsewhere. These builds will 2254988febSNick Fitzgerald not include the executable code for Wasm compilation. This is done by 2354988febSNick Fitzgerald disabling the `cranelift` and `winch` cargo features at build time. These 2454988febSNick Fitzgerald builds are useful for embedded devices, where programs must be small and fit 2554988febSNick Fitzgerald within the device's constrained environment. 2654988febSNick Fitzgerald 2754988febSNick Fitzgerald* **Smaller Attack Surfaces:** Similarly, building Wasmtime without a compiler, 2854988febSNick Fitzgerald and with only support for running pre-compiled Wasm programs, can be useful 2954988febSNick Fitzgerald for security-minded embeddings to reduce the potential attack surface exposed 3054988febSNick Fitzgerald to untrusted and potentially hostile Wasm guests. Compilation, triggered by 3154988febSNick Fitzgerald the control plane, can happen inside a Wasmtime build that can compile but not 3254988febSNick Fitzgerald run Wasm programs. Execution, in the data plane, can happen inside a Wasmtime 3354988febSNick Fitzgerald build that can run but not compile new Wasm programs. Exposing a minimal 3454988febSNick Fitzgerald attack surface to untrusted code is good security practice. 3554988febSNick Fitzgerald 3654988febSNick FitzgeraldNote that these benefits are applicable regardless which Wasm execution strategy 3754988febSNick Fitzgeraldyou've configured: Cranelift, Winch, or Pulley. 3854988febSNick Fitzgerald 3954988febSNick Fitzgerald## Pre-Compile the Wasm on One Machine 4054988febSNick Fitzgerald 4154988febSNick FitzgeraldThis must be done with a Wasmtime build that has a Wasm execution strategy 4254988febSNick Fitzgeraldenabled, e.g. was built with the `cranelift` or `winch` cargo features. It does 4354988febSNick Fitzgeraldnot require the ability to run Wasm programs, so the `runtime` cargo feature can 4454988febSNick Fitzgeraldbe disabled at build time. 4554988febSNick Fitzgerald 4654988febSNick Fitzgerald```rust,ignore 4754988febSNick Fitzgerald{{#include ../examples/pre_compile.rs}} 4854988febSNick Fitzgerald``` 4954988febSNick Fitzgerald 5054988febSNick Fitzgerald## Run the Pre-Compiled Wasm on Another Machine 5154988febSNick Fitzgerald 5254988febSNick FitzgeraldThis must be done with a Wasmtime build that can run pre-compiled Wasm programs, 5354988febSNick Fitzgeraldthat is a Wasmtime built with the `runtime` cargo feature. It does not need to 5454988febSNick Fitzgeraldcompile new Wasm programs, so the `cranelift` and `winch` cargo features can be 5554988febSNick Fitzgeralddisabled. 5654988febSNick Fitzgerald 5754988febSNick Fitzgerald```rust,ignore 5854988febSNick Fitzgerald{{#include ../examples/run_pre_compiled.rs}} 5954988febSNick Fitzgerald``` 6054988febSNick Fitzgerald 6154988febSNick Fitzgerald## See Also 6254988febSNick Fitzgerald 6354988febSNick Fitzgerald* [Tuning Wasmtime for Fast Wasm Instantiation](./examples-fast-instantiation.md) 6454988febSNick Fitzgerald* [Tuning Wasmtime for Fast Wasm Execution](./examples-fast-execution.md) 6554988febSNick Fitzgerald* [Building a Minimal Wasmtime Embedding](./examples-minimal.md) 6654988febSNick Fitzgerald* [`wasmtime::Engine::precompile_module`](https://docs.rs/wasmtime/latest/wasmtime/struct.Engine.html#method.precompile_module) 6754988febSNick Fitzgerald and 6854988febSNick Fitzgerald [`wasmtime::Engine::precompile_component`](https://docs.rs/wasmtime/latest/wasmtime/struct.Engine.html#method.precompile_component) 6954988febSNick Fitzgerald* [`wasmtime::Module::deserialize`](https://docs.rs/wasmtime/latest/wasmtime/struct.Module.html#method.deserialize), 7054988febSNick Fitzgerald [`wasmtime::Module::deserialize_file`](https://docs.rs/wasmtime/latest/wasmtime/struct.Module.html#method.deserialize_file), 71*3fcee695SPat Hickey [`wasmtime::component::Component::deserialize`](https://docs.rs/wasmtime/latest/wasmtime/component/struct.Component.html#method.deserialize), 7254988febSNick Fitzgerald and 73*3fcee695SPat Hickey [`wasmtime::component::Component::deserialize_file`](https://docs.rs/wasmtime/latest/wasmtime/component/struct.Component.html#method.deserialize_file) 74