Skip to content
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

lib: make queueMicrotask faster #27032

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions benchmark/process/queue-microtask-breadth.js
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions benchmark/process/queue-microtask-depth.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
25 changes: 14 additions & 11 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
emitBefore,
emitAfter,
emitDestroy,
initHooksExist,
} = internal_async_hooks;

// Get symbols
Expand Down Expand Up @@ -144,29 +145,31 @@ class AsyncResource {
throw new ERR_INVALID_ASYNC_ID('triggerAsyncId', triggerAsyncId);
}

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

// This prop name (destroyed) has to be synchronized with C++
this[destroyedSymbol] = { destroyed: false };
const destroyed = { destroyed: false };
this[destroyedSymbol] = destroyed;

emitInit(
this[async_id_symbol], type, this[trigger_async_id_symbol], this
);
if (initHooksExist()) {
emitInit(asyncId, type, triggerAsyncId, this);
}

if (!opts.requireManualDestroy) {
registerDestroyHook(this, this[async_id_symbol], this[destroyedSymbol]);
registerDestroyHook(this, asyncId, destroyed);
}
}

runInAsyncScope(fn, thisArg, ...args) {
emitBefore(this[async_id_symbol], this[trigger_async_id_symbol]);
let ret;
const asyncId = this[async_id_symbol];
emitBefore(asyncId, this[trigger_async_id_symbol]);
try {
ret = Reflect.apply(fn, thisArg, args);
return Reflect.apply(fn, thisArg, args);
} finally {
emitAfter(this[async_id_symbol]);
emitAfter(asyncId);
}
return ret;
}

emitDestroy() {
Expand Down
5 changes: 1 addition & 4 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,11 @@ function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;

let ret;
try {
ret = Reflect.apply(block, null, args);
return Reflect.apply(block, null, args);
} finally {
async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId;
}

return ret;
}


Expand Down
36 changes: 21 additions & 15 deletions lib/internal/process/task_queues.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const {
} = require('internal/errors').codes;
const FixedQueue = require('internal/fixed_queue');

const FunctionBind = Function.call.bind(Function.prototype.bind);

// *Must* match Environment::TickInfo::Fields in src/env.h.
const kHasTickScheduled = 0;

Expand Down Expand Up @@ -147,28 +149,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) {
if (typeof callback !== 'function') {
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
}

const asyncResource = createMicrotaskResource();
asyncResource.callback = callback;

enqueueMicrotask(() => {
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();
}
});
});
enqueueMicrotask(FunctionBind(runMicrotask, asyncResource));
}

module.exports = {
Expand Down