Skip to content

Commit

Permalink
perf(analyze): Compile WebAssembly upon module load (#2727)
Browse files Browse the repository at this point in the history
This change will pre-compile the Wasm files for long-running process when the analyze-wasm module is loaded.

By keeping the Promise results of the compilation as variables at the top of the module, we no longer need a module cache map.
  • Loading branch information
e-moran authored Jan 14, 2025
1 parent fe54f0d commit 489f1c6
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions analyze-wasm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,19 @@ import { wasm as componentCore3Wasm } from "./wasm/arcjet_analyze_js_req.compone
type DetectSensitiveInfoFunction =
typeof ArcjetJsReqSensitiveInformationIdentifier.detect;

const wasmCache = new Map<string, WebAssembly.Module>();
const componentCoreWasmPromise = componentCoreWasm();
const componentCore2WasmPromise = componentCore2Wasm();
const componentCore3WasmPromise = componentCore3Wasm();

async function moduleFromPath(path: string): Promise<WebAssembly.Module> {
const cachedModule = wasmCache.get(path);
if (typeof cachedModule !== "undefined") {
return cachedModule;
}

if (path === "arcjet_analyze_js_req.component.core.wasm") {
const mod = await componentCoreWasm();
wasmCache.set(path, mod);
return mod;
return componentCoreWasmPromise;
}
if (path === "arcjet_analyze_js_req.component.core2.wasm") {
const mod = await componentCore2Wasm();
wasmCache.set(path, mod);
return mod;
return componentCore2WasmPromise;
}
if (path === "arcjet_analyze_js_req.component.core3.wasm") {
const mod = await componentCore3Wasm();
wasmCache.set(path, mod);
return mod;
return componentCore3WasmPromise;
}

throw new Error(`Unknown path: ${path}`);
Expand Down

0 comments on commit 489f1c6

Please sign in to comment.