WebAssembly (Wasm) Optimizer Skill
This skill provides guidelines and tools for optimizing WebAssembly modules used in the project, particularly for game engine adapters (e.g., Stockfish).
Capabilities
- Size Optimization: Strategies to minimize
.wasmbinary size (e.g., usingwasm-opt, compiler flags). - Loading Performance: Best practices for fetching, compiling, and instantiating Wasm modules (streaming compilation).
- Memory Management: Guidelines for managing Wasm memory from JavaScript/TypeScript to prevent leaks.
- Integration: Patterns for seamless communication between Wasm and the main JS thread or Web Workers.
Optimization Checklist
- Compiler Flags: Are you using optimization flags like
-O3,-Oz, or-Os? - Binary Stripping: Have debug symbols been stripped from the production binary?
-
wasm-opt: Is the binary post-processed withwasm-opt(from Binaryen)? - Streaming Compilation: Do you use
WebAssembly.instantiateStreamingwhere supported? - Compression: Is the
.wasmfile served with Brotli or Gzip compression?
Code Snippets
Efficient Instantiation
// Prefer instantiateStreaming for better performance
async function loadWasm(url: string, importObject: WebAssembly.Imports) {
if (WebAssembly.instantiateStreaming) {
return await WebAssembly.instantiateStreaming(fetch(url), importObject);
} else {
const response = await fetch(url);
const bytes = await response.arrayBuffer();
return await WebAssembly.instantiate(bytes, importObject);
}
}
Tools
- Emscripten: Standard SDK for compiling C/C++ to Wasm.
- Binaryen (
wasm-opt): Compiler infrastructure and optimization toolchain. - Twiggy: A code size profiler for Wasm.
- wasm-snip: Replaces unneeded Wasm functions with
unreachable.
Troubleshooting
- "Out of bounds memory access": Check for correct pointer arithmetic and memory allocation sizes on the JS side.
- "LinkError: Import #x module="env" function="...": function import requires a callable": Verify that the
importObjectpassed to instantiation matches the imports expected by the Wasm module.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.