buffer: improve allocation and from(arrayLike) performance #10443

Closed
wants to merge 2 commits into
from

Projects

None yet

9 participants

@mscdex
Contributor
mscdex commented Dec 25, 2016 edited
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)
  • buffer
Description of change

Improve Buffer allocation performance (up to ~11%) by making assertSize() inlineable and improve Buffer.from(arrayLike) performance (~50%) by replacing the usage of the in operator when checking for a length property.

Relevant benchmark results:

Buffer allocation:

                                                                   improvement significant      p.value
buffers/buffer-creation.js n=1024 len=10 type="buffer()"                3.67 %         *** 1.907380e-31
buffers/buffer-creation.js n=1024 len=10 type="fast-alloc-fill"         4.34 %         *** 3.497544e-11
buffers/buffer-creation.js n=1024 len=10 type="fast-alloc"              9.94 %         *** 7.596522e-44
buffers/buffer-creation.js n=1024 len=10 type="fast-allocUnsafe"        5.05 %         *** 2.696345e-51
buffers/buffer-creation.js n=1024 len=10 type="slow-allocUnsafe"        3.49 %         *** 1.076653e-07
buffers/buffer-creation.js n=1024 len=10 type="slow"                   11.22 %         *** 5.188153e-61
buffers/buffer-creation.js n=1024 len=1024 type="buffer()"              1.42 %          ** 1.348395e-03
buffers/buffer-creation.js n=1024 len=1024 type="fast-alloc-fill"       2.52 %         *** 1.446582e-06
buffers/buffer-creation.js n=1024 len=1024 type="fast-alloc"            5.38 %         *** 1.283277e-27
buffers/buffer-creation.js n=1024 len=1024 type="fast-allocUnsafe"      2.18 %         *** 2.896226e-06
buffers/buffer-creation.js n=1024 len=1024 type="slow-allocUnsafe"      3.41 %         *** 6.929603e-10
buffers/buffer-creation.js n=1024 len=1024 type="slow"                  9.38 %         *** 1.607505e-56
buffers/buffer-creation.js n=1024 len=2048 type="buffer()"              2.13 %         *** 2.849077e-05
buffers/buffer-creation.js n=1024 len=2048 type="fast-alloc-fill"       2.39 %         *** 1.291052e-07
buffers/buffer-creation.js n=1024 len=2048 type="fast-alloc"            3.96 %         *** 2.701098e-22
buffers/buffer-creation.js n=1024 len=2048 type="fast-allocUnsafe"      1.57 %         *** 5.975363e-04
buffers/buffer-creation.js n=1024 len=2048 type="slow-allocUnsafe"      4.54 %         *** 3.838296e-15
buffers/buffer-creation.js n=1024 len=2048 type="slow"                  8.52 %         *** 6.733714e-46
buffers/buffer-creation.js n=1024 len=4096 type="buffer()"              3.62 %         *** 1.399411e-10
buffers/buffer-creation.js n=1024 len=4096 type="fast-alloc-fill"       1.85 %         *** 2.706257e-05
buffers/buffer-creation.js n=1024 len=4096 type="fast-alloc"            1.30 %         *** 1.625928e-04
buffers/buffer-creation.js n=1024 len=4096 type="fast-allocUnsafe"      4.14 %         *** 4.122709e-15
buffers/buffer-creation.js n=1024 len=4096 type="slow-allocUnsafe"      3.78 %         *** 5.601502e-13
buffers/buffer-creation.js n=1024 len=4096 type="slow"                  8.09 %         *** 6.466906e-46
buffers/buffer-creation.js n=1024 len=8192 type="buffer()"              2.54 %         *** 1.426874e-05
buffers/buffer-creation.js n=1024 len=8192 type="fast-alloc-fill"       1.16 %          ** 1.392665e-03
buffers/buffer-creation.js n=1024 len=8192 type="fast-alloc"            0.83 %          ** 2.391328e-03
buffers/buffer-creation.js n=1024 len=8192 type="fast-allocUnsafe"      5.33 %         *** 1.811794e-19
buffers/buffer-creation.js n=1024 len=8192 type="slow-allocUnsafe"      4.85 %         *** 1.764478e-16
buffers/buffer-creation.js n=1024 len=8192 type="slow"                  8.18 %         *** 1.006915e-42

Buffer.from(arrayLike):

                                                       improvement significant      p.value
buffers/buffer-from.js n=10024 len=10 source="object"       48.85 %         *** 4.055606e-41
buffers/buffer-from.js n=10024 len=2048 source="object"     49.44 %         *** 2.809991e-51

/cc @nodejs/buffer
CI: https://ci.nodejs.org/job/node-test-pull-request/5580/

@mscdex mscdex added the buffer label Dec 25, 2016
@mscdex mscdex added the performance label Dec 25, 2016
@mscdex mscdex changed the title from Buffer allocate perf to buffer: improve allocation and from(arrayLike) performance Dec 25, 2016
lib/buffer.js
@@ -38,6 +38,9 @@ function createUnsafeBuffer(size) {
return new FastBuffer(createUnsafeArrayBuffer(size));
}
+/*function newArrayBuffer(size) {
@cjihrig
cjihrig Dec 25, 2016 Member

Is this intentional?

@mscdex
mscdex Dec 25, 2016 Contributor

Nope. Removed.

@cjihrig

LGTM with a question.

mscdex added some commits Dec 25, 2016
@mscdex mscdex buffer: improve allocation performance
assertSize() is adjusted to be inlineable according to V8's default
function size limits when determining inlineability. This results in
up to 11% performance gains when allocating any kind of Buffer.
b6dba12
@mscdex mscdex buffer: avoid 'in' operator
This change results in ~50% improvement when creating a Buffer from
an array-like object.
801493c
@Trott
Member
Trott commented Dec 25, 2016 edited

Probably too tangentially related to this PR to be worth doing, but just in case: There's a != on line 172 of lib/buffer.js that seems to be calling out to be replaced with !==.

@lpinca
lpinca approved these changes Dec 25, 2016 View changes
@ChALkeR

LGTM after CI.

@mscdex mscdex referenced this pull request Dec 26, 2016
Open

lib,src: make constants not inherit from Object #10458

3 of 3 tasks complete
@jasnell jasnell added a commit that referenced this pull request Dec 27, 2016
@mscdex @jasnell mscdex + jasnell buffer: improve allocation performance
assertSize() is adjusted to be inlineable according to V8's default
function size limits when determining inlineability. This results in
up to 11% performance gains when allocating any kind of Buffer.

Avoid avoids use of in, resulting in ~50% improvement when creating
a Buffer from an array-like object.

PR-URL: #10443
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
13a4887
@jasnell
Member
jasnell commented Dec 27, 2016

Landed in 13a4887

@jasnell jasnell closed this Dec 27, 2016
@mscdex mscdex deleted the mscdex:buffer-allocate-perf branch Dec 29, 2016
@mscdex mscdex referenced this pull request Dec 31, 2016
Merged

http: various performance improvements #10558

3 of 3 tasks complete
@joyeecheung joyeecheung added a commit to joyeecheung/node that referenced this pull request Jan 2, 2017
@mscdex @joyeecheung mscdex + joyeecheung buffer: improve allocation performance
assertSize() is adjusted to be inlineable according to V8's default
function size limits when determining inlineability. This results in
up to 11% performance gains when allocating any kind of Buffer.

Avoid avoids use of in, resulting in ~50% improvement when creating
a Buffer from an array-like object.

PR-URL: nodejs#10443
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
491b44b
@evanlucas evanlucas added a commit that referenced this pull request Jan 3, 2017
@mscdex @evanlucas mscdex + evanlucas buffer: improve allocation performance
assertSize() is adjusted to be inlineable according to V8's default
function size limits when determining inlineability. This results in
up to 11% performance gains when allocating any kind of Buffer.

Avoid avoids use of in, resulting in ~50% improvement when creating
a Buffer from an array-like object.

PR-URL: #10443
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
296c847
@evanlucas evanlucas added a commit that referenced this pull request Jan 3, 2017
@evanlucas evanlucas 2017-01-04, Version 7.4.0 (Current)
Notable changes:

* buffer:
  - Improve performance of Buffer allocation by ~11% (Brian White) #10443
  - Improve performance of Buffer.from() by ~50% (Brian White) #10443
* events: Improve performance of EventEmitter.once() by ~27% (Brian White) #10445
* http: Improve performance of http server by ~7% (Brian White) #6533
89fa67d
@evanlucas evanlucas referenced this pull request Jan 3, 2017
Merged

v7.4.0 release proposal #10589

@evanlucas evanlucas added a commit that referenced this pull request Jan 3, 2017
@mscdex @evanlucas mscdex + evanlucas buffer: improve allocation performance
assertSize() is adjusted to be inlineable according to V8's default
function size limits when determining inlineability. This results in
up to 11% performance gains when allocating any kind of Buffer.

Avoid avoids use of in, resulting in ~50% improvement when creating
a Buffer from an array-like object.

PR-URL: #10443
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2439468
@evanlucas evanlucas added a commit that referenced this pull request Jan 3, 2017
@evanlucas evanlucas 2017-01-04, Version 7.4.0 (Current)
Notable changes:

* buffer:
  - Improve performance of Buffer allocation by ~11% (Brian White) #10443
  - Improve performance of Buffer.from() by ~50% (Brian White) #10443
* events: Improve performance of EventEmitter.once() by ~27% (Brian White) #10445
* http: Improve performance of http server by ~7% (Brian White) #6533
22e9bbf
@evanlucas evanlucas added a commit that referenced this pull request Jan 4, 2017
@mscdex @evanlucas mscdex + evanlucas buffer: improve allocation performance
assertSize() is adjusted to be inlineable according to V8's default
function size limits when determining inlineability. This results in
up to 11% performance gains when allocating any kind of Buffer.

Avoid avoids use of in, resulting in ~50% improvement when creating
a Buffer from an array-like object.

PR-URL: #10443
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
797495a
@evanlucas evanlucas added a commit that referenced this pull request Jan 4, 2017
@evanlucas evanlucas 2017-01-04, Version 7.4.0 (Current)
Notable changes:

* buffer:
  - Improve performance of Buffer allocation by ~11% (Brian White) #10443
  - Improve performance of Buffer.from() by ~50% (Brian White) #10443
* events: Improve performance of EventEmitter.once() by ~27% (Brian White) #10445
* fs: Allow passing Uint8Array to fs methods where Buffers are supported. (Anna Henningsen) #10382
* http: Improve performance of http server by ~7% (Brian White) #6533
* npm: Upgrade to v4.0.5 (Kat Marchán) #10330
5d3dd96
@evanlucas evanlucas added a commit that referenced this pull request Jan 4, 2017
@evanlucas evanlucas 2017-01-04, Version 7.4.0 (Current)
Notable changes:

* buffer:
  - Improve performance of Buffer allocation by ~11% (Brian White) #10443
  - Improve performance of Buffer.from() by ~50% (Brian White) #10443
* events: Improve performance of EventEmitter.once() by ~27% (Brian White) #10445
* fs: Allow passing Uint8Array to fs methods where Buffers are supported. (Anna Henningsen) #10382
* http: Improve performance of http server by ~7% (Brian White) #6533
* npm: Upgrade to v4.0.5 (Kat Marchán) #10330

PR-URL: #10589
4760abc
@evanlucas evanlucas added a commit that referenced this pull request Jan 4, 2017
@evanlucas evanlucas 2017-01-04, Version 7.4.0 (Current)
Notable changes:

* buffer:
  - Improve performance of Buffer allocation by ~11% (Brian White) #10443
  - Improve performance of Buffer.from() by ~50% (Brian White) #10443
* events: Improve performance of EventEmitter.once() by ~27% (Brian White) #10445
* fs: Allow passing Uint8Array to fs methods where Buffers are supported. (Anna Henningsen) #10382
* http: Improve performance of http server by ~7% (Brian White) #6533
* npm: Upgrade to v4.0.5 (Kat Marchán) #10330

PR-URL: #10589
b8f6c1f
@mysticatea mysticatea referenced this pull request in eslint/eslint Jan 19, 2017
Open

Update: rewrite TokenStore (fixes #7810) #7936

@MylesBorins
Member

Adding LTS watch, likely should bake a bit longer before landing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment