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 — util: improve util inspect performance by BridgeAR · Pull Request #22503 · nodejs/node

Conversation

@BridgeAR

@BridgeAR BridgeAR commented Aug 24, 2018

Copy link
Copy Markdown
Member

This significantly improves the inspection performance for all array
types. From now on only the visible elements cause work instead of
having to process all array keys no matter how many entries are
visible. So the complexity for (typed) arrays is reduced from
O(n + m) to O(m), while n is the number of keys and m is the
number of visible entries. m is set to 100 by default
(maxArrayLength).

This also moves some code out of the main function to reduce the
overall function complexity.

It also adds a specific boxed primitives c++ function to reduce the
boundary crossing for the average use case (boxed primitives are rare).

I also reduced the runtime for the util benchmarks as they were significantly
to high and removed some obsolete options.

Intermediate benchmark results:
 util/inspect-array.js type='denseArray_showHidden' len=100 n=500             ***     16.58 %       ±2.32%   ±3.09%   ±4.02%
 util/inspect-array.js type='denseArray_showHidden' len=100000 n=500          ***  35312.58 %     ±352.42% ±474.97% ±630.57%
 util/inspect-array.js type='denseArray' len=100 n=500                        ***     11.41 %       ±2.29%   ±3.04%   ±3.96%
 util/inspect-array.js type='denseArray' len=100000 n=500                     ***  19361.00 %     ±243.13% ±327.68% ±435.02%
 util/inspect-array.js type='mixedArray' len=100 n=500                        ***     16.69 %       ±2.88%   ±3.83%   ±4.98%
 util/inspect-array.js type='mixedArray' len=100000 n=500                     ***      1.64 %       ±0.53%   ±0.70%   ±0.92%
 util/inspect-array.js type='sparseArray' len=100 n=500                       ***      3.62 %       ±1.40%   ±1.87%   ±2.45%
 util/inspect-array.js type='sparseArray' len=100000 n=500                             0.31 %       ±0.69%   ±0.92%   ±1.20%
 util/inspect-proxy.js n=20000                                                ***     -5.93 %       ±3.01%   ±4.01%   ±5.22%
 util/inspect.js option='none' method='Array' n=20000                         ***      6.69 %       ±0.92%   ±1.23%   ±1.60%
 util/inspect.js option='none' method='Date' n=20000                           **     -5.01 %       ±2.92%   ±3.90%   ±5.13%
 util/inspect.js option='none' method='Error' n=20000                         ***    -11.74 %       ±3.11%   ±4.16%   ±5.44%
 util/inspect.js option='none' method='Number' n=20000                        ***     19.64 %       ±2.57%   ±3.43%   ±4.49%
 util/inspect.js option='none' method='Object_deep_ln' n=20000                         0.61 %       ±1.80%   ±2.42%   ±3.18%
 util/inspect.js option='none' method='Object_empty' n=20000                  ***     -9.55 %       ±2.98%   ±3.99%   ±5.23%
 util/inspect.js option='none' method='Object' n=20000                                -0.94 %       ±1.80%   ±2.39%   ±3.12%
 util/inspect.js option='none' method='Set' n=20000                             *     -4.64 %       ±3.54%   ±4.72%   ±6.15%
 util/inspect.js option='none' method='String_boxed' n=20000                  ***    -11.52 %       ±1.96%   ±2.61%   ±3.40%
 util/inspect.js option='none' method='String_complex' n=20000                ***      7.97 %       ±2.29%   ±3.05%   ±3.97%
 util/inspect.js option='none' method='String' n=20000                        ***     15.03 %       ±3.54%   ±4.73%   ±6.19%
 util/inspect.js option='none' method='TypedArray_extra' n=20000              ***     39.86 %       ±2.39%   ±3.19%   ±4.16%
 util/inspect.js option='none' method='TypedArray' n=20000                    ***     31.53 %       ±3.66%   ±4.91%   ±6.47%
 util/inspect.js option='showHidden' method='Array' n=20000                   ***     23.30 %       ±3.02%   ±4.07%   ±5.39%
 util/inspect.js option='showHidden' method='Date' n=20000                     **     -3.12 %       ±1.92%   ±2.56%   ±3.34%
 util/inspect.js option='showHidden' method='Error' n=20000                           -2.64 %       ±3.82%   ±5.12%   ±6.74%
 util/inspect.js option='showHidden' method='Number' n=20000                  ***     15.16 %       ±2.83%   ±3.77%   ±4.90%
 util/inspect.js option='showHidden' method='Object_deep_ln' n=20000                  -0.62 %       ±1.68%   ±2.26%   ±2.98%
 util/inspect.js option='showHidden' method='Object_empty' n=20000            ***    -10.84 %       ±2.09%   ±2.80%   ±3.66%
 util/inspect.js option='showHidden' method='Object' n=20000                           0.08 %       ±1.86%   ±2.48%   ±3.23%
 util/inspect.js option='showHidden' method='Set' n=20000                             -2.69 %       ±3.61%   ±4.84%   ±6.36%
 util/inspect.js option='showHidden' method='String_boxed' n=20000            ***    -10.19 %       ±1.70%   ±2.26%   ±2.95%
 util/inspect.js option='showHidden' method='String_complex' n=20000          ***      6.34 %       ±2.57%   ±3.42%   ±4.45%
 util/inspect.js option='showHidden' method='String' n=20000                  ***      9.59 %       ±2.45%   ±3.27%   ±4.26%
 util/inspect.js option='showHidden' method='TypedArray_extra' n=20000        ***     60.41 %       ±2.94%   ±3.91%   ±5.08%
 util/inspect.js option='showHidden' method='TypedArray' n=20000              ***     75.31 %       ±1.55%   ±2.07%   ±2.71%
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Sorry, something went wrong.

Uh oh!

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

bricss and ex1st reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
@nodejs-github-bot

nodejs-github-bot commented Aug 24, 2018

Copy link
Copy Markdown
Collaborator

@BridgeAR build started: https://ci.nodejs.org/blue/organizations/jenkins/node-test-pull-request-lite-pipeline/detail/node-test-pull-request-lite-pipeline/660/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 c++ Issues and PRs that require attention from people who are familiar with C++. util Issues and PRs related to the built-in util module. labels Aug 24, 2018
@BridgeAR
BridgeAR force-pushed the improve-util-perf branch from acb806d to 2224329 Compare August 24, 2018 14:45
@BridgeAR

BridgeAR commented Aug 24, 2018 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

CI https://ci.nodejs.org/job/node-test-pull-request/16729/
CI https://ci.nodejs.org/job/node-test-pull-request/16730/
Benchmark https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/228/

All reactions

Sorry, something went wrong.

Uh oh!

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

mscdex
mscdex reviewed Aug 24, 2018
View reviewed changes
Comment thread src/node_types.cc Outdated

@mscdex mscdex Aug 24, 2018 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Would it be faster to do this in javascript somehow? For example: typeof arg === 'object' && arg !== null && arg.constructor === String ?

Sorry, something went wrong.

Uh oh!

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

All reactions

@BridgeAR BridgeAR Aug 24, 2018 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I did not test it but I would not want to use it as it's not a safe check. Someone could easily manipulate the constructor.

const s = new String('test')
s.constructor = Number

Sorry, something went wrong.

Uh oh!

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

All reactions

@mscdex mscdex Aug 24, 2018 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Alright, how about:

if (typeof arg === 'object' &&
    arg !== null &&
    Object.prototype.toString.call(arg) === '[object String]')

? Object.prototype.toString could even be cached at startup in case someone overrides it later on.

The only issue would be symbols and big integers as they don't have constructors that return object types, but at least maybe we could speed up the other type checks if this js method ends up being faster or avoid crossing the C++<->js boundary entirely if we place the symbol and bigint checks last.

Sorry, something went wrong.

Uh oh!

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

All reactions

@BridgeAR BridgeAR Aug 25, 2018 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

That is also not a safe check:

const a = new String('test')
a[Symbol.toStringTag] = 'Foo'
console.log(Object.prototype.toString.call(a))
// '[object Foo]'

I'll move the bigint check in the C++ function down and I'll see if I can work around the performance penalty. Do you expect a lot of people using boxed primitives? Because for all other cases there are now less calls to C++ than before.

Sorry, something went wrong.

Uh oh!

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

All reactions

@BridgeAR BridgeAR Aug 26, 2018 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Seems like the main regression came due to moving the functionality to an extra function. It's probably not inlined anymore as other code is hotter and the main function is already big. I moved it back in to address the performance regression.

Sorry, something went wrong.

Uh oh!

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

All reactions
@BridgeAR BridgeAR added the performance Issues and PRs related to the performance of Node.js. label Aug 24, 2018
jdalton
jdalton reviewed Aug 24, 2018
View reviewed changes
Comment thread src/node_types.cc Outdated

@jdalton jdalton Aug 24, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@BridgeAR 👆 Ideally this helper should have a JS form (that calls it) exposed on util.types like the other isNumberObject, etc.

Sorry, something went wrong.

Uh oh!

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

All reactions

@BridgeAR BridgeAR Aug 25, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

That is automatically the case. But good that you bring it up! I'll move this to an individual commit that also adds documentation for the function.

Sorry, something went wrong.

Uh oh!

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

All reactions
@refack

refack commented Aug 24, 2018

Copy link
Copy Markdown
Contributor

I -1 on this.
IMHO util.inspect is a cold path, and I would much rather stability and readability. Again IMHO this is not a place that is worth the churn:
image

All reactions

Sorry, something went wrong.

Uh oh!

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

@mscdex

mscdex commented Aug 24, 2018

Copy link
Copy Markdown
Contributor

I'm not sure I'd call util.inspect a "cold path". I could see end users using it for logging purposes for example.

BridgeAR and ex1st reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

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

@refack

refack commented Aug 24, 2018

Copy link
Copy Markdown
Contributor

I'm not sure I'd call util.inspect a "cold path". I could see end users using it for logging purposes for example.

Ok. But then I'd assume log appending will generate I/O that will shadow the CPU work by inspect. In anyway I'm not convinced even a 100% improvement here is worth the churn, and possible instability.
If we can find a real world workload that this change improves significantly, I'd be happy to change my mind.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jdalton

jdalton commented Aug 24, 2018

Copy link
Copy Markdown
Member

@refack

This is goodness for util.inspect which is used by the REPL and many console methods. And the helpers introduced in this PR can in turn be goodness for util.types (in a follow-up). @BridgeAR is heavily involved in making improvements to util.inspect. I'm in favor of more of these incremental improvements. Long term I'd like to see inspect moved to its own file or folder of files (it's got enough parts).

BridgeAR and benjamingr reacted with heart emoji
All reactions
  • ❤️ 2 reactions

Sorry, something went wrong.

Uh oh!

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

@refack

refack commented Aug 24, 2018

Copy link
Copy Markdown
Contributor

This is goodness for util.inspect which is used by the REPL and many console methods. And the helpers introduced in this PR can in turn be goodness for util.types (in a follow-up).

Then this needs reframing. Call it util: refactor `util.inspect` and let's judge this on merits of readability and reusability.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jdalton

jdalton commented Aug 24, 2018 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

@refack The PR here doesn't appear to make things any harder to reason about, simplifies an expensive set of operations, and opens the possibility of carrying over useful helpers to util.types.

Would it help if this PR moved/saved any of the source refactoring and cleanup bits for another PR?

BridgeAR and benjamingr reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

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

@BridgeAR

BridgeAR commented Aug 25, 2018

Copy link
Copy Markdown
Member Author

@refack I actually tried to reduce the overall complexity while significantly improving the performance. util.inspect is used a ton and we ran into multiple issues because there is a performance overhead when inspecting bigger objects. Here are some issues and PRs that dealt with util.inspect overhead #19994 #19405 #17791 #6334.

@jdalton

Long term I'd like to see inspect moved to its own file or folder of files

Definitely! I want to do that as well.

All reactions

Sorry, something went wrong.

Uh oh!

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

@BridgeAR

BridgeAR commented Aug 26, 2018 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

I just pushed two new commits: one to document the new function and one to address the boxed regression.

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

All reactions

Sorry, something went wrong.

Uh oh!

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

@BridgeAR

BridgeAR commented Aug 26, 2018

Copy link
Copy Markdown
Member Author

@nodejs/util PTAL

All reactions

Sorry, something went wrong.

Uh oh!

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

@BridgeAR
BridgeAR requested review from addaleax and mscdex August 27, 2018 13:21
@BridgeAR BridgeAR assigned mcollina, jasnell, benjamingr and devsnek and unassigned mcollina, jasnell, benjamingr and devsnek Aug 27, 2018
@BridgeAR
BridgeAR requested review from benjamingr, devsnek and jasnell August 27, 2018 13:22
@BridgeAR BridgeAR closed this Sep 7, 2018
@targos targos added the backport-requested-v10.x label Sep 8, 2018
@targos

targos commented Sep 8, 2018

Copy link
Copy Markdown
Member

Should this be backported to v10.x-staging? If yes please follow the guide and raise a backport PR, if not let me know or add the dont-land-on label.

All reactions

Sorry, something went wrong.

Uh oh!

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

@targos targos mentioned this pull request Sep 15, 2018
[v10.x needs a backport] - util: improve util inspect performance #22872
Closed
targos pushed a commit to targos/node that referenced this pull request Sep 23, 2018
@BridgeAR @targos
util: remove outdated TODO
3f0e52e
PR-URL: nodejs#22503
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: John-David Dalton <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
targos pushed a commit to targos/node that referenced this pull request Sep 23, 2018
@BridgeAR @targos
benchmark: refactor util benchmarks
0560cd0
This significantly reduces the benchmark runtime. It removes to many
variations that do not provide any benefit and reduces the iterations.

PR-URL: nodejs#22503
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: John-David Dalton <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
targos pushed a commit to targos/node that referenced this pull request Sep 23, 2018
@BridgeAR @targos
util: improve inspect performance
15b0c80
This significantly improves the inspection performance for all array
types. From now on only the visible elements cause work instead of
having to process all array keys no matter how many entries are
visible.

This also moves some code out of the main function to reduce the
overall function complexity.

PR-URL: nodejs#22503
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: John-David Dalton <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
@targos targos mentioned this pull request Sep 23, 2018
[v10.x] Backport util PRs #23039
Closed
targos pushed a commit that referenced this pull request Sep 24, 2018
@BridgeAR @targos
util: remove outdated TODO
92e0f38
Backport-PR-URL: #23039
PR-URL: #22503
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: John-David Dalton <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
targos pushed a commit that referenced this pull request Sep 24, 2018
@BridgeAR @targos
benchmark: refactor util benchmarks
90bbab6
This significantly reduces the benchmark runtime. It removes to many
variations that do not provide any benefit and reduces the iterations.

Backport-PR-URL: #23039
PR-URL: #22503
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: John-David Dalton <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
targos pushed a commit that referenced this pull request Sep 24, 2018
@BridgeAR @targos
util: improve inspect performance
5a13e66
This significantly improves the inspection performance for all array
types. From now on only the visible elements cause work instead of
having to process all array keys no matter how many entries are
visible.

This also moves some code out of the main function to reduce the
overall function complexity.

Backport-PR-URL: #23039
PR-URL: #22503
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: John-David Dalton <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
@targos targos added backported-to-v10.x and removed author ready PRs with CI started, the required approvals, and no outstanding review comments. backport-requested-v10.x labels Sep 24, 2018
@targos targos mentioned this pull request Oct 7, 2018
Release proposal: v10.12.0 #23313
Merged
This was referenced Oct 10, 2018
2018-10-10 Version 10.12.0 (Current) @targos beevelop/docker-nodejs#225
Closed
2018-10-10 Version 10.12.0 (Current) @targos beevelop/docker-android-nodejs#239
Closed
2018-10-10 Version 10.12.0 (Current) @targos beevelop/docker-nodejs#226
Closed
2018-10-10 Version 10.12.0 (Current) @targos beevelop/docker-android-nodejs#240
Closed
2018-10-10 Version 10.12.0 (Current) @targos beevelop/docker-nodejs#227
Closed
2018-10-10 Version 10.12.0 (Current) @targos beevelop/docker-android-nodejs#241
Closed
2018-10-10 Version 10.12.0 (Current) @targos beevelop/docker-android-nodejs#242
Closed
2018-10-10 Version 10.12.0 (Current) @targos beevelop/docker-nodejs#228
Closed
2018-10-10 Version 10.12.0 (Current) @targos beevelop/docker-android-nodejs#243
Closed
2018-10-10 Version 10.12.0 (Current) @targos beevelop/docker-nodejs#229
Closed
@BridgeAR BridgeAR mentioned this pull request Dec 20, 2019
for ... of replacing for(;;) in library networking code? #31024
Closed
@BridgeAR
BridgeAR deleted the improve-util-perf branch January 20, 2020 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@mcollina mcollina mcollina approved these changes
@benjamingr benjamingr benjamingr approved these changes
@addaleax addaleax Awaiting requested review from addaleax
@mscdex mscdex Awaiting requested review from mscdex
@devsnek devsnek Awaiting requested review from devsnek
@jasnell jasnell Awaiting requested review from jasnell
@TimothyGu TimothyGu Awaiting requested review from TimothyGu
@joyeecheung joyeecheung Awaiting requested review from joyeecheung
@ryzokuken ryzokuken Awaiting requested review from ryzokuken
+2 more reviewers
@refack refack refack approved these changes
@jdalton jdalton jdalton 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

c++ Issues and PRs that require attention from people who are familiar with C++. performance Issues and PRs related to the performance of Node.js. util Issues and PRs related to the built-in util module.

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.

10 participants

@BridgeAR @nodejs-github-bot @refack @mscdex @jdalton @targos @mcollina @benjamingr @jasnell @devsnek
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.