buffer: use FastBuffer when fill is set to 0#21989
Closed
ChALkeR wants to merge 1 commit intonodejs:masterfrom
Closed
buffer: use FastBuffer when fill is set to 0#21989ChALkeR wants to merge 1 commit intonodejs:masterfrom
fill is set to 0#21989ChALkeR wants to merge 1 commit intonodejs:masterfrom
Conversation
A large number of libraries seem to use Buffer.alloc(size, 0) instead of just Buffer.alloc(size). We don't need to follow the "create unsafe buffer and fill it" path (i.e. actually allocate and perform fill) in that situation, that is better handled by Uint8Array constructor. Buffer.alloc(size) and Buffer.alloc(size, 0) are equivalent, so use the same code path. Not performing the zero-fill manually and having the underlying memory allocator do it for us can improve speed and reduce the memory usage for situations where Buffer.alloc(size, 0) is used.
addaleax
approved these changes
Jul 26, 2018
BridgeAR
approved these changes
Jul 26, 2018
trivikr
approved these changes
Jul 27, 2018
JungMinu
approved these changes
Jul 27, 2018
cjihrig
approved these changes
Jul 27, 2018
jasnell
approved these changes
Jul 27, 2018
TimothyGu
approved these changes
Jul 27, 2018
Member
|
@ChALkeR - can you please show me where the zero-filling happens with the proposed changes? |
Member
Author
|
@gireeshpunathil It calls
This change makes |
gireeshpunathil
approved these changes
Jul 27, 2018
tniessen
approved these changes
Jul 29, 2018
Member
Member
Member
Member
|
Landed in b07852d |
trivikr
pushed a commit
that referenced
this pull request
Aug 5, 2018
A large number of libraries seem to use Buffer.alloc(size, 0) instead of just Buffer.alloc(size). We don't need to follow the "create unsafe buffer and fill it" path (i.e. actually allocate and perform fill) in that situation, that is better handled by Uint8Array constructor. Buffer.alloc(size) and Buffer.alloc(size, 0) are equivalent, so use the same code path. Not performing the zero-fill manually and having the underlying memory allocator do it for us can improve speed and reduce the memory usage for situations where Buffer.alloc(size, 0) is used. PR-URL: #21989 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
targos
pushed a commit
that referenced
this pull request
Aug 6, 2018
A large number of libraries seem to use Buffer.alloc(size, 0) instead of just Buffer.alloc(size). We don't need to follow the "create unsafe buffer and fill it" path (i.e. actually allocate and perform fill) in that situation, that is better handled by Uint8Array constructor. Buffer.alloc(size) and Buffer.alloc(size, 0) are equivalent, so use the same code path. Not performing the zero-fill manually and having the underlying memory allocator do it for us can improve speed and reduce the memory usage for situations where Buffer.alloc(size, 0) is used. PR-URL: #21989 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This was referenced Aug 16, 2018
This was referenced Aug 16, 2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A large number of libraries seem to use
Buffer.alloc(size, 0)instead of justBuffer.alloc(size).We don't need to follow the «create unsafe buffer and fill it» path (i.e. actually allocate and perform fill) in that situation, that is better handled by
Uint8Arrayconstructor.Buffer.alloc(size)andBuffer.alloc(size, 0)are equivalent, so use the same code path.Not performing the zero-fill manually and having the underlying memory allocator do it for us can improve speed and reduce the memory usage for situations where
Buffer.alloc(size, 0)is used.Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesTests/benchmarks are not included as whether or not there would be improvement depends on the underlying memory allocator behavior (and probably also operating system memory management).
This is the difference on the current Node.js version (v10.7.0) and Linux 4.17.9:
As it can be seen,
Buffer.alloc(size)does not actually consume and/or zero-fill physical memory on the time of construction. Underlying memory allocation mechanism is able to track that the memory is supposed to be zero-filled (ascallocbehaves), and zero-filled pages could be returned on first read.That also affects speed in cases when memory is not read but is just overwritten:
This change makes
Buffer.alloc(size, 0)follow the same code path as more performantBuffer.alloc(size), which should increase speed in some cases where the buffer is overwritten and reduce memory usage in some cases where part of the buffer is later thrown away without being used (i.e. where more memory than needed is allocated temporarily).I have seen usage of
Buffer.alloc(size, 0)in many packages, including, but not limited to mysql2, hdkey, secp256k1 and more./cc @addaleax @bnoordhuis