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: prewarm wasm #2727

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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();
const analyze = await initializeWasm(coreImports);

return typeof analyze !== "undefined";
}

/**
* 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();

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();

let fingerprint = "";

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