You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
worker_threads is used by many people,but we can only use SharedArrayBuffer(for shared memory) ,but Buffer is node's first citizen(socket eg),can you share Buffer to SharedArrayBuffer ? #4288
threads_worker is used by many people,but we can only use SharedArrayBuffer ,but Buffer is node's first citizen(socket eg),can you share Buffer to SharedArrayBuffer ?
What is the feature you are proposing to solve the problem?
let instance=SharedArrayBuffer.from(Buffer instance);//zero copy
let instance2=Buffer.From(SharedArrayBuffer instance);//zero copy
You mean node:worker_threads, right? If all buffers could be shared, the language would not have bothered to come up with a SharedArrayBuffer implementation. What you can do is the following:
Uint8Array is not exactly a Buffer, but I don't think you'll find any Node.js API that accept Buffer and not Uint8Array (if you do, that'd be a bug, please report it).
On the other hand, if you have a Buffer/Uint8Array and want to transfer its content to an other thread, you have two options:
// Option 1: copy the content to a SharedArrayBuffer:functioncopyBufferToSAB(buffer,sab){if(buffer.byteLength>sab.byteLength)thrownewError('Buffer overflow');newUint8Array(sab).set(buffer);}// Option 2: transfer the underlying array buffer:functiontransferBuffer(buffer){otherThread.postMessage(buffer,[buffer.buffer]);}
Option 2 is way faster, but note that only one thread interact with a non-sharable buffer, meaning that once it is transfered, the original thread can no longer have access to that buffer data.
no,you are wrong. copy ArrayBuffer is so expensive
I don't know why you say I'm wrong, I don't think I have said anything about copy not being expensive. Like I said, transfering is the faster option, that's the one that let you reuse the same data on another thread without copying data.
Activity
aduh95 commentedon Nov 7, 2023
You mean
node:worker_threads, right? If all buffers could be shared, the language would not have bothered to come up with aSharedArrayBufferimplementation. What you can do is the following:Uint8Arrayis not exactly aBuffer, but I don't think you'll find any Node.js API that acceptBufferand notUint8Array(if you do, that'd be a bug, please report it).On the other hand, if you have a
Buffer/Uint8Arrayand want to transfer its content to an other thread, you have two options:Option 2 is way faster, but note that only one thread interact with a non-sharable buffer, meaning that once it is transfered, the original thread can no longer have access to that buffer data.
introspection3 commentedon Nov 7, 2023
introspection3 commentedon Nov 7, 2023
aduh95 commentedon Nov 7, 2023
I don't know why you say I'm wrong, I don't think I have said anything about copy not being expensive. Like I said, transfering is the faster option, that's the one that let you reuse the same data on another thread without copying data.
avivkeller commentedon Jun 15, 2024
@introspection3 @aduh95 is this issue resolved? If so, feel free to close the issue!
Thanks!