Skip to content

Commit 0c1e7b5

Browse files
trevnorrisisaacs
authored andcommitted
process: separate nextTick domain logic
It's cleaner to only load domain ticker logic when the domains are being used. This makes execution slightly quicker in both cases, and simpler from the spinner since there is no need to check if the latest callback requires use of domains.
1 parent 875e4a0 commit 0c1e7b5

3 files changed

Lines changed: 25 additions & 14 deletions

File tree

lib/domain.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ var endMethods = ['end', 'abort', 'destroy', 'destroySoon'];
3232
// a few side effects.
3333
events.usingDomains = true;
3434

35+
// replace tickers with domain specific implementation
36+
process.nextTick = process._nextDomainTick;
37+
process._tickCallback = process._tickDomainCallback;
38+
3539
exports.Domain = Domain;
3640

3741
exports.create = exports.createDomain = function(cb) {

src/node.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@
305305
startup.processNextTick = function() {
306306
var _needTickCallback = process._needTickCallback;
307307
var nextTickQueue = [];
308-
var usingDomains = false;
309308
var needSpinner = true;
310309
var inTick = false;
311310

@@ -324,6 +323,7 @@
324323
// needs to be accessible from cc land
325324
process._tickDomainCallback = _tickDomainCallback;
326325
process.nextTick = nextTick;
326+
process._nextDomainTick = _nextDomainTick;
327327

328328
// the maximum number of times it'll process something like
329329
// nextTick(function f(){nextTick(f)})
@@ -372,10 +372,7 @@
372372
// no callbacks to run
373373
if (infoBox[length] === 0)
374374
return infoBox[index] = infoBox[depth] = 0;
375-
if (nextTickQueue[infoBox[length] - 1].domain)
376-
_tickDomainCallback();
377-
else
378-
_tickCallback();
375+
process._tickCallback();
379376
}
380377

381378
// run callbacks that have no domain
@@ -467,15 +464,25 @@
467464
if (infoBox[depth] >= process.maxTickDepth)
468465
maxTickWarn();
469466

470-
var obj = { callback: callback };
471-
if (process.domain !== null) {
472-
obj.domain = process.domain;
473-
// user has opt'd to use domains, so override default functionality
474-
if (!usingDomains) {
475-
process._tickCallback = _tickDomainCallback;
476-
usingDomains = true;
477-
}
467+
var obj = { callback: callback, domain: null };
468+
469+
nextTickQueue.push(obj);
470+
infoBox[length]++;
471+
472+
if (needSpinner) {
473+
_needTickCallback();
474+
needSpinner = false;
478475
}
476+
}
477+
478+
function _nextDomainTick(callback) {
479+
// on the way out, don't bother. it won't get fired anyway.
480+
if (process._exiting)
481+
return;
482+
if (infoBox[depth] >= process.maxTickDepth)
483+
maxTickWarn();
484+
485+
var obj = { callback: callback, domain: process.domain };
479486

480487
nextTickQueue.push(obj);
481488
infoBox[length]++;

test/message/max_tick_depth_trace.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ Trace: (node) warning: Recursive process.nextTick detected. This will break in t
3030
at maxTickWarn (node.js:*:*)
3131
at process.nextTick (node.js:*:*)
3232
at f (*test*message*max_tick_depth_trace.js:*:*)
33-
at _tickCallback (node.js:*:*)
33+
at process._tickCallback (node.js:*:*)
3434
at process._tickFromSpinner (node.js:*:*)
3535
tick 0

0 commit comments

Comments
 (0)