From b72f986b9990d79820877efb8b5717369df0d86e Mon Sep 17 00:00:00 2001 From: "Victor M. Alvarez" Date: Thu, 4 Jun 2026 10:39:00 +0200 Subject: [PATCH] perf: remove unused functions from WASM module The `WasmModuleBuilder` initially includes all functions marked with `#[wasm_export]` or `#[module_export]`. However, not all of these functions are necessarily used in the final compiled rules (e.g., when YARA modules are not imported). This change adds a `walrus::passes::gc` pass to the build process, which removes any functions that are not actually referenced, thereby reducing the size of the compiled WASM rules. --- lib/src/wasm/builder.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/src/wasm/builder.rs b/lib/src/wasm/builder.rs index 551caec01..4e809172f 100644 --- a/lib/src/wasm/builder.rs +++ b/lib/src/wasm/builder.rs @@ -344,6 +344,15 @@ impl WasmModuleBuilder { self.main_func.finish(Vec::new(), &mut self.module.funcs); self.module.exports.add("main", main_func); + + // WasmModuleBuilder::new adds to the WASM module all the functions + // that are labeled as #[wasm_export] or #[module_export]. However, + // not all the functions are used in the final WASM code because + // (ie: some YARA modules are not imported by the rules). This removes + // the functions that are not actually used, reducing the size of the + // compiled rules. + walrus::passes::gc::run(&mut self.module); + self.module } }