-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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: improve util.inspect performance #14492
Conversation
This is a huge performance improvement in case of sparse arrays when using util.inspect as the hole will simple be skipped.
lib/util.js
Outdated
index++; | ||
index = i; | ||
visibleLength++; | ||
if (visibleLength === ctx.maxArrayLength) |
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.
Couldn't these previous two lines be merged into if (++visibleLength === ctx.maxArrayLength)
?
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.
Done
lib/util.js
Outdated
break; | ||
const i = +elem; | ||
if (index !== i) { | ||
if (i > 0 === false) |
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.
Can you add a comment on why this is necessary, and different from i <= 0
? (I understand it because I saw an earlier version of the PR, but comments are more for future than now anyway.)
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.
Added
Comments addressed |
Thanks for addressing the issue! I made some benchmarks back when it was opened, so feel free to include them in your pull request if you wish: https://gist.github.com/aqrln/195743d38f632b5b9a8d5944f30cb6af Speaking of which, I observe a performance regression with dense arrays:
Can you reproduce it or is it just me (the machine was a bit loaded with other stuff, so the result might be unreliable)? |
@@ -803,7 +813,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { | |||
str = ctx.stylize('[Setter]', 'special'); | |||
} | |||
} | |||
if (!hasOwnProperty(visibleKeys, key)) { | |||
if (visibleKeys[key] === undefined) { |
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.
It may be worth to eventually make visibleKeys
an ES2015 Set
instead of emulating it via an object, but I don't know how it will affect performance with the current V8 (if it will at all).
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.
When I looked at the code there were a couple more optimizations possible but I tried to be more surgical here. E.g. for arrays there is no need for Object.keys
when using for in
and ctx.seen
should be a Set
instead of an Array
.
@aqrln thanks for the benchmarks. I saw some minor drop as well and changed it now. With Current benchmark:
|
PTAL |
Landed in 95bbb68 |
* improve util.inspect performance This is a huge performance improvement in case of sparse arrays when using util.inspect as the hole will simple be skipped. * use faster visibleKeys property lookup * add inspect-array benchmark PR-URL: #14492 Fixes: #14487 Reviewed-By: Alexey Orlenko <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
* improve util.inspect performance This is a huge performance improvement in case of sparse arrays when using util.inspect as the hole will simple be skipped. * use faster visibleKeys property lookup * add inspect-array benchmark PR-URL: #14492 Fixes: #14487 Reviewed-By: Alexey Orlenko <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
I'm not sure if we want to backport this to v6.x, but I've put it under baking |
@MylesBorins I think it should be backported - especially as there are other commits that should also be backported at some point and they depend on this. But when this is backported #14880 has to be backported as well due to the minor regression introduced here. |
I think enough time has passed to backport this. From this change itself there was no further complain about any regression. What do you think @MylesBorins ? |
Fixes #14487
A hole in a sparse array is now in itself a O(1) operation instead of O(n) where n is the number of coherent elements of the hole.
In addition a
hasOwnProperty
check was replaced with a cheaper one.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
util