File: examples-pre-compiling-wasm.md

package info (click to toggle)
rust-wasmtime 36.0.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 60,576 kB
  • sloc: cpp: 5,670; ansic: 4,079; sh: 636; javascript: 608; asm: 110; ml: 96; makefile: 61; python: 12
file content (73 lines) | stat: -rw-r--r-- 3,985 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Pre-Compiling and Cross-Compiling WebAssembly Programs

Wasmtime can compile a WebAssembly program to native code on one machine, and
then run it on a different machine. This has a number of benefits:

* **Faster start up:** Compilation is removed from the critical path. When a new
  HTTP request comes into your function-as-a-service platform, for example, you
  do not have to wait for the associated Wasm program to compile before it can
  start handling the request. Similarly, when a new update for your embedded
  device's Wasm application logic comes in, you do not need to compile the
  update on the under-powered device before it can begin running new updated
  logic.

* **Less Memory Usage:** Pre-compiled Wasm programs can be lazily `mmap`ed from
  disk, only paging their code into memory as those code paths are executed. If
  none of the code on a page is ever executed, the OS will never make the page
  resident. This means that running pre-compiled Wasm programs lowers overall
  memory usage in the system.

* **Smaller Code Size for Embedded:** Wasmtime can be built such that it can
  *only* run Wasm programs that were pre-compiled elsewhere. These builds will
  not include the executable code for Wasm compilation. This is done by
  disabling the `cranelift` and `winch` cargo features at build time. These
  builds are useful for embedded devices, where programs must be small and fit
  within the device's constrained environment.

* **Smaller Attack Surfaces:** Similarly, building Wasmtime without a compiler,
  and with only support for running pre-compiled Wasm programs, can be useful
  for security-minded embeddings to reduce the potential attack surface exposed
  to untrusted and potentially hostile Wasm guests. Compilation, triggered by
  the control plane, can happen inside a Wasmtime build that can compile but not
  run Wasm programs. Execution, in the data plane, can happen inside a Wasmtime
  build that can run but not compile new Wasm programs. Exposing a minimal
  attack surface to untrusted code is good security practice.

Note that these benefits are applicable regardless which Wasm execution strategy
you've configured: Cranelift, Winch, or Pulley.

## Pre-Compile the Wasm on One Machine

This must be done with a Wasmtime build that has a Wasm execution strategy
enabled, e.g. was built with the `cranelift` or `winch` cargo features. It does
not require the ability to run Wasm programs, so the `runtime` cargo feature can
be disabled at build time.

```rust,ignore
{{#include ../examples/pre_compile.rs}}
```

## Run the Pre-Compiled Wasm on Another Machine

This must be done with a Wasmtime build that can run pre-compiled Wasm programs,
that is a Wasmtime built with the `runtime` cargo feature. It does not need to
compile new Wasm programs, so the `cranelift` and `winch` cargo features can be
disabled.

```rust,ignore
{{#include ../examples/run_pre_compiled.rs}}
```

## See Also

* [Tuning Wasmtime for Fast Wasm Instantiation](./examples-fast-instantiation.md)
* [Tuning Wasmtime for Fast Wasm Execution](./examples-fast-execution.md)
* [Building a Minimal Wasmtime Embedding](./examples-minimal.md)
* [`wasmtime::Engine::precompile_module`](https://docs.rs/wasmtime/latest/wasmtime/struct.Engine.html#method.precompile_module)
  and
  [`wasmtime::Engine::precompile_component`](https://docs.rs/wasmtime/latest/wasmtime/struct.Engine.html#method.precompile_component)
* [`wasmtime::Module::deserialize`](https://docs.rs/wasmtime/latest/wasmtime/struct.Module.html#method.deserialize),
  [`wasmtime::Module::deserialize_file`](https://docs.rs/wasmtime/latest/wasmtime/struct.Module.html#method.deserialize_file),
  [`wasmtime::component::Component::deserialize`](https://docs.rs/wasmtime/latest/wasmtime/component/struct.Component.html#method.deserialize),
  and
  [`wasmtime::component::Component::deserialize_file`](https://docs.rs/wasmtime/latest/wasmtime/component/struct.Component.html#method.deserialize_file)