Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transferring data using Emscripten Worker API without copying

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?

like image 892
Michal Charemza Avatar asked Apr 03 '15 06:04

Michal Charemza


1 Answers

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.

like image 80
Deathicon Avatar answered Sep 23 '22 00:09

Deathicon