3 ms·
One use case I have, which I do not like, but there is literally no other way: Communicating with workerthreads. I so much would like to pass references of bi
by hutzlibu 3y ago
One use case I have, which I do not like, but there is literally no other way:
Communicating with workerthreads.
I so much would like to pass references of big readonly objects towards them - but no, full copy it is, for every thread.
- chatmasta 3y agoYou're probably aware of this, but in some cases you might be able to use SharedArrayBuffer [0], which doesn't make a copy of the underlying data but does use structured cloning to clone the outside of the object. [0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- hutzlibu 3y agoI only used it for experimentation so far, but I abondoned it, after I learned, that only one worker can be the active user of a SharedArrayBuffer. So they cannot be shared at the same time? Sharing seems to mean, now A uses it, than gives control back, via the main thread, then B uses it, etc (but I hope I missunderstood something there) But I need many threads to access the same data, at the same time (readonly, so no racecondition).
- bongobingo1 3y agoAh, the OnlyChild implementation of Shared.
- kaoD 3y agoAre you sure about this? You might be mixing ArrayBuffer (which is a transferrable object[0] and gets zero-copy moved) and SharedArrayBuffer. From [1]: > The structured clone algorithm accepts SharedArrayBuffer objects and typed arrays mapped onto SharedArrayBuffer objects. In both cases, the SharedArrayBuffer object is transmitted to the receiver resulting in a new, private SharedArrayBuffer object in the receiving agent (just as for ArrayBuffer). However, the shared data block referenced by the two SharedArrayBuffer objects is the same data block, and a side effect to the block in one agent will eventually become visible in the other agent. [0] https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers... [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- hutzlibu 3y ago"Are you sure about this?" No. And I hope to be wrong. I will give it another try then, but I was using SharedArrayBuffer and I was getting explicit errors about this limitation.
- kaoD 3y agoDid a quick test and seems to work as expected: https://codesandbox.io/p/devbox/sharedarraybuffer-33qywc https://codesandbox.io/p/devbox/sharedarraybuffer-33qywc (see console in https://33qywc-3000.csb.app/ https://33qywc-3000.csb.app/) Caveat is you need these headers for security reasons (hence why the sandbox above has Express) or SharedArrayBuffer is not even defined (at least in Firefox): Cross-Origin-Opener-Policy: same-origin Cross-Origin-Embedder-Policy: require-corp