lib: make queueMicrotask faster
No longer create an additional scope within queueMicrotask in order to improve performance. PR-URL: https://github.com/nodejs/node/pull/27032 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
d3d4e10107
commit
2c49e8b537
21
benchmark/process/queue-microtask-breadth.js
Normal file
21
benchmark/process/queue-microtask-breadth.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const common = require('../common.js');
|
||||||
|
const bench = common.createBenchmark(main, {
|
||||||
|
n: [4e5]
|
||||||
|
});
|
||||||
|
|
||||||
|
function main({ n }) {
|
||||||
|
var j = 0;
|
||||||
|
|
||||||
|
function cb() {
|
||||||
|
j++;
|
||||||
|
if (j === n)
|
||||||
|
bench.end(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
bench.start();
|
||||||
|
for (var i = 0; i < n; i++) {
|
||||||
|
queueMicrotask(cb);
|
||||||
|
}
|
||||||
|
}
|
17
benchmark/process/queue-microtask-depth.js
Normal file
17
benchmark/process/queue-microtask-depth.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
'use strict';
|
||||||
|
const common = require('../common.js');
|
||||||
|
const bench = common.createBenchmark(main, {
|
||||||
|
n: [12e5]
|
||||||
|
});
|
||||||
|
|
||||||
|
function main({ n }) {
|
||||||
|
let counter = n;
|
||||||
|
bench.start();
|
||||||
|
queueMicrotask(onNextTick);
|
||||||
|
function onNextTick() {
|
||||||
|
if (--counter)
|
||||||
|
queueMicrotask(onNextTick);
|
||||||
|
else
|
||||||
|
bench.end(n);
|
||||||
|
}
|
||||||
|
}
|
@ -37,6 +37,8 @@ const {
|
|||||||
} = require('internal/errors').codes;
|
} = require('internal/errors').codes;
|
||||||
const FixedQueue = require('internal/fixed_queue');
|
const FixedQueue = require('internal/fixed_queue');
|
||||||
|
|
||||||
|
const FunctionBind = Function.call.bind(Function.prototype.bind);
|
||||||
|
|
||||||
// *Must* match Environment::TickInfo::Fields in src/env.h.
|
// *Must* match Environment::TickInfo::Fields in src/env.h.
|
||||||
const kHasTickScheduled = 0;
|
const kHasTickScheduled = 0;
|
||||||
|
|
||||||
@ -149,28 +151,32 @@ function createMicrotaskResource() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function runMicrotask() {
|
||||||
|
this.runInAsyncScope(() => {
|
||||||
|
const callback = this.callback;
|
||||||
|
try {
|
||||||
|
callback();
|
||||||
|
} catch (error) {
|
||||||
|
// TODO(devsnek) remove this if
|
||||||
|
// https://bugs.chromium.org/p/v8/issues/detail?id=8326
|
||||||
|
// is resolved such that V8 triggers the fatal exception
|
||||||
|
// handler for microtasks
|
||||||
|
triggerFatalException(error);
|
||||||
|
} finally {
|
||||||
|
this.emitDestroy();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function queueMicrotask(callback) {
|
function queueMicrotask(callback) {
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
|
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
const asyncResource = createMicrotaskResource();
|
const asyncResource = createMicrotaskResource();
|
||||||
|
asyncResource.callback = callback;
|
||||||
|
|
||||||
enqueueMicrotask(() => {
|
enqueueMicrotask(FunctionBind(runMicrotask, asyncResource));
|
||||||
asyncResource.runInAsyncScope(() => {
|
|
||||||
try {
|
|
||||||
callback();
|
|
||||||
} catch (error) {
|
|
||||||
// TODO(devsnek) remove this if
|
|
||||||
// https://bugs.chromium.org/p/v8/issues/detail?id=8326
|
|
||||||
// is resolved such that V8 triggers the fatal exception
|
|
||||||
// handler for microtasks
|
|
||||||
triggerFatalException(error);
|
|
||||||
} finally {
|
|
||||||
asyncResource.emitDestroy();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user