Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(analyze): Compile WebAssembly upon module load #2727

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ function createCoreImports(detect?: DetectSensitiveInfoFunction): ImportObject {
};
}

/**
* Pre-warms the wasm cache to improve cold start performance.
* @returns Whether the cache was successfully warmed or not.
*/
export async function prewarmWasmCache(): Promise<boolean> {
const coreImports = createCoreImports();
blaine-arcjet marked this conversation as resolved.
Show resolved Hide resolved
blaine-arcjet marked this conversation as resolved.
Show resolved Hide resolved
const analyze = await initializeWasm(coreImports);

return typeof analyze !== "undefined";
}

blaine-arcjet marked this conversation as resolved.
Show resolved Hide resolved
/**
* Generate a fingerprint for the client. This is used to identify the client
* across multiple requests.
Expand Down
11 changes: 11 additions & 0 deletions arcjet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,8 @@ export default function arcjet<
}
const log = options.log;

const wasmPrewarm = analyze.prewarmWasmCache();
blaine-arcjet marked this conversation as resolved.
Show resolved Hide resolved

const perf = new Performance(log);

// TODO(#207): Remove this when we can default the transport so client is not required
Expand Down Expand Up @@ -1298,6 +1300,15 @@ export default function arcjet<
...ctx,
};

const logWasmPrewarmPerf = perf.measure("wasm prewarm");
const prewarmed = await wasmPrewarm;
if (!prewarmed) {
log.debug(
"Wasm prewarm failed. This may indicate that Wasm is not supported on this platform",
);
}
logWasmPrewarmPerf();

blaine-arcjet marked this conversation as resolved.
Show resolved Hide resolved
let fingerprint = "";

const logFingerprintPerf = perf.measure("fingerprint");
Expand Down
Loading