SUCHMOKUO/node-worker-threads-pool

Send TransferList ArrayBuffer Back to Main Thread From Worker Thread

Opened this issue · 3 comments

Hello, thanks for writing this awesome library!

Is it possible to send data from a worker thread back to the parent thread by reference (via the parentPort.postMessage transfer list argument API) rather than by clone? I have worker threads in my program that create large arrays of objects (composed of primitive JavaScript types).

The objects are C-like structures, so converting them to binary ArrayBuffer objects would be doable. I believe it would be more performant to pass the large array back to the main thread by handing it the pointer to the array from the worker thread, rather than using the structured clone algorithm to send a copy back.

From my understanding it seems the existing setTransferList API can only be used for passing large binary data from the main thread to the worker thread?

@mdaronco Great point! Thank you mate!
Yeah... currently there is no existing way to transfer ArrayBuffer back to main thread in this library, but that is a good point and hopefully I'll add it in the next version :)

I'm also interested in this feature. A similar package, Piscina, implemented this as a static function that wraps the worker return value: https://github.com/piscinajs/piscina#static-method-movevalue

@Narretz Cool, I'll take a look, thanks!