Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: fix inspect performance bug #20007

Closed
wants to merge 1 commit into from

Conversation

BridgeAR
Copy link
Member

In case an object contained a circular reference Object.keys was
called even though it was not necessary at all. This caused a
significant overhead for objects that contained a lot of such entries.

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

In case an object contained a circular reference `Object.keys` was
called even though it was not necessary at all. This caused a
significant overhead for objects that contained a lot of such entries.
@BridgeAR BridgeAR added the fast-track PRs that do not need to wait for 48 hours to land. label Apr 13, 2018
@nodejs-github-bot nodejs-github-bot added the util Issues and PRs related to the built-in util module. label Apr 13, 2018
@BridgeAR
Copy link
Member Author

@BridgeAR BridgeAR added the performance Issues and PRs related to the performance of Node.js. label Apr 13, 2018
@@ -445,6 +445,11 @@ function formatValue(ctx, value, recurseTimes, ln) {
}
}

// Using an array here is actually better for the average case than using
// a Set. `seen` will only check for the depth and will never grow too large.
if (ctx.seen.indexOf(value) !== -1)
Copy link
Contributor

Choose a reason for hiding this comment

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

.includes() unless it has a performance penalty?

Copy link
Member Author

Choose a reason for hiding this comment

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

It had a performance penalty the last time I checked.

Copy link
Member

Choose a reason for hiding this comment

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

indexOf should be much faster than anything else.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it is a case for @nodejs/v8 performance group to look into then.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

I would love to have/see a benchmark for this.

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 13, 2018
@BridgeAR
Copy link
Member Author

@mcollina I tested a extreme case:

'use strict';

const util = require('util');

let i = 0;
const obj = {};

while (i++ < 1500) {
  obj[i] = {};
}

const circular = {};

for (const k of Object.keys(obj)) {
  circular[k] = obj[k];
  obj[k].circular = circular;
  obj[k].obj = obj;
}

console.time('run');
util.inspect(obj);
console.timeEnd('run');

// Before the patch:
// run: 165971.450ms

// After the patch:
// run: 7377.811ms

@richardlau richardlau removed the fast-track PRs that do not need to wait for 48 hours to land. label Apr 13, 2018
@richardlau
Copy link
Member

I don't believe this qualifies for fast tracking based on the guidance given in https://github.com/nodejs/node/blob/master/COLLABORATOR_GUIDE.md#waiting-for-approvals.

@BridgeAR BridgeAR mentioned this pull request Apr 13, 2018
4 tasks
@BridgeAR
Copy link
Member Author

Landed in f413f56

@BridgeAR BridgeAR closed this Apr 16, 2018
BridgeAR added a commit to BridgeAR/node that referenced this pull request Apr 16, 2018
In case an object contained a circular reference `Object.keys` was
called even though it was not necessary at all. This caused a
significant overhead for objects that contained a lot of such entries.

PR-URL: nodejs#20007
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
jasnell pushed a commit that referenced this pull request Apr 16, 2018
In case an object contained a circular reference `Object.keys` was
called even though it was not necessary at all. This caused a
significant overhead for objects that contained a lot of such entries.

PR-URL: #20007
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
@BridgeAR BridgeAR deleted the fix-circular-util-inspect branch April 1, 2019 23:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. 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
Development

Successfully merging this pull request may close these issues.