-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
lib: simplify nextTick() usage #1612
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -448,16 +448,16 @@ CryptoStream.prototype.destroy = function(err) { | |
| } | ||
| this._opposite.destroy(); | ||
|
|
||
| process.nextTick(destroyNT, this, err); | ||
| process.nextTick(destroyNT, this, err ? true : false); | ||
| }; | ||
|
|
||
|
|
||
| function destroyNT(self, err) { | ||
| function destroyNT(self, hadErr) { | ||
| // Force EOF | ||
| self.push(null); | ||
|
|
||
| // Emit 'close' event | ||
| self.emit('close', err ? true : false); | ||
| self.emit('close', hadErr); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious, why change this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improved readability?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another reason: keeping an exception object alive for some time means keeping a complex object alive that in turn contains references to other objects. A boolean is much (much!) easier for the GC to scan. |
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -444,9 +444,7 @@ TLSSocket.prototype.renegotiate = function(options, callback) { | |
| } | ||
| if (!this._handle.renegotiate()) { | ||
| if (callback) { | ||
| process.nextTick(function() { | ||
| callback(new Error('Failed to renegotiate')); | ||
| }); | ||
| process.nextTick(callback, new Error('Failed to renegotiate')); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of note, this will change the stacktrace for the Error. Not sure if that's a huge problem, since the new stack will be slightly more descriptive.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH the old error wasn't much better than just throwing a string. At least here you get a pointer to where the error originated. |
||
| } | ||
| return false; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -321,19 +321,14 @@ Socket.prototype.send = function(buffer, | |
| !!callback); | ||
| if (err && callback) { | ||
| // don't emit as error, dgram_legacy.js compatibility | ||
| process.nextTick(sendEmitErrorNT, err, address, port, callback); | ||
| var ex = exceptionWithHostPort(err, 'send', address, port); | ||
| process.nextTick(callback, ex); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
|
|
||
| function sendEmitErrorNT(err, address, port, callback) { | ||
| var ex = exceptionWithHostPort(err, 'send', address, port); | ||
| callback(ex); | ||
| } | ||
|
|
||
|
|
||
| function afterSend(err) { | ||
| if (err) { | ||
| err = exceptionWithHostPort(err, 'send', this.address, this.port); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,17 +61,16 @@ function makeAsync(callback) { | |
| // The API already returned, we can invoke the callback immediately. | ||
| callback.apply(null, arguments); | ||
| } else { | ||
| process.nextTick(callMakeAsyncCbNT, callback, arguments); | ||
| var args = new Array(arguments.length + 1); | ||
| args[0] = callback; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems a little bit hacky. var args = [callback];
for (var i = 0; i <arguments.length; i++)
args.push(arguments[i]);
process.nextTick.apply(null, args);
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's faster to specify the length up-front since v8 then knows how much to (pre-)allocate. Here is a jsperf showing the difference for a short array.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! so this change is reasonable for me. |
||
| for (var i = 1, a = 0; a < arguments.length; ++i, ++a) | ||
| args[i] = arguments[a]; | ||
| process.nextTick.apply(null, args); | ||
| } | ||
| }; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DNS callbacks have either two or three arguments so maybe it's cleaner to write this as: return function asyncCallback(arg0, arg1, arg2) {
if (asyncCallback.immediately) {
if (arguments.length === 3)
callback(arg0, arg1, arg2);
else
callback(arg0, arg1);
} else {
if (arguments.length === 3)
process.nextTick(callback, arg0, arg1, arg2);
else
process.nextTick(callback, arg0, arg1);
}
};It's still ugly but it avoids allocating an array and going through .apply().
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I was going to do that initially but I wanted see if I could write |
||
|
|
||
|
|
||
| function callMakeAsyncCbNT(callback, args) { | ||
| callback.apply(null, args); | ||
| } | ||
|
|
||
|
|
||
| function onlookup(err, addresses) { | ||
| if (err) { | ||
| return this.callback(errnoException(err, 'getaddrinfo', this.hostname)); | ||
|
|
||
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'm not sure if it's correct to decrement the counter upfront. Maybe e.g. @chrisdickinson can chime in?
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 wasn't sure either, but all tests pass.
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.
Hm, so while I think this is correct, it feels unsafe to decouple
pendingcbfrom actually calling the callback. We end up relying ononwriteto nextTick the next callback in the queue to ensure the order of events happens as planned.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'm confused how this isn't a change functionality? It went from decrementing only in the
elseto always.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 was also being decremented in the nextTick callback.