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

lib: replace self/bind usage with arrow func #3816

Closed
wants to merge 3 commits into from

Conversation

tflanagan
Copy link
Contributor

This commit replaces trivial occurances of var self = this and .bind()
where appropriate.


FWIW, passes local tests.

May be adding to this over the next day - this is the result of a couple simple greps.

This is in parallel to #3622.

@cjihrig
Copy link
Contributor

cjihrig commented Nov 13, 2015

LGTM if the CI is happy

@jasnell jasnell added the lib / src Issues and PRs related to general changes in the lib or src directory. label Nov 13, 2015
@mscdex
Copy link
Contributor

mscdex commented Nov 13, 2015

I'm curious as to any performance impact this may have. I know that bind() is slow, but how does it compare to arrow functions or even using var self = this; and an anonymous function?

Does anyone have actual numbers to show?

@Fishrock123
Copy link
Contributor

@mscdex Afaik Arrow Functions have identical overhead to regular functions.

@Fishrock123
Copy link
Contributor

And regarding bind, to my knowledge it is specifically making the bound function that is slow, not running it.

@mscdex
Copy link
Contributor

mscdex commented Nov 13, 2015

@Fishrock123 A quick google search turns up this jsperf. So at first glance it seems like it's not just the creation of bound functions that is slow? At any rate, if that jsperf is trustworthy, then arrow functions should be as fast as self = this and both are much faster than functions bound with bind().

self.server = null;
this.server.once('listening', () => {
this.handle = this.server._handle;
this.handle.onconnection = this.distribute.call(this);
Copy link

Choose a reason for hiding this comment

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

Should this still be a bind (or another arrow function)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, this should not be a .call(), otherwise the function will be immediately invoked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, forgot to wrap that in a () => {}. One sec...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually...

What's better?

this.handle.onconnection = () => this.distribute.call(this);

or

this.handle.onconnection = this.distribute.bind(this);

One is one and done and the other is an anon function that invokes .call each time

Copy link

Choose a reason for hiding this comment

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

Since you have access to this, can't you just do the following?

this.handle.onconnection = () => this.distribute();

Although it looks like .distribute expects some arguments, so .bind might be more appropriate here (I'm not familiar with cluster internals, so that's really just speculative).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Long day. @lucthev, you're 100% right. I'll revisit after some shuteye.

@tflanagan tflanagan force-pushed the trivial-arr-func branch 4 times, most recently from 65e152c to 3c8630e Compare November 16, 2015 15:22
@tflanagan
Copy link
Contributor Author

Okay, looks like I've got all of the non-stacktrace changing (named functions like onDone), trivial self/bind usages that were not addressed in parallel PR.

FWIW, passing all local tests

self.send.apply(self, self._sendQueue[i]);
self._sendQueue = undefined;
for (var i = 0; i < this._sendQueue.length; i++)
this.send.apply(this, this._sendQueue[i]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this is worthy of a comment or not.

Very interestingly, rewriting this to this.send(this._sendQueue[i]); blew up the dgram tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

@tflanagan Note that it is apply and not call, that is, it applies like so:

this.send.call(this, this._sendQueue[i][0], this._sendQueue[i][1], etc...);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Fishrock123 right, dumb moment, don't mind me. ( thank you ;) )

Copy link
Member

Choose a reason for hiding this comment

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

I think we can do spread call now: this.send(...this._sendQueue[i])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@targos I remember reading that the spread operator isn't too friendly to v8 optimizations and to performance in general.

@tflanagan tflanagan force-pushed the trivial-arr-func branch 4 times, most recently from afa939a to 9df8a7f Compare November 16, 2015 21:14
@tflanagan
Copy link
Contributor Author

@jasnell I've reverted that constructor and also rebase'd with the newest lib/_debugger.js changes.

@tflanagan
Copy link
Contributor Author

I've just updated this PR to match master.

/poke


// Don't display any default messages
var listeners = this.repl.rli.listeners('SIGINT').slice(0);
this.repl.rli.removeAllListeners('SIGINT');

function exitDebugRepl() {
var exitDebugRepl = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

const?

@silverwind
Copy link
Contributor

Please don't force-push after a review the next time, it makes it very hard for me to track the new changes.

@tflanagan
Copy link
Contributor Author

I'm sorry - I'm running into errors with local tests, will commit fixes (wont force), in a moment

This commit replaces trivial occurances of var self = this and .bind()
where appropriate.
@tflanagan tflanagan force-pushed the trivial-arr-func branch 2 times, most recently from d5608cf to c780fcd Compare January 28, 2016 23:26
@tflanagan
Copy link
Contributor Author

I'm done toying now :). Passes local tests fwiw (minus some flaky ones that happen on master too)

@jasnell
Copy link
Member

jasnell commented Jan 29, 2016

LGTM

@silverwind
Copy link
Contributor

@tflanagan
Copy link
Contributor Author

Any reason those CI's aren't public? Would love to see if there some errors I can fix

@jasnell
Copy link
Member

jasnell commented Jan 29, 2016

Yeah, we're currently testing the security fixes for next weeks release using the CI. Because the details of the fix are embargoed until the release we need to restrict access to the CI. Once we have the fixes fully tested the full CI will be made public again. Standard procedure when we have security updates pending! I think @rvagg just forgot to mention that in the security release notification posted the other day.

@jasnell
Copy link
Member

jasnell commented Jan 29, 2016

Just looking at the CI now and it's almost all red. There are a ton of failures on this one unfortunately :-( .. I'm a bit tied up currently but here shortly I'll post some of the failures here for review

@tflanagan
Copy link
Contributor Author

Oh boy, sounds like some fun heading my way. ;) Thanks!

-- I'll build it on my own boxes until then

@silverwind
Copy link
Contributor

Not sure how far @jasnell got, but here are some OS X failures:

not ok 514 test-listen-fd-cluster.js
# TIMEOUT
# Cluster listen fd test runner
# about to listen in parent
# server listening on 12346
# master spawned
# Cluster listen fd test master
# in master, spawning worker
# Cluster listen fd test worker
# worker, about to create server and listen on fd=3
# worker listening on fd=3

not ok 862 test-tls-ticket-cluster.js
# TIMEOUT
# [master] got "listening"
# [master] connecting
# [master] got "listening"
# [master] got "listening"
# [master] got "listening"

not ok 87 test-cluster-disconnect.js
# TIMEOUT

not ok 100 test-cluster-http-pipe.js
# TIMEOUT

not ok 103 test-cluster-message.js
# TIMEOUT

not ok 109 test-cluster-send-handle-twice.js
# TIMEOUT

@tflanagan
Copy link
Contributor Author

Thanks, @silverwind, I see those on my debian box too, I'm working on it.

@jasnell
Copy link
Member

jasnell commented Feb 1, 2016

@silverwind .. thank you, @tflanagan sorry I didn't get the details like I said I would, ended up completely sidetracked for the rest of that day :-/

@tflanagan
Copy link
Contributor Author

Just an update, I've been really busy these last couple months, I plan in fixing this soon. Its just a little low on priorities.

The main deterrent is the lack of view rights on ci's. I don't see all the errors locally...

@jasnell
Copy link
Member

jasnell commented Feb 19, 2016

@tflanagan ... no worries, completely understandable. The CI visibility is getting to be a major pain. Hopefully you'll be onboarded soon so you can access it.

@silverwind
Copy link
Contributor

All failures on the Ubuntu 14.04 x64 box:

not ok 87 test-cluster-disconnect.js
# TIMEOUT
  ---
  duration_ms: 60.96
  ...
not ok 100 test-cluster-http-pipe.js
# TIMEOUT
  ---
  duration_ms: 60.78
  ...
not ok 103 test-cluster-message.js
# TIMEOUT
  ---
  duration_ms: 60.85
  ...
not ok 109 test-cluster-send-handle-twice.js
# TIMEOUT
  ---
  duration_ms: 60.101
  ...
not ok 131 test-cluster-worker-wait-server-close.js
# TIMEOUT
  ---
  duration_ms: 60.97
  ...
not ok 514 test-listen-fd-cluster.js
# TIMEOUT
# Cluster listen fd test runner
# about to listen in parent
# server listening on 12346
# master spawned
# Cluster listen fd test master
# in master, spawning worker
# Cluster listen fd test worker
# worker, about to create server and listen on fd=3
# worker listening on fd=3
# master exit on disconnect
  ---
  duration_ms: 60.81
  ...
not ok 862 test-tls-ticket-cluster.js
# TIMEOUT
# [master] got "listening"
# [master] connecting
# [master] got "listening"
# [master] got "listening"
# [master] got "listening"
# [worker] exit
# [worker] exit
# [worker] exit
# [worker] exit
  ---
  duration_ms: 60.83
  ...

@jasnell
Copy link
Member

jasnell commented Feb 19, 2016

Given the changes here I'm going to mark this as a don't land on v4 for the time being. We may want to revisit this later /cc @thealphanerd

@jasnell
Copy link
Member

jasnell commented Apr 2, 2016

What's the status on this one?

@jasnell jasnell added the stalled Issues and PRs that are stalled. label Apr 2, 2016
@MylesBorins
Copy link
Contributor

I'm going to close this for now as there has been no response in a month. Please feel free to reopen if you would like to pick this back up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib / src Issues and PRs related to general changes in the lib or src directory. stalled Issues and PRs that are stalled.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants