Skip to content

util: improve spliceOne perf - #20453

Closed
apapirovski wants to merge 3 commits into
nodejs:masterfrom
apapirovski:patch-splice-one-perf
Closed

util: improve spliceOne perf#20453
apapirovski wants to merge 3 commits into
nodejs:masterfrom
apapirovski:patch-splice-one-perf

Conversation

@apapirovski

Copy link
Copy Markdown
Contributor

Do less variable allocations and reassignments inside spliceOne since it's relied on by some performance sensitive code. This also adds a benchmark for spliceOne.

 confidence improvement accuracy (*) (**) (***)
util/splice.js size=10 n=10000000 ** 2.95 % ±1.83% ±2.46% ±3.26%
util/splice.js size=100 n=10000000 *** 10.68 % ±1.46% ±1.98% ±2.66%
util/splice.js size=500 n=10000000 *** 11.26 % ±1.23% ±1.65% ±2.19%
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

Do less variable allocations and reassignments inside spliceOne
since it's relied on by some performance sensitive code.
confidence improvement
util/splice.js size=10 n=10000000 ** 2.95 %
util/splice.js size=100 n=10000000 *** 10.68 %
util/splice.js size=500 n=10000000 *** 11.26 %
@apapirovskiapapirovski added the util Issues and PRs related to the built-in util module. label May 1, 2018
@apapirovski

Copy link
Copy Markdown
ContributorAuthor

@BridgeARBridgeAR left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with the comment addressed.

Comment threadlib/internal/util.js Outdated

// About 1.5x faster than the two-arg version of Array#splice().
// Depending on the size of the array, this is anywhere between 1.5-10x
// faster than the two-arg version of Array#splice()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the V8 version this is tested with. And there is a new faster version that is currently deactivated. See https://bugs.chromium.org/p/v8/issues/detail?id=7221

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

}, { flags: ['--expose-internals'] });

function main({ n, size, type }) {
const { spliceOne } = require('internal/util');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, why put this here?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benchmark runs with --expose-internals so the main function has access to them but the outside doesn't.

@mscdex

Copy link
Copy Markdown
Contributor

It might be a good idea to also add other special cases for the benchmark, such as splicing the first element and the last element.

@apapirovski

Copy link
Copy Markdown
ContributorAuthor

Made all the requested changes.

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

@mscdex

Copy link
Copy Markdown
Contributor

What do the benchmark results look like for the newly added cases?

@apapirovski

Copy link
Copy Markdown
ContributorAuthor

@mscdex Didn't have time for 100 runs but here's 30:

 util/splice-one.js size=10 pos='end' n=10000000 * 1.36 % ±1.25% ±1.66% ±2.17%
util/splice-one.js size=10 pos='middle' n=10000000 *** 4.31 % ±1.90% ±2.53% ±3.29%
util/splice-one.js size=10 pos='start' n=10000000 *** 7.90 % ±1.38% ±1.84% ±2.40%
util/splice-one.js size=100 pos='end' n=10000000 -0.33 % ±1.78% ±2.37% ±3.09%
util/splice-one.js size=100 pos='middle' n=10000000 *** 12.21 % ±1.07% ±1.43% ±1.86%
util/splice-one.js size=100 pos='start' n=10000000 *** 9.94 % ±1.12% ±1.49% ±1.95%
util/splice-one.js size=500 pos='end' n=10000000 * 1.60 % ±1.34% ±1.78% ±2.33%
util/splice-one.js size=500 pos='middle' n=10000000 *** 11.28 % ±1.04% ±1.38% ±1.80%
util/splice-one.js size=500 pos='start' n=10000000 *** 12.45 % ±0.84% ±1.12% ±1.46%

@apapirovskiapapirovski added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 2, 2018
@apapirovski

Copy link
Copy Markdown
ContributorAuthor

Landed in b04d092

@apapirovski
apapirovski deleted the patch-splice-one-perf branch May 3, 2018 12:47
apapirovski added a commit that referenced this pull request May 3, 2018
Do less variable allocations and reassignments inside spliceOne
since it's relied on by some performance sensitive code.
PR-URL: #20453
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
MylesBorins pushed a commit that referenced this pull request May 4, 2018
Do less variable allocations and reassignments inside spliceOne
since it's relied on by some performance sensitive code.
PR-URL: #20453
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
@MylesBorinsMylesBorins mentioned this pull request May 8, 2018
MylesBorins pushed a commit that referenced this pull request May 8, 2018
Do less variable allocations and reassignments inside spliceOne
since it's relied on by some performance sensitive code.
PR-URL: #20453
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.utilIssues and PRs related to the built-in util module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@apapirovski@mscdex@danbev@jasnell@addaleax@lpinca@TimothyGu@tniessen@BridgeAR@trivikr