Skip to content

Commit

Permalink
feat(web-backend.js): add function to allocate wasm memory based on u…
Browse files Browse the repository at this point in the history
…ser agent to improve performance on iOS Safari devices
  • Loading branch information
MartinMinkov committed May 29, 2023
1 parent 9948427 commit ea06920
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion js/web/web-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ let init = wasm.default;
let worker;

async function initSnarkyJS() {
let { memory } = await init();
const memory = allocateWasmMemoryForUserAgent(window.navigator.userAgent);

await init(undefined, memory);
let module = init.__wbindgen_wasm_module;
let numWorkers = await getEfficientNumWorkers();

Expand Down Expand Up @@ -162,3 +164,21 @@ async function workerCall(worker, type, message) {
worker.postMessage({ type, id, message });
return (await promise).result;
}

function allocateWasmMemoryForUserAgent(userAgent) {
const isIOSDevice = /iPad|iPhone|iPod/.test(userAgent);
const isSafari = /Safari/.test(userAgent);
if (isIOSDevice && isSafari) {
return new WebAssembly.Memory({
initial: 19,
maximum: 16384, // 1 GiB
shared: true,
});
} else {
return new WebAssembly.Memory({
initial: 19,
maximum: 65536, // 4 GiB
shared: true,
});
}
}

0 comments on commit ea06920

Please sign in to comment.