Skip to content
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

Unexpected behavior for .buffer property of sliced Buffers #6744

Closed
jfirebaugh opened this issue May 13, 2016 · 10 comments
Closed

Unexpected behavior for .buffer property of sliced Buffers #6744

jfirebaugh opened this issue May 13, 2016 · 10 comments
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@jfirebaugh
Copy link

jfirebaugh commented May 13, 2016

  • Version: 6.1.0
  • Platform: OS X
  • Subsystem: Buffer

Since instances of Buffer purport to also be Uint8Array TypedArray instances, I'd expect both of the following to produce 8:

> new Buffer(16).slice(8).buffer.byteLength
8192
> new Uint8Array(16).slice(8).buffer.byteLength
8

The fact that the buffer property on a Buffer slice returns an ArrayBuffer viewing the entire Buffer -- rather than just the slice as I expected -- looks like a bug to me. However, there's the possibility that this falls under the category of "subtle incompatibilities with the TypedArray specification" that the documentation mentions. If that is the case, please explicitly state that in the documentation.

@addaleax addaleax added the buffer Issues and PRs related to the buffer subsystem. label May 13, 2016
@bnoordhuis
Copy link
Member

Buffer objects may come from a shared memory pool and the documentation mentions that in several places, grep for 'pool'.

@jfirebaugh
Copy link
Author

@bnoordhuis If there's a connection between any of the mentions of "pool" in the documentation, and the issue here, it's not clear to me. Are you implying that the behavior of the buffer property on a Buffer slice depends on whether or not the backing store comes from the shared memory pool?

@addaleax
Copy link
Member

@jfirebaugh Yes:

> Buffer.allocUnsafeSlow(16).slice(8).buffer.byteLength
16
> Buffer.allocUnsafe(16).slice(8).buffer.byteLength
8192

@jfirebaugh
Copy link
Author

And for completeness, a third possibility:

> Buffer.alloc(16).slice(8).buffer.byteLength
16

Is this intended behavior for the buffer property of a Buffer slice, that it returns a result that's dependent on the original allocation method?

@bnoordhuis
Copy link
Member

Yes. It's worked that way since node v0.3 or v0.4, although the property was called .parent back then.

@jfirebaugh
Copy link
Author

Okay. Can you please document the buffer property, and mention that has different behavior than Uint8Array.prototype.buffer?

@vkurchatkin
Copy link
Contributor

vkurchatkin commented May 13, 2016

mention that has different behavior than Uint8Array

Well, it's not, it's the same property. For example:

> new Uint8Array(new ArrayBuffer(16), 0, 8).buffer.byteLength
16

or

> new Uint8Array(16).subarray(0, 8).buffer.byteLength
16

@jfirebaugh
Copy link
Author

jfirebaugh commented May 13, 2016

@vkurchatkin It does have different behavior, as I demonstrated in the initial description. For a Uint8Array slice, buffer returns an ArrayBuffer reflecting the slice. (If you want, I'll dig up a standards reference for that.) For a Buffer slice, it does not -- based on the results, I assume it returns an ArrayBuffer reflecting the unsliced backing store.

> new Buffer(16).slice(8).buffer.byteLength
8192
> new Uint8Array(16).slice(8).buffer.byteLength
8

(I personally don't think 8192 is a defensible result here, but if you've decided that's the desired behavior, fine -- I'm just asking that it be documented.)

@jfirebaugh
Copy link
Author

Ok, disregard the above comment. After reading some more, I can see that the difference in behavior is stemming from slice(), not from buffer.

@vkurchatkin
Copy link
Contributor

closing then, slice behaviour is well documented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants