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

Avoid copying the (potentially large) rest of the decoder buffer #471

Merged
merged 2 commits into from
Aug 18, 2019

Conversation

ankon
Copy link
Contributor

@ankon ankon commented Aug 16, 2019

This aligns 'readAll' with other 'read' methods, which do not produce a copy unless needed.
This avoids a couple of likely very expensive copy operations in the network receive path.

Signed-off-by: Andreas Kohn andreas.kohn@gmail.com

This aligns 'readAll' with other 'read' methods, which do not produce a copy
unless needed.
This avoids a couple of likely very expensive copy operations in the network
receive path.

Signed-off-by: Andreas Kohn <andreas.kohn@gmail.com>
@Nevon
Copy link
Collaborator

Nevon commented Aug 18, 2019

While I applaud the initiative (I desperately want to fix the performance of our encoder/decoder), I don't think this actually changes anything. Buffer.slice returns a new copy of the buffer, so either way you are copying the data.

Also, your new implementation changes the offset of the decoder when reading. While this makes sense, I believe the original intent was to be able to read the entire remaining content of the buffer without affecting the offset (at least that's what the old implementation does).

@ankon
Copy link
Contributor Author

ankon commented Aug 18, 2019

Buffer.slice returns a new copy of the buffer, so either way you are copying the data.

Maybe I'm misreading the docs then, but https://nodejs.org/api/buffer.html#buffer_buf_slice_start_end says:

Returns a new Buffer that references the same memory as the original, but offset and cropped by the start and end indices.

As to changing the offset: Indeed, it does, to the end of the buffer of the decoder. In the places where readAll is used that isn't a problem, as the decoder is afterwards thrown away. Changing the offset explicitly here was to protect future users from using readAll multiple times and getting the same content back. That's what I meant with aligning the behavior: All read-ish methods move the offset, i.e. they consume the specific length of bytes from the decoder.

@Nevon
Copy link
Collaborator

Nevon commented Aug 18, 2019

You're completely correct about the copying behavior. I must have been thinking of the behavior of Uint8Array, where slicing does create a new copy.

Since that's the case, I'm all for getting this merged. Modifying the offset is a "breaking" change, but if none of the call sites are really affected, then I agree that it makes sense to align the behavior of the read methods. If we really do need a "read without moving the offset", we can create a method that's more explicit about that.

@Nevon Nevon merged commit 8feb245 into tulios:master Aug 18, 2019
@ankon
Copy link
Contributor Author

ankon commented Aug 19, 2019

Thanks!

@ankon ankon deleted the pr/avoid-copy-of-buffers branch August 19, 2019 07:54
@tulios
Copy link
Owner

tulios commented Aug 19, 2019

Pre-release 1.11.0-beta.6 was published with the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants