mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 16:42:21 -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:
parent
1e725e6fd5
commit
de82fa2f83
3 changed files with 18 additions and 1 deletions
|
@ -187,7 +187,16 @@
|
||||||
const cb = macrotaskCallbacks[i];
|
const cb = macrotaskCallbacks[i];
|
||||||
while (true) {
|
while (true) {
|
||||||
const res = cb();
|
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();
|
ops.op_run_microtasks();
|
||||||
|
// If callback returned `true` then it has no more work to do, stop
|
||||||
|
// calling it then.
|
||||||
if (res === true) {
|
if (res === true) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,8 +54,10 @@ const timerTasks = [];
|
||||||
let timerNestingLevel = 0;
|
let timerNestingLevel = 0;
|
||||||
|
|
||||||
function handleTimerMacrotask() {
|
function handleTimerMacrotask() {
|
||||||
|
// We have no work to do, tell the runtime that we don't
|
||||||
|
// need to perform microtask checkpoint.
|
||||||
if (timerTasks.length === 0) {
|
if (timerTasks.length === 0) {
|
||||||
return true;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const task = ArrayPrototypeShift(timerTasks);
|
const task = ArrayPrototypeShift(timerTasks);
|
||||||
|
|
|
@ -351,6 +351,12 @@ function promiseRejectCallback(type, promise, reason) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function promiseRejectMacrotaskCallback() {
|
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) {
|
while (pendingRejections.length > 0) {
|
||||||
const promise = ArrayPrototypeShift(pendingRejections);
|
const promise = ArrayPrototypeShift(pendingRejections);
|
||||||
const hasPendingException = ops.op_has_pending_promise_rejection(
|
const hasPendingException = ops.op_has_pending_promise_rejection(
|
||||||
|
|
Loading…
Reference in a new issue