emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.4k stars 3.26k forks source link

Efficiently using memory with wasm/asmjs? #13117

Open lnxjnky opened 3 years ago

lnxjnky commented 3 years ago

Hello, I am porting cross platform code to Webassembly with USE_PTHREADS=1, PTHREAD_POOL_SIZE=8, TOTAL_MEMORY/INITIAL_MEMORY=64MB. While running the code in chrome as browser extension, I observe that in chrome task manager that the memory usage of extension is around 350MB even when it is in idle state. I made sure that none of the extension JS code is using that memory and also decreasing PTHREAD_POOL_SIZE is not helping. I checked most of the memory is allocated at the time of wasm load itself much before I invoke my native methods.

Just trying to figure out if there are any hidden gotchas or an advisable way to use memory efficiently while compiling for WASM? I have below code in the generated JS file.

var PAGE_SIZE = 16384;
var WASM_PAGE_SIZE = 65536;
var buffer, HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64;

function updateGlobalBufferViews() {
    Module["HEAP8"] = HEAP8 = new Int8Array(buffer);
    Module["HEAP16"] = HEAP16 = new Int16Array(buffer);
    Module["HEAP32"] = HEAP32 = new Int32Array(buffer);
    Module["HEAPU8"] = HEAPU8 = new Uint8Array(buffer);
    Module["HEAPU16"] = HEAPU16 = new Uint16Array(buffer);
    Module["HEAPU32"] = HEAPU32 = new Uint32Array(buffer);
    Module["HEAPF32"] = HEAPF32 = new Float32Array(buffer);
    Module["HEAPF64"] = HEAPF64 = new Float64Array(buffer)
}

var INITIAL_TOTAL_MEMORY = Module["TOTAL_MEMORY"] || 67108864;

if (!ENVIRONMENT_IS_PTHREAD) {
    wasmMemory = new WebAssembly.Memory({
        "initial": INITIAL_TOTAL_MEMORY / WASM_PAGE_SIZE,
        "maximum": INITIAL_TOTAL_MEMORY / WASM_PAGE_SIZE,
        "shared": true
    });
    buffer = wasmMemory.buffer;
lnxjnky commented 3 years ago

Any guidance please?

I am expecting that multiple allocations of HEAP variables with buffer size inside updateGlobalBufferViews will be sharing the buffer space and are not individual allocations. Even if there are global variables in the native code which might be initialized at the time of wasm load should be using the same shared array buffer right? How can we identify such variables, any tools?

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.