1# Pre-Compiling and Cross-Compiling WebAssembly Programs 2 3Wasmtime can compile a WebAssembly program to native code on one machine, and 4then run it on a different machine. This has a number of benefits: 5 6* **Faster start up:** Compilation is removed from the critical path. When a new 7 HTTP request comes into your function-as-a-service platform, for example, you 8 do not have to wait for the associated Wasm program to compile before it can 9 start handling the request. Similarly, when a new update for your embedded 10 device's Wasm application logic comes in, you do not need to compile the 11 update on the under-powered device before it can begin running new updated 12 logic. 13 14* **Less Memory Usage:** Pre-compiled Wasm programs can be lazily `mmap`ed from 15 disk, only paging their code into memory as those code paths are executed. If 16 none of the code on a page is ever executed, the OS will never make the page 17 resident. This means that running pre-compiled Wasm programs lowers overall 18 memory usage in the system. 19 20* **Smaller Code Size for Embedded:** Wasmtime can be built such that it can 21 *only* run Wasm programs that were pre-compiled elsewhere. These builds will 22 not include the executable code for Wasm compilation. This is done by 23 disabling the `cranelift` and `winch` cargo features at build time. These 24 builds are useful for embedded devices, where programs must be small and fit 25 within the device's constrained environment. 26 27* **Smaller Attack Surfaces:** Similarly, building Wasmtime without a compiler, 28 and with only support for running pre-compiled Wasm programs, can be useful 29 for security-minded embeddings to reduce the potential attack surface exposed 30 to untrusted and potentially hostile Wasm guests. Compilation, triggered by 31 the control plane, can happen inside a Wasmtime build that can compile but not 32 run Wasm programs. Execution, in the data plane, can happen inside a Wasmtime 33 build that can run but not compile new Wasm programs. Exposing a minimal 34 attack surface to untrusted code is good security practice. 35 36Note that these benefits are applicable regardless which Wasm execution strategy 37you've configured: Cranelift, Winch, or Pulley. 38 39## Pre-Compile the Wasm on One Machine 40 41This must be done with a Wasmtime build that has a Wasm execution strategy 42enabled, e.g. was built with the `cranelift` or `winch` cargo features. It does 43not require the ability to run Wasm programs, so the `runtime` cargo feature can 44be disabled at build time. 45 46```rust,ignore 47{{#include ../examples/pre_compile.rs}} 48``` 49 50## Run the Pre-Compiled Wasm on Another Machine 51 52This must be done with a Wasmtime build that can run pre-compiled Wasm programs, 53that is a Wasmtime built with the `runtime` cargo feature. It does not need to 54compile new Wasm programs, so the `cranelift` and `winch` cargo features can be 55disabled. 56 57```rust,ignore 58{{#include ../examples/run_pre_compiled.rs}} 59``` 60 61## See Also 62 63* [Tuning Wasmtime for Fast Wasm Instantiation](./examples-fast-instantiation.md) 64* [Tuning Wasmtime for Fast Wasm Execution](./examples-fast-execution.md) 65* [Building a Minimal Wasmtime Embedding](./examples-minimal.md) 66* [`wasmtime::Engine::precompile_module`](https://docs.rs/wasmtime/latest/wasmtime/struct.Engine.html#method.precompile_module) 67 and 68 [`wasmtime::Engine::precompile_component`](https://docs.rs/wasmtime/latest/wasmtime/struct.Engine.html#method.precompile_component) 69* [`wasmtime::Module::deserialize`](https://docs.rs/wasmtime/latest/wasmtime/struct.Module.html#method.deserialize), 70 [`wasmtime::Module::deserialize_file`](https://docs.rs/wasmtime/latest/wasmtime/struct.Module.html#method.deserialize_file), 71 [`wasmtime::component::Component::deserialize`](https://docs.rs/wasmtime/latest/wasmtime/component/struct.Component.html#method.deserialize), 72 and 73 [`wasmtime::component::Component::deserialize_file`](https://docs.rs/wasmtime/latest/wasmtime/component/struct.Component.html#method.deserialize_file) 74