-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
buffer: add Buffer.createView() to create a new buffer that shares ArrayBuffer with another ArrayBufferView #29101
Conversation
This seems generally useful, enough so that i'd be worried about it getting in the way of a future js language addition. |
fe24da5
to
e85c10b
Compare
@nodejs/buffer |
It creates a new Buffer that shares ArrayBuffer with another ArrayBufferView. A shortcut for `Buffer.from(view.buffer, view.byteOffset, view.byteLength)` but it requires no temporary var. refs: nodejs#28725
e85c10b
to
963f42d
Compare
I'm in favor, the logic looks good to me. |
But as @devsnek says -- it is useful enough to be introduced to all other ArrayBuffer views. |
Actually, it would be better if ECMA-262's array buffer views have this method 😄 |
@nodejs/open-standards Anyone know if something like this is in the works? |
nothing like this is currently in the works, but i am considering pursuing it at some point (not soon) |
How would this be different from: const arr = new Uint16Array(2);
arr[0] = 5000;
arr[1] = 4000;
// Shares memory with `arr`.
const buf = Buffer.from(arr.buffer);
console.log(buf);
// Prints: <Buffer 88 13 a0 0f>
// Changing the original Uint16Array changes the Buffer also.
arr[1] = 6000;
console.log(buf);
// Prints: <Buffer 88 13 70 17> |
@jasnell
$ node
> const a = Buffer.from('foo')
undefined
> const b = new Uint8Array(a.buffer, a.offset, a.length)
undefined
> const c = Buffer.from(b.buffer)
undefined
> b
Uint8Array [ 102, 111, 111 ]
> c
<Buffer 66 6f 6f ef fe 7f 00 00 00 00 00 00 00 00 00 00 00 20 00 ... > |
Can this be implemented as a userland module? If so, maybe that's the way to split the difference between "super useful to have right now" vs. "don't want to duplicate/conflict/whatever a future standard"? Another possibility is to implement it but leave it as Experimental status, but I'm wary of that. People may just start using it in important modules and production systems anyway. |
Wouldn't it make more sense to just change .from to work correctly with ArrayBufferView? |
I’d also expect changing the semantics of |
How can I make progress for this pull-request? Or if this pull-request is unacceptable, please close it. |
@gfx I do not have a strong opinion on the PR itself. I am closing this, since this did not get any traction recently and it does not seem likely to get accepted as it is. Thank you very much for your work though and it does seem like it would be a good thing to publish this as a small module. |
This is a proposal for a new method
Buffer.createView()
, although its name could be changed.This is not ready to merge because of missing docs and tests, but rather I've made this PR for discussion.
Rationale
When users convert ArrayBufferView (e.g. typed arrays and buffer itself) to Buffer, there are typical mistakes about handling underlying ArrayBuffer.:
Buffer.from(arrayBufferView)
that copies the underlying ArrayBuffer ofarrayBufferView
which might be unexpectedly slowBuffer.from(arrayBufferView.buffer)
that creates a view of the underlying ArrayBuffer withoutoffset
andlength
parametersSo this PR creates a new method
Buffer.createView(arrayBufferView)
:And also
Buffer.createView(arrayBuffer)
is provided as the same semantics of the currentBuffer.from(arrayBuffer)
to clarify that it is just a view ofarrayBuffer
.I think
Buffer.from(arrayBuffer)
should be deprecated because it is extremely confusing, and ECMA-262's typed arrays have no such overload, anyway. However, it is not directly related to this PR.refs: #28725
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes