Skip to content

Commit

Permalink
[Scheduler] Track start of current chunk
Browse files Browse the repository at this point in the history
Currently in `shouldYield`, we compare the current time to a deadline
that is pre-computed at the beginning of the current chunk.

However, since we use different deadlines depending on whether an input
event is pending, it makes more sense to track the start of the current
chunk and check how much time has elapsed since then.

Doesn't change any behavior, just refactors the logic.
  • Loading branch information
acdlite committed Aug 16, 2021
1 parent 152ecce commit 4f7d257
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/scheduler/src/forks/Scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,23 +419,23 @@ let taskTimeoutID = -1;
// It does not attempt to align with frame boundaries, since most tasks don't
// need to be frame aligned; for those that do, use requestAnimationFrame.
let yieldInterval = 5;
let deadline = 0;
let startTime = -1;

// TODO: Make this configurable
// TODO: Adjust this based on priority?
const maxYieldInterval = 300;
let needsPaint = false;

function shouldYieldToHost() {
const timeElapsed = getCurrentTime() - startTime;
if (
enableIsInputPending &&
navigator !== undefined &&
navigator.scheduling !== undefined &&
navigator.scheduling.isInputPending !== undefined
) {
const scheduling = navigator.scheduling;
const currentTime = getCurrentTime();
if (currentTime >= deadline) {
if (timeElapsed >= yieldInterval) {
// There's no time left. We may want to yield control of the main
// thread, so the browser can perform high priority tasks. The main ones
// are painting and user input. If there's a pending paint or a pending
Expand All @@ -450,7 +450,6 @@ function shouldYieldToHost() {
}
// There's no pending input. Only yield if we've reached the max
// yield interval.
const timeElapsed = currentTime - (deadline - yieldInterval);
return timeElapsed >= maxYieldInterval;
} else {
// There's still time left in the frame.
Expand All @@ -459,7 +458,7 @@ function shouldYieldToHost() {
} else {
// `isInputPending` is not available. Since we have no way of knowing if
// there's pending input, always yield at the end of the frame.
return getCurrentTime() >= deadline;
return timeElapsed >= yieldInterval;
}
}

Expand Down Expand Up @@ -499,7 +498,7 @@ const performWorkUntilDeadline = () => {
// Yield after `yieldInterval` ms, regardless of where we are in the vsync
// cycle. This means there's always time remaining at the beginning of
// the message event.
deadline = currentTime + yieldInterval;
startTime = currentTime;
const hasTimeRemaining = true;

// If a scheduler task throws, exit the current browser task so the
Expand Down

0 comments on commit 4f7d257

Please sign in to comment.