Skip to content

fix(scheduler): adjust yield timing logic to improve task scheduling … #2483

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 9 additions & 8 deletions packages/solid/src/reactive/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ let taskIdCounter = 1,
yieldInterval = 5,
deadline = 0,
maxYieldInterval = 300,
maxDeadline = 0,
scheduleCallback: (() => void) | null = null,
scheduledCallback: ((hasTimeRemaining: boolean, initialTime: number) => boolean) | null = null;
scheduledCallback: ((initialTime: number) => boolean) | null = null;

const maxSigned31BitInt = 1073741823;
/* istanbul ignore next */
Expand All @@ -33,9 +34,9 @@ function setupScheduler() {
if (scheduledCallback !== null) {
const currentTime = performance.now();
deadline = currentTime + yieldInterval;
const hasTimeRemaining = true;
maxDeadline = currentTime + maxYieldInterval;
try {
const hasMoreWork = scheduledCallback(hasTimeRemaining, currentTime);
const hasMoreWork = scheduledCallback(currentTime);
if (!hasMoreWork) {
scheduledCallback = null;
} else port.postMessage(null);
Expand Down Expand Up @@ -70,7 +71,7 @@ function setupScheduler() {
}
// There's no pending input. Only yield if we've reached the max
// yield interval.
return currentTime >= maxYieldInterval;
return currentTime >= maxDeadline;
} else {
// There's still time left in the frame.
return false;
Expand Down Expand Up @@ -128,23 +129,23 @@ export function cancelCallback(task: Task) {
task.fn = null;
}

function flushWork(hasTimeRemaining: boolean, initialTime: number) {
function flushWork(initialTime: number) {
// We'll need a host callback the next time work is scheduled.
isCallbackScheduled = false;
isPerformingWork = true;
try {
return workLoop(hasTimeRemaining, initialTime);
return workLoop(initialTime);
} finally {
currentTask = null;
isPerformingWork = false;
}
}

function workLoop(hasTimeRemaining: boolean, initialTime: number) {
function workLoop(initialTime: number) {
let currentTime = initialTime;
currentTask = taskQueue[0] || null;
while (currentTask !== null) {
if (currentTask.expirationTime > currentTime && (!hasTimeRemaining || shouldYieldToHost!())) {
if (currentTask.expirationTime > currentTime && shouldYieldToHost!()) {
// This currentTask hasn't expired, and we've reached the deadline.
break;
}
Expand Down