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

DataView doesn't work with Buffer from pool #2888

Closed
skomski opened this issue Sep 15, 2015 · 5 comments
Closed

DataView doesn't work with Buffer from pool #2888

skomski opened this issue Sep 15, 2015 · 5 comments
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@skomski
Copy link
Contributor

skomski commented Sep 15, 2015

I wanted to simplify the various .read{Type} or .write{Type} by using a DataView implementation and
found out that is not possible to use the Buffer's ArrayBuffer if the buffer was sliced from the pool.

const assert = require('assert');
const SlowBuffer = require('buffer').SlowBuffer;

const buffer = new Buffer(4).fill(0);
new DataView(buffer.buffer).setFloat32(0, 1, true);
console.log(buffer.buffer.byteLength);
console.log(buffer);

const slowBuffer = new SlowBuffer(4).fill(0);
new DataView(slowBuffer.buffer).setFloat32(0, 1, true);
console.log(slowBuffer.buffer.byteLength);
console.log(slowBuffer);

assert.deepEqual(buffer, slowBuffer);

results into

8192
<Buffer 00 00 00 00>
4
<Buffer 00 00 80 3f>

AssertionError: <Buffer 00 00 00 00> deepEqual <Buffer 00 00 80 3f>

The problem is the underlying ArrayBuffer for the new sliced TypedArray does not change.

@Fishrock123 Fishrock123 added the buffer Issues and PRs related to the buffer subsystem. label Sep 15, 2015
@targos
Copy link
Member

targos commented Sep 16, 2015

The ArrayBuffer changes, but not at the right index. If you want your DataView to start at the same offset as the Buffer object, you need to instantiate it with the byteOffset option:
new DataView(buffer.buffer, buffer.byteOffset)

@skomski
Copy link
Contributor Author

skomski commented Sep 16, 2015

Thanks. This behaviour is a bit counter-intuitive since I would suspect the underlying ArrayBuffer in .buffer is not the pool ArrayBuffer but a sliced version of it where byteOffset would be 0.

Correct usage:

const buffer = new Buffer(4).fill(0);
const dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
dataView.setFloat32(0, 1, true)

@skomski skomski closed this as completed Sep 16, 2015
@targos
Copy link
Member

targos commented Sep 16, 2015

I guess that if it was a slice, there would be no point in having a pool in the first place.
btw you can achieve the same without constructing a DataView with buffer.writeFloatLE(1, 0)

@skomski
Copy link
Contributor Author

skomski commented Sep 16, 2015

With slice I mean subarray because slice and subarray are the same thing for a Buffer.
From a design standpoint it would be better if the pool would be not visible in the returned Buffer and the underlying ArrayBuffer would be a view to the pool ArrayBuffer.

I want to simplify write{Type} or read{Type} by using DataView ;)

@trevnorris
Copy link
Contributor

Remember that Buffer is now just a Uint8Array with an assigned __proto__. All that is due to current specification. :-)

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