1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 08:33:43 -05:00

perf: don't run microtask checkpoint if macrotask callback did no work (#19492)

Most of the time there's no firing timers, nor pending promise
rejections, so it's wasteful to run microtask checkpoint 
additionally twice on each tick of the event loop.

Closes https://github.com/denoland/deno/issues/18871
Ref https://github.com/denoland/deno/issues/19451
This commit is contained in:
Bartek Iwańczuk 2023-06-14 16:21:06 +02:00 committed by GitHub
parent 691ef2cc6a
commit 5ef225853c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 1 deletions

View file

@ -187,7 +187,16 @@
const cb = macrotaskCallbacks[i];
while (true) {
const res = cb();
// If callback returned `undefined` then it has no work to do, we don't
// need to perform microtask checkpoint.
if (res === undefined) {
break;
}
ops.op_run_microtasks();
// If callback returned `true` then it has no more work to do, stop
// calling it then.
if (res === true) {
break;
}

View file

@ -54,8 +54,10 @@ const timerTasks = [];
let timerNestingLevel = 0;
function handleTimerMacrotask() {
// We have no work to do, tell the runtime that we don't
// need to perform microtask checkpoint.
if (timerTasks.length === 0) {
return true;
return undefined;
}
const task = ArrayPrototypeShift(timerTasks);

View file

@ -351,6 +351,12 @@ function promiseRejectCallback(type, promise, reason) {
}
function promiseRejectMacrotaskCallback() {
// We have no work to do, tell the runtime that we don't
// need to perform microtask checkpoint.
if (pendingRejections.length === 0) {
return undefined;
}
while (pendingRejections.length > 0) {
const promise = ArrayPrototypeShift(pendingRejections);
const hasPendingException = ops.op_has_pending_promise_rejection(