emscripten-core / emscripten-core/emscripten

wasmMemory cleanup

Open
#15,813 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Hi,

I have a specific case with emcripten wasm executed from within my own Worker where I want to create and execute main (using pthreads) in loop, while using 2GB of INITIAL_MEMORY. It seems the most performant to do the following:

```js
const INITIAL_MEMORY=2146435072;
const wasmMemory=new WebAssembly.Memory({"initial":INITIAL_MEMORY/65536,"maximum":4294967296/65536,"shared":true});
for (...) {
resetMemory();
module = await createModule({wasmMemory, ...});
await runMain(); // executes module._emscripten_proxy_main(...);
module.PThread.terminateAllThreads(); // destroy all module related workers, otherwise these doesnt get GCed
}
```

So instead of reallocating this large WebAssembly memory, I reuse it while resetting before each creation.

My question is, whats the fastest way / algorithm to reset the memory, considering how emscripten reads and use it, inside my `resetMemory()`? Obviously I can reset it whole which seems slow:

```js
new Uint32Array(wasmMemory.buffer).fill(0); // takes up to 900ms
```

...but it also seems sufficient to reset just first 16Mb

```js
new Uint32Array(wasmMemory.buffer).fill(0, 0, 1024 * 1024 * 4); // ~4ms
```

I am thinking there might be some particular byte, byte range or even better built-in method to do the reset?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.