Tools You Should Know

This is a curated list of awesome tools you should know about when doing Rustand WebAssembly development.

Development, Build, and Workflow Orchestration

wasm-pack | repository

wasm-pack seeks to be a one-stop shop for building and working with Rust-generated WebAssembly that you would like to interoperate with JavaScript, onthe Web or with Node.js. wasm-pack helps you build and publish Rust-generatedWebAssembly to the npm registry to be used alongside any other JavaScriptpackage in workflows that you already use.

Optimizing and Manipulating .wasm Binaries

wasm-opt | repository

The wasm-opt tool reads WebAssembly as input, runs transformation,optimization, and/or instrumentation passes on it, and then emits thetransformed WebAssembly as output. Running it on the .wasm binaries producedby LLVM by way of rustc will usually create .wasm binaries that are bothsmaller and execute faster. This tool is a part of the binaryen project.

wasm2js | repository

The wasm2js tool compiles WebAssembly into "almost asm.js". This is great forsupporting browsers that don't have a WebAssembly implementation, such asInternet Explorer 11. This tool is a part of the binaryen project.

wasm-gc | repository

A small tool to garbage collect a WebAssembly module and remove all unneededexports, imports, functions, etc. This is effectively a —gc-sections linkerflag for WebAssembly.

You don't usually need to use this tool yourself because of two reasons:

  • rustc now has a new enough version of lld that it supports the—gc-sections flag for WebAssembly. This is automatically enabled for LTObuilds.
  • The wasm-bindgen CLI tool runs wasm-gc for you automatically.

wasm-snip | repository

wasm-snip replaces a WebAssembly function's body with an unreachableinstruction.

Maybe you know that some function will never be called at runtime, but thecompiler can't prove that at compile time? Snip it! Then run wasm-gc again andall the functions it transitively called (which could also never be called atruntime) will get removed too.

This is useful for forcibly removing Rust's panicking infrastructure innon-debug production builds.

Inspecting .wasm Binaries

twiggy | repository

twiggy is a code size profiler for .wasm binaries. It analyzes a binary'scall graph to answer questions like:

  • Why was this function included in the binary in the first place? I.e. whichexported functions are transitively calling it?
  • What is the retained size of this function? I.e. how much space would be savedif I removed it and all the functions that become dead code after its removal.

Use twiggy to make your binaries slim!

wasm-objdump | repository

Print low-level details about a .wasm binary and each of its sections. Alsosupports disassembling into the WAT text format. It's like objdump but forWebAssembly. This is a part of the WABT project.

wasm-nm | repository

List the imported, exported, and private function symbols defined within a.wasm binary. It's like nm but for WebAssembly.