Is there a way to get Emscripten to transfer, and not copy, data between web workers and the main UI thread?
Emscripten has an API that manages communication between Web Workers, which I believes just uses the postMessage
/ onmessage
mechanism under the hood. Looking in the source for the Emscripten Worker API, it appears that it doesn't use the transferList
option when it calls postMessage
, and so the data gets copied.
Actually, I think it gets copied at least twice: first by the browser between the threads, and then a second time by Emscripten to get it into the Emscripten-managed heap-space. And if you want the data to continue to survive on the receiving end after the callback, it would have to be copied a third time, as according to the docs the data passed to the callback is only guaranteed to exist during the callback.
Repeating my question from the top: is there a way to get Emscripten to avoid all this copying by transferring, and not copying, data between web workers and the main UI thread?
This is possible if you make use of SharedArrayBuffer. Recently, the Emscripten guys added experimental support for pthread in Emscripten, which use this feature. However, only the Firefox nightly supports SharedArrayBuffers at the moment, so this is not widely adopted yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With