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 — lib: Prevent leaking arguments in several places. by ChALkeR · Pull Request #1752 · nodejs/node

Conversation

@ChALkeR

@ChALkeR ChALkeR commented May 20, 2015

Copy link
Copy Markdown
Member

This prevents leaking arguments in several places.

Not yet final, I will probably update this with more patches, waiting for comments.

Currently the three fixed places are util.format (called for example by console.log), console.assert, and path.win32.join.

In path.win32.join this PR changes the stack of the TypeError('Arguments to path.join must be strings'), removing the top at f (path.js:190:13) and at Object.filter (native) so now it is similar to the stack in path.posix.join.

util.format results:

  • ~50% speedup on util.format('abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc1234567890') (16700000 ⇒ 32900000 ops/second).
  • ~8% speedup on util.format({a:2,b:10,c:10},20,'d') (50000 ⇒ 54000 ops/second).
  • ~28% speedup on util.format('Hello %s %s',20,'d') (580000 ⇒ 800000 ops/second).

console.assert results:

  • ~27% speedup on common console.assert(true, 'text') (2550000 ⇒ 3230000 ops/second).
  • ~23% speedup on console.assert(true) ( 3000000 ⇒ 3700000 ops/second).

path.win32.join results:

  • ~33% speedup on path.join('a','b','c') (400000 ⇒ 598000 ops/second).

See #1749 (comment), https://gist.github.com/Fishrock123/98c35a0c745cb59d7496 and https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments

Sorry, something went wrong.

Uh oh!

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

All reactions
@ChALkeR

ChALkeR commented May 20, 2015

Copy link
Copy Markdown
Member Author

/cc @Fishrock123

All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR

ChALkeR commented May 20, 2015

Copy link
Copy Markdown
Member Author

The total list of issues found:

  • lib/_tls_wrap.js:860: var args = normalizeConnectArgs(arguments);
  • lib/path.js:195: var paths = Array.prototype.filter.call(arguments, f); Fixed by this PR.
  • lib/net.js:58: var args = normalizeConnectArgs(arguments);
  • lib/net.js:859: var args = normalizeConnectArgs(arguments);
  • lib/dns.js:64: process.nextTick(callMakeAsyncCbNT, callback, arguments);
  • lib/console.js:86: var arr = Array.prototype.slice.call(arguments, 1); Fixed by this PR.
  • lib/_http_client.js:562: this._deferToConnect('setNoDelay', arguments);
  • lib/_http_client.js:565: this._deferToConnect('setKeepAlive', arguments);
  • lib/util.js:19: var args = arguments; Fixed by this PR.
  • lib/domain.js:239: return intercepted(this, self, cb, arguments); Deprecated
  • lib/domain.js:267: return bound(this, self, cb, arguments); Deprecated
  • lib/crypto.js:655: return filterDuplicates(getCiphers.call(null, arguments));
  • lib/crypto.js:660: return filterDuplicates(getHashes.call(null, arguments));

I'm not sure if fixing most of those matters, but at least some should be fixed imo.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR
ChALkeR force-pushed the prevent-leaking-arguments branch from e917608 to bdf3e6f Compare May 21, 2015 00:14
@Fishrock123

Fishrock123 commented May 21, 2015

Copy link
Copy Markdown
Contributor

If I get time, I'll run IRHydra against my patch and see what comes out. That would be the best first step, to compare the actual compiled result.

All reactions

Sorry, something went wrong.

Uh oh!

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

brendanashworth
brendanashworth reviewed May 22, 2015
View reviewed changes
Comment thread lib/path.js Outdated

@brendanashworth brendanashworth May 22, 2015

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.

Perhaps specifying the length up front, var paths = new Array(arguments.length); and assigning at the index would be faster?

Sorry, something went wrong.

Uh oh!

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

All reactions

@ChALkeR ChALkeR May 22, 2015

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.

@brendanashworth Done.

Sorry, something went wrong.

Uh oh!

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

All reactions
@ChALkeR
ChALkeR force-pushed the prevent-leaking-arguments branch from bdf3e6f to dd51d5b Compare May 22, 2015 23:15
@chrisdickinson

chrisdickinson commented May 22, 2015

Copy link
Copy Markdown
Contributor

A while back, we had support for macro expansion in our native modules. If this is proving to be a generally viable speedup, it would be nice to put it in macro form so that it's easy to rip out again if V8 changes things.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR ChALkeR mentioned this pull request May 23, 2015
path: refactor for performance and consistency #1778
Closed
bnoordhuis
bnoordhuis reviewed May 24, 2015
View reviewed changes
Comment thread lib/console.js Outdated

@bnoordhuis bnoordhuis May 24, 2015

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.

There's another obvious performance optimization here: cache the result of require('assert'). :-)

Sorry, something went wrong.

Uh oh!

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

All reactions
@bnoordhuis

bnoordhuis commented May 24, 2015

Copy link
Copy Markdown
Member

A comment and a question:

  1. al is a terrible name, prefer something like argslen or argc. (argc is terrible too but at least it has a well-understood meaning.)
  2. Is there a reason to use var instead of const?
All reactions

Sorry, something went wrong.

Uh oh!

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

@Fishrock123

Fishrock123 commented May 24, 2015

Copy link
Copy Markdown
Contributor

@bnoordhuis

2: Iirc last time we checked const is still slower than var.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bnoordhuis

bnoordhuis commented May 24, 2015

Copy link
Copy Markdown
Member

If you're referring to the OSR issue, I think I only saw that with let, not const. Also, it was a while ago, it would be good to retest.

All reactions

Sorry, something went wrong.

Uh oh!

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

@brendanashworth

brendanashworth commented May 24, 2015

Copy link
Copy Markdown
Contributor

this jsperf shows that const is faster than var right now

All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR

ChALkeR commented May 24, 2015

Copy link
Copy Markdown
Member Author

@Fishrock123, @bnoordhuis Compound assignment deoptimizes let: let x = 10; x += 1; is slow in both 4.2 and 4.3, but let x = 10; x = x + 1; is fine. This shouldn't be an issue for const because you don't want compound assigments with const =).

All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR
lib: Prevent leaking arguments in several places.
8bf79b4
@ChALkeR
ChALkeR force-pushed the prevent-leaking-arguments branch from dd51d5b to 8bf79b4 Compare May 24, 2015 19:19
@ChALkeR

ChALkeR commented May 24, 2015

Copy link
Copy Markdown
Member Author

@bnoordhuis Done:

  • assert is now cached;
  • al got renamed to argsleft because that's the number of not yet processed/copied arguments;
  • arr and paths are now const.
All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR

ChALkeR commented May 24, 2015

Copy link
Copy Markdown
Member Author

@brendanashworth Ow. That jsperf shows that var is faster than const right in Chromium 43.
Will test in iojs + 4.2 and iojs + 4.3 in a moment.
Edit: I would say that there is no difference between var and const performance for the testcase supplied above (as the jsperf link) in iojs + 4.2 or 4.3.

All reactions

Sorry, something went wrong.

Uh oh!

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

@Fishrock123

Fishrock123 commented May 24, 2015

Copy link
Copy Markdown
Contributor

We should probably move to preferring const (where correct/possible) in all new patches then.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bnoordhuis

bnoordhuis commented May 24, 2015

Copy link
Copy Markdown
Member

LGTM

All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR

ChALkeR commented May 24, 2015

Copy link
Copy Markdown
Member Author

Should I patch other places from #1752 (comment) ?

All reactions

Sorry, something went wrong.

Uh oh!

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

@Fishrock123

Fishrock123 commented May 24, 2015

Copy link
Copy Markdown
Contributor

@ChALkeR yes please :)

All reactions

Sorry, something went wrong.

Uh oh!

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

@trevnorris

trevnorris commented May 26, 2015

Copy link
Copy Markdown
Contributor

The let performance patch landed in v8 4.4.30.

All reactions

Sorry, something went wrong.

Uh oh!

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

@chrisdickinson

chrisdickinson commented May 26, 2015

Copy link
Copy Markdown
Contributor

I'd be a lot more comfortable with this if it were reintroduced as a macro. It'd be a lot easier to document / spread knowledge about its use, as well as to back out of it if perf characteristics change.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR

ChALkeR commented May 26, 2015

Copy link
Copy Markdown
Member Author

@chrisdickinson This is a temporary solution anyways.
The proper way would be using rest params.

Related: https://code.google.com/p/v8/issues/detail?id=2159
Once this is properly implemented and optimized (as it should be), it should be used instead of arguments and copy-arguments-into-an-array methods.

A macro (and documenting it) seems like a bit undue for a temporary hack.

All reactions

Sorry, something went wrong.

Uh oh!

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

@trevnorris

trevnorris commented May 26, 2015

Copy link
Copy Markdown
Contributor

@ChALkeR In my experience rest params are already almost on par with using arguments. It's the spread operator that will eat your face.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR

ChALkeR commented May 26, 2015

Copy link
Copy Markdown
Member Author

@trevnorris arguments is not an array, also it introduces a deopt if you do anything to it except taking arguments.length (in fact this also should be done carefully) and accessing arguments[i] where 0 <= i < arguments.length. This is why manually copying arguments to an array and passing that array is faster than passing the arguments object.

«On par with arguments» is not good enough. Afaik, rest params are deoptimized (or, rather, not yet optimized) for now. Also, I am not sure what is the v8 version requirements for them to work without issues. And they are behind a flag.

See https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments.

All reactions

Sorry, something went wrong.

Uh oh!

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

@trevnorris

trevnorris commented May 27, 2015

Copy link
Copy Markdown
Contributor

@ChALkeR I'm aware of all that. Was just stating that rest params being well optimized isn't far off.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR

ChALkeR commented May 27, 2015

Copy link
Copy Markdown
Member Author

@trevnorris Yes. And that's why I don't see an advantage in hacking this with something more complex, as macro (and documenting it), as @chrisdickinson proposed here: #1752 (comment)

All reactions

Sorry, something went wrong.

Uh oh!

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

@bnoordhuis
bnoordhuis force-pushed the master branch from 311c3e4 to b926718 Compare June 2, 2015 17:25
@rvagg
rvagg force-pushed the master branch from 9d21135 to 628a3ab Compare June 25, 2015 09:18
@indutny
indutny force-pushed the master branch from bffb204 to eb35968 Compare July 22, 2015 21:21
@rvagg
rvagg force-pushed the master branch from 11c25c2 to ba02bd0 Compare September 6, 2015 11:55
@jasnell

jasnell commented Oct 22, 2015

Copy link
Copy Markdown
Member

@ChALkeR @trevnorris ... what's the status on this?

All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR

ChALkeR commented Oct 22, 2015

Copy link
Copy Markdown
Member Author

@jasnell I need to re-apply this (some places mentioned in the above list were already fixed by other patches), and then re-test to see if there is any actual improvement with the current v8 version in master.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jasnell jasnell added the lib / src Issues and PRs involving general changes in the lib/ or src/ directories. label Nov 16, 2015
@ChALkeR

ChALkeR commented Dec 20, 2015

Copy link
Copy Markdown
Member Author

Closing, obsolete. See #4361, also current master has rest parameters enabled by default, perhaps this should be redone using those.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ChALkeR ChALkeR closed this Dec 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

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

Assignees

No one assigned

Labels

lib / src Issues and PRs involving general changes in the lib/ or src/ directories.

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.

7 participants

@ChALkeR @Fishrock123 @chrisdickinson @bnoordhuis @brendanashworth @trevnorris @jasnell
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.