Skip to content

Commit

Permalink
process: improve nextTick performance
Browse files Browse the repository at this point in the history
PR-URL: #25461
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
mscdex authored and BridgeAR committed Sep 4, 2019
1 parent 8c6896e commit f4f8827
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion benchmark/process/next-tick-breadth-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const common = require('../common.js');
const bench = common.createBenchmark(main, {
n: [4e6]
n: [1e7]
});

function main({ n }) {
Expand Down
2 changes: 1 addition & 1 deletion benchmark/process/next-tick-breadth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const common = require('../common.js');
const bench = common.createBenchmark(main, {
n: [4e6]
n: [1e7]
});

function main({ n }) {
Expand Down
2 changes: 1 addition & 1 deletion benchmark/process/next-tick-depth-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const common = require('../common.js');
const bench = common.createBenchmark(main, {
n: [12e6]
n: [7e6]
});

function main({ n }) {
Expand Down
2 changes: 1 addition & 1 deletion benchmark/process/next-tick-depth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
n: [12e6]
n: [7e6]
});

function main({ n }) {
Expand Down
2 changes: 1 addition & 1 deletion benchmark/process/next-tick-exec-args.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
n: [5e6]
n: [4e6]
});

function main({ n }) {
Expand Down
2 changes: 1 addition & 1 deletion benchmark/process/next-tick-exec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
n: [5e6]
n: [4e6]
});

function main({ n }) {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/fixed_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ module.exports = class FixedQueue {
}

shift() {
const { tail } = this;
const tail = this.tail;
const next = tail.shift();
if (tail.isEmpty() && tail.next !== null) {
// If there is another queue, it forms the new tail.
Expand Down
42 changes: 22 additions & 20 deletions lib/internal/process/task_queues.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ function processTicksAndRejections() {

try {
const callback = tock.callback;
if (tock.args === undefined)
if (tock.args === undefined) {
callback();
else
callback(...tock.args);
} else {
const args = tock.args;
switch (args.length) {
case 1: callback(args[0]); break;
case 2: callback(args[0], args[1]); break;
case 3: callback(args[0], args[1], args[2]); break;
case 4: callback(args[0], args[1], args[2], args[3]); break;
default: callback(...args);
}
}
} finally {
if (destroyHooksExist())
emitDestroy(asyncId);
Expand All @@ -88,22 +96,6 @@ function processTicksAndRejections() {
setHasRejectionToWarn(false);
}

class TickObject {
constructor(callback, args) {
this.callback = callback;
this.args = args;

const asyncId = newAsyncId();
const triggerAsyncId = getDefaultTriggerAsyncId();
this[async_id_symbol] = asyncId;
this[trigger_async_id_symbol] = triggerAsyncId;

if (initHooksExist()) {
emitInit(asyncId, 'TickObject', triggerAsyncId, this);
}
}
}

// `nextTick()` will not enqueue any callback when the process is about to
// exit since the callback would not have a chance to be executed.
function nextTick(callback) {
Expand All @@ -127,7 +119,17 @@ function nextTick(callback) {

if (queue.isEmpty())
setHasTickScheduled(true);
queue.push(new TickObject(callback, args));
const asyncId = newAsyncId();
const triggerAsyncId = getDefaultTriggerAsyncId();
const tickObject = {
[async_id_symbol]: asyncId,
[trigger_async_id_symbol]: triggerAsyncId,
callback,
args
};
if (initHooksExist())
emitInit(asyncId, 'TickObject', triggerAsyncId, tickObject);
queue.push(tickObject);
}

let AsyncResource;
Expand Down

0 comments on commit f4f8827

Please sign in to comment.