WasmOptimizer

interface WasmOptimizer

Injectable contract for a wasm→wasm optimizer — the transform a compiler node runs on a raw kernel before publishing the result as a gossiped variant.

A compiler node fetches the source bytes for a CompileRequest, calls optimize, and hands the leaner module to WarpNode.publishVariant under the requested VariantKey. Everything downstream — gossip, discover, fetch, tier-swap, the interpreted/compiled counters — is unchanged; the optimizer only swaps the transform that produces the variant bytes. It slots in exactly where WarpNode.registerCompiler takes its compile lambda.

ABI-preserving. A conforming implementation MUST return a still-runnable module that preserves the warp ABI — the warp_alloc/warp_run exports a WasmRuntime requires — so a weaker peer can load and run the optimized variant identically. Production optimizers (wasm-opt/Binaryen) preserve exported functions at every level, so the ABI survives.

Level semantics. The OptLevel selects the effort: O2/O3 optimize for speed, Oz for size; OptLevel.O0 is a passthrough (no variant worth shipping), served by PassthroughWasmOptimizer. A real optimizer typically rejects O0 or returns the input unchanged.

Where implementations live. :kuilt-warp core stays dependency-free of any optimizer toolchain — it holds only this interface and the PassthroughWasmOptimizer default. The real BinaryenWasmOptimizer (bundled wasm-opt, extract-and-exec) ships in the opt-in :kuilt-warp-compiler satellite, so the toolchain weight falls solely on compiler-node operators. Tests inject PassthroughWasmOptimizer or a fake.

See also

Inheritors

Functions

Link copied to clipboard
abstract suspend fun optimize(bytes: ByteArray, optLevel: OptLevel): ByteArray

Optimize bytes at optLevel, returning a distinct, still-runnable, ABI-preserving module. The result's content hash differs from the source (unless optLevel is a passthrough), making it a genuinely distinct, fetchable variant.