GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — buffer: use FastBuffer when `fill` is set to 0 by ChALkeR · Pull Request #21989 · nodejs/node

Conversation

@ChALkeR

@ChALkeR ChALkeR commented Jul 26, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

Tests/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:

> x = Buffer.alloc(1e9); x.length
1000000000
> `${process.memoryUsage().rss / 2**20} MiB`
'31.16796875 MiB'
> x.fill(0x42, 10, 20)
<Buffer 00 00 00 00 00 00 00 00 00 00 42 42 42 42 42 42 42 42 42 42 00 00 00 00 00 ... >
> `${process.memoryUsage().rss / 2**20} MiB`
'31.9921875 MiB'
> x = Buffer.alloc(1e9, 0); x.length
1000000000
> `${process.memoryUsage().rss / 2**20} MiB`
'985.9296875 MiB'

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 (as calloc behaves), 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:

> x = process.hrtime(); Buffer.alloc(2e9).fill(2); process.hrtime(x)
[ 0, 916665006 ]
> x = process.hrtime(); Buffer.alloc(2e9, 0).fill(2); process.hrtime(x)
[ 1, 4762215 ]
> x = process.hrtime(); Buffer.alloc(2e9).fill(2); process.hrtime(x)
[ 0, 970604205 ]
> x = process.hrtime(); Buffer.alloc(2e9, 0).fill(2); process.hrtime(x)
[ 1, 19652513 ]
> x = process.hrtime(); Buffer.alloc(2e9).fill(2); process.hrtime(x)
[ 0, 874969612 ]
> x = process.hrtime(); Buffer.alloc(2e9, 0).fill(2); process.hrtime(x)
[ 1, 30765432 ]

This change makes Buffer.alloc(size, 0) follow the same code path as more performant Buffer.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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bricss and lundibundi reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
@ChALkeR
buffer: use FastBuffer when fill is set to 0
f197e7a
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.
@nodejs-github-bot

nodejs-github-bot commented Jul 26, 2018

Copy link
Copy Markdown
Collaborator

@ChALkeR build started: https://ci.nodejs.org/blue/organizations/jenkins/node-test-pull-request-lite-pipeline/detail/node-test-pull-request-lite-pipeline/361/pipeline

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot nodejs-github-bot added the buffer Issues and PRs related to the buffer subsystem. label Jul 26, 2018
addaleax
addaleax approved these changes Jul 26, 2018
View reviewed changes
BridgeAR
BridgeAR approved these changes Jul 26, 2018
View reviewed changes
trivikr
trivikr approved these changes Jul 27, 2018
View reviewed changes
JungMinu
JungMinu approved these changes Jul 27, 2018
View reviewed changes
cjihrig
cjihrig approved these changes Jul 27, 2018
View reviewed changes
jasnell
jasnell approved these changes Jul 27, 2018
View reviewed changes
TimothyGu
TimothyGu approved these changes Jul 27, 2018
View reviewed changes
@gireeshpunathil

gireeshpunathil commented Jul 27, 2018

Copy link
Copy Markdown
Member

@ChALkeR - can you please show me where the zero-filling happens with the proposed changes?
[ On a side note, Ben is out of office ]

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ChALkeR

ChALkeR commented Jul 27, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

@gireeshpunathil It calls new FastBuffer(size) on line 282, which inherits from Uint8Array, which is zero-filled by default. That is the same code path that is followed when fill is not defined, see https://nodejs.org/api/buffer.html#buffer_class_method_buffer_alloc_size_fill_encoding:

If fill is undefined, the Buffer will be zero-filled.

This change makes fill === 0 case follow the same code path as fill === undefined case.

gireeshpunathil reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gireeshpunathil
gireeshpunathil approved these changes Jul 27, 2018
View reviewed changes
tniessen
tniessen approved these changes Jul 29, 2018
View reviewed changes
@trivikr

trivikr commented Aug 1, 2018

Copy link
Copy Markdown
Member

CI: https://ci.nodejs.org/job/node-test-pull-request/16124/

ChALkeR reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trivikr trivikr added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Aug 1, 2018
@addaleax

addaleax commented Aug 2, 2018

Copy link
Copy Markdown
Member

Resume CI: https://ci.nodejs.org/job/node-test-pull-request/16151/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trivikr

trivikr commented Aug 3, 2018

Copy link
Copy Markdown
Member

Resumed CI: https://ci.nodejs.org/job/node-test-pull-request/16172/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trivikr

trivikr commented Aug 5, 2018

Copy link
Copy Markdown
Member

Landed in b07852d

ChALkeR reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trivikr trivikr closed this Aug 5, 2018
trivikr pushed a commit that referenced this pull request Aug 5, 2018
@ChALkeR @trivikr
buffer: use FastBuffer when fill is set to 0
b07852d
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 <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
targos pushed a commit that referenced this pull request Aug 6, 2018
@ChALkeR @targos
buffer: use FastBuffer when fill is set to 0
6622ac7
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 <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
@rvagg rvagg mentioned this pull request Aug 13, 2018
Release proposal: v10.9.0 #22295
Merged
This was referenced Aug 16, 2018
2018-08-15 Version 10.9.0 (Current) @rvagg beevelop/docker-android-nodejs#230
Closed
2018-08-15 Version 10.9.0 (Current) @rvagg beevelop/docker-nodejs#215
Closed
2018-08-15 Version 10.9.0 (Current) @rvagg beevelop/docker-android-nodejs#231
Closed
2018-08-15 Version 10.9.0 (Current) @rvagg beevelop/docker-nodejs#216
Closed
2018-08-15 Version 10.9.0 (Current) @rvagg beevelop/docker-android-nodejs#232
Closed
2018-08-15 Version 10.9.0 (Current) @rvagg beevelop/docker-nodejs#217
Closed
2018-08-15 Version 10.9.0 (Current) @rvagg beevelop/docker-android-nodejs#233
Closed
This was referenced Aug 16, 2018
2018-08-15 Version 10.9.0 (Current) @rvagg beevelop/docker-nodejs#218
Closed
2018-08-15 Version 10.9.0 (Current) @rvagg beevelop/docker-android-nodejs#234
Closed
2018-08-15 Version 10.9.0 (Current) @rvagg beevelop/docker-nodejs#219
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jasnell jasnell jasnell approved these changes
@addaleax addaleax addaleax approved these changes
@cjihrig cjihrig cjihrig approved these changes
@tniessen tniessen tniessen approved these changes
@JungMinu JungMinu JungMinu approved these changes
@gireeshpunathil gireeshpunathil gireeshpunathil approved these changes
@BridgeAR BridgeAR BridgeAR approved these changes
@trivikr trivikr trivikr approved these changes
+1 more reviewer
@TimothyGu TimothyGu TimothyGu approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. buffer Issues and PRs related to the buffer subsystem.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

11 participants

@ChALkeR @nodejs-github-bot @gireeshpunathil @trivikr @addaleax @jasnell @TimothyGu @cjihrig @tniessen @JungMinu @BridgeAR
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.