emscripten-core / emscripten-core/emscripten

Embind: instantiate multiple times

Open
#14,978 5 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I want Embind to allow instantiating the same module multiple times., i.e.:
- download once
- compile once
- instantiate multiple independent modules

This would be something along these lines with vanilla `WebAssembly.instantiateStreaming()` + `WebAssembly.instantiate()`:

```js
const fetchPromise = fetch('fibonacci.wasm');
const { module, instance } = await WebAssembly.instantiateStreaming(fetchPromise);
const otherInstance = await WebAssembly.instantiate(module);
```
(taken from [developers.google.com: Loading WebAssembly modules efficiently](https://developers.google.com/web/updates/2018/04/loading-wasm))

Note the `instance` and `otherInstance` are the 2 instances (I need even more) of the same `module`.

What I do right now is horribly inefficient and users have to wait a few seconds for the entire thing to complete. I am forced (I think so) to download, compile and instantiate in one go, multiple times:

```js
return new Promise((resolve) => {
const module = emscriptenJs({
onRuntimeInitialized: () => {
resolve(module)
},
})
})
```

which means doing a lot of useless duplicated work multiple times.

(source: (1) [the loop calling init function on WebWorker](https://github.com/nextstrain/nextclade/blob/34d262d3327267833e55629d6ee0df0061301088/packages/web/src/workers/run.ts#L39-L44), (2) [the init function calling loading code](https://github.com/nextstrain/nextclade/blob/34d262d3327267833e55629d6ee0df0061301088/packages/web/src/workers/worker.analyze.ts#L73-L88), (3) [the loading code](https://github.com/nextstrain/nextclade/blob/34d262d3327267833e55629d6ee0df0061301088/packages/web/src/workers/wasmModule.ts#L47-L59))

Context: I am using a WebWorker thread pool, where each WebWorker does the same thing, and the work is implemented in C++/WebAssembly. My idea is to compile once, pass the compiled blob to WebWorkers, instantiate there.

Is it currently possible to instantiate Embind-based modules multiple times? So far I tried to instantiate them with the vanilla code above, but they don't have imports section and even if they did, will probably require the JS glue code.

If it's not yet possible, I am ready to try to implement a solution, either in userland or in Emscripten, so I appreciate any hints.

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.