process: improve nextTick performance
PR-URL: https://github.com/nodejs/node/pull/25461 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
01504904f3
commit
34961c7b5f
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const common = require('../common.js');
|
const common = require('../common.js');
|
||||||
const bench = common.createBenchmark(main, {
|
const bench = common.createBenchmark(main, {
|
||||||
n: [4e6]
|
n: [1e7]
|
||||||
});
|
});
|
||||||
|
|
||||||
function main({ n }) {
|
function main({ n }) {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const common = require('../common.js');
|
const common = require('../common.js');
|
||||||
const bench = common.createBenchmark(main, {
|
const bench = common.createBenchmark(main, {
|
||||||
n: [4e6]
|
n: [1e7]
|
||||||
});
|
});
|
||||||
|
|
||||||
function main({ n }) {
|
function main({ n }) {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const common = require('../common.js');
|
const common = require('../common.js');
|
||||||
const bench = common.createBenchmark(main, {
|
const bench = common.createBenchmark(main, {
|
||||||
n: [12e6]
|
n: [7e6]
|
||||||
});
|
});
|
||||||
|
|
||||||
function main({ n }) {
|
function main({ n }) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const common = require('../common.js');
|
const common = require('../common.js');
|
||||||
const bench = common.createBenchmark(main, {
|
const bench = common.createBenchmark(main, {
|
||||||
n: [12e6]
|
n: [7e6]
|
||||||
});
|
});
|
||||||
|
|
||||||
function main({ n }) {
|
function main({ n }) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const common = require('../common.js');
|
const common = require('../common.js');
|
||||||
const bench = common.createBenchmark(main, {
|
const bench = common.createBenchmark(main, {
|
||||||
n: [5e6]
|
n: [4e6]
|
||||||
});
|
});
|
||||||
|
|
||||||
function main({ n }) {
|
function main({ n }) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const common = require('../common.js');
|
const common = require('../common.js');
|
||||||
const bench = common.createBenchmark(main, {
|
const bench = common.createBenchmark(main, {
|
||||||
n: [5e6]
|
n: [4e6]
|
||||||
});
|
});
|
||||||
|
|
||||||
function main({ n }) {
|
function main({ n }) {
|
||||||
|
@ -102,7 +102,7 @@ module.exports = class FixedQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
shift() {
|
shift() {
|
||||||
const { tail } = this;
|
const tail = this.tail;
|
||||||
const next = tail.shift();
|
const next = tail.shift();
|
||||||
if (tail.isEmpty() && tail.next !== null) {
|
if (tail.isEmpty() && tail.next !== null) {
|
||||||
// If there is another queue, it forms the new tail.
|
// If there is another queue, it forms the new tail.
|
||||||
|
@ -71,10 +71,18 @@ function processTicksAndRejections() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const callback = tock.callback;
|
const callback = tock.callback;
|
||||||
if (tock.args === undefined)
|
if (tock.args === undefined) {
|
||||||
callback();
|
callback();
|
||||||
else
|
} else {
|
||||||
callback(...tock.args);
|
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 {
|
} finally {
|
||||||
if (destroyHooksExist())
|
if (destroyHooksExist())
|
||||||
emitDestroy(asyncId);
|
emitDestroy(asyncId);
|
||||||
@ -88,22 +96,6 @@ function processTicksAndRejections() {
|
|||||||
setHasRejectionToWarn(false);
|
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
|
// `nextTick()` will not enqueue any callback when the process is about to
|
||||||
// exit since the callback would not have a chance to be executed.
|
// exit since the callback would not have a chance to be executed.
|
||||||
function nextTick(callback) {
|
function nextTick(callback) {
|
||||||
@ -127,7 +119,17 @@ function nextTick(callback) {
|
|||||||
|
|
||||||
if (queue.isEmpty())
|
if (queue.isEmpty())
|
||||||
setHasTickScheduled(true);
|
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;
|
let AsyncResource;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user