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

[Scheduler] Use rIC to post first callback #16166

Merged
merged 1 commit into from
Jul 22, 2019

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Jul 20, 2019

Scheduler uses requestAnimationFrame to post tasks to the browser. If this happens at the beginning of a frame, the callback might not fire until the subsequent frame, even if the main thread is idle.

Our theory was that this wouldn't be an issue in practice, because once the first rAF fires, we schedule the next rAF as early as possible in that frame. Combined with our heuristic for computing frame deadlines, we shouldn't get any idle time in between frames — only before the first frame.

This reasoning holds true when you have a small number of large tasks, such as the ones posted by React. The idle time before the task starts is negligible relative to the lifetime of the entire task.

However, it does not hold if you have many small tasks posted over a span of time, perhaps spawned by a flurry of IO events. In this case, instead of single, contiguous rAF loop preceded by an idle frame, you get many rAF loops preceded by many idle frames. Our new theory is that this is contributing to poor main thread utilization during page loads.

To try to reclaim as much idle time as possible, this PR adds two experimental flags. The first one adds a requestIdleCallback call to start the rAF loop, which will fire before rAF if there's idle time left in the frame. (We still call rAF in case there isn't. We start working in whichever event fires first.)

The second flag tries a similar strategy using setTimeout(fn, 0). If the timer fires before rAF, we'll assume that the main thread is idle.

If either requestIdleCallback or setTimeout fires before rAF, we'll immediately perform some work. Since we don't have a real frame time that we can use to compute the frame deadline, we might miss the next vsync, but this is fine because we can catch up during the next frame, by which point a real rAF will have fired and the loop can proceed the same way it does today.

Test plan: Try this on Facebook to see if it improves load times.

Scheduler uses `requestAnimationFrame` to post tasks to the browser.
If this happens at the beginning of a frame, the callback might not
fire until the subsequent frame, even if the main thread is idle.

Our theory was that this wouldn't be an issue in practice, because once
the first rAF fires, we schedule the next rAF as early as possible in
that frame. Combined with our heuristic for computing frame deadlines,
we shouldn't get any idle time in between frames — only before the
*first* frame.

This reasoning holds true when you have a small number of large tasks,
such as the ones posted by React. The idle time before the task starts
is negligible relative to the lifetime of the entire task.

However, it does not hold if you have many small tasks posted over a
span of time, perhaps spawned by a flurry of IO events. In this case,
instead of single, contiguous rAF loop preceded by an idle frame, you
get many rAF loops preceded by many idle frames. Our new theory is that
this is contributing to poor main thread utilization during page loads.

To try to reclaim as much idle time as possible, this PR adds two
experimental flags. The first one adds a `requestIdleCallback` call to
start the rAF loop, which will fire before rAF if there's idle time left
in the frame. (We still call rAF in case there isn't. We start working
in whichever event fires first.)

The second flag tries a similar strategy using `setTimeout(fn, 0)`. If
the timer fires before rAF, we'll assume that the main thread is idle.

If either `requestIdleCallback` or `setTimeout` fires before rAF, we'll
immediately peform some work. Since we don't have a real frame time that
we can use to compute the frame deadline, we'll do an entire frame
length of work. This will probably push us past the vsync, but this is
fine because we can catch up during the next frame, by which point a
real rAF will have fired and the loop can proceed the same way it
does today.

Test plan: Try this on Facebook to see if it improves load times
@sizebot
Copy link

sizebot commented Jul 20, 2019

React: size: -0.9%, gzip: 0.0%

Details of bundled changes.

Comparing: 2c4d61e...08633f9

react

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react.development.js +0.9% +0.8% 113.49 KB 114.55 KB 28.57 KB 28.79 KB UMD_DEV
react.production.min.js -0.9% 0.0% 13.25 KB 13.13 KB 5.09 KB 5.09 KB UMD_PROD
react.profiling.min.js -0.8% -0.0% 15.45 KB 15.33 KB 5.63 KB 5.63 KB UMD_PROFILING
react.production.min.js 0.0% 0.0% 6.69 KB 6.69 KB 2.75 KB 2.75 KB NODE_PROD
React-dev.js 0.0% 0.0% 69.18 KB 69.18 KB 17.8 KB 17.8 KB FB_WWW_DEV

scheduler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
SchedulerMock-dev.js +0.8% +0.8% 21.24 KB 21.41 KB 4.62 KB 4.66 KB FB_WWW_DEV
scheduler.development.js +3.9% +3.4% 27.46 KB 28.54 KB 6.9 KB 7.14 KB NODE_DEV
scheduler.production.min.js -2.1% 🔺+0.6% 5.9 KB 5.78 KB 2.18 KB 2.19 KB NODE_PROD
Scheduler-dev.js +4.1% +3.4% 28.03 KB 29.19 KB 7 KB 7.24 KB FB_WWW_DEV
Scheduler-prod.js 🔺+6.0% 🔺+4.9% 16.33 KB 17.32 KB 3.45 KB 3.62 KB FB_WWW_PROD
scheduler-tracing.development.js 0.0% 0.0% 11.15 KB 11.15 KB 2.79 KB 2.79 KB NODE_DEV
scheduler-tracing.production.min.js 0.0% 🔺+0.3% 728 B 728 B 382 B 383 B NODE_PROD
scheduler-unstable_mock.development.js 0.0% 0.0% 20.93 KB 20.93 KB 4.59 KB 4.59 KB UMD_DEV
scheduler-tracing.profiling.min.js 0.0% +0.1% 3.26 KB 3.26 KB 999 B 1000 B NODE_PROFILING
scheduler-unstable_mock.development.js 0.0% 0.0% 20.74 KB 20.74 KB 4.53 KB 4.53 KB NODE_DEV
scheduler-unstable_mock.production.min.js 0.0% 0.0% 5.16 KB 5.16 KB 1.96 KB 1.96 KB NODE_PROD

Generated by 🚫 dangerJS

@acdlite acdlite force-pushed the reclaim-idle-time branch 8 times, most recently from 7584310 to 173b0ac Compare July 22, 2019 00:02
@acdlite acdlite changed the title [WIP][Scheduler] Use rIC to post first callback [Scheduler] Use rIC to post first callback Jul 22, 2019
@acdlite acdlite requested a review from bvaughn July 22, 2019 00:18
@acdlite acdlite marked this pull request as ready for review July 22, 2019 00:18
fpsLocked = true;
} else {
// reset the framerate
activeFrameTime = 33;
frameLength = 33;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you be resetting to 33.33?

scheduledHostCallback = null;
}
} catch (error) {
// If a scheduler task throws, exit the current browser task so the
// error can be observed, and post a new task as soon as possible
// so we can continue where we left off.
isMessageEventScheduled = true;
port.postMessage(undefined);
port.postMessage(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is undefined -> null an important change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really

};

requestHostCallback = function(callback) {
if (scheduledHostCallback === null) {
if (!isRAFLoopRunning) {
isRAFLoopRunning = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    if (!isRAFLoopRunning) {
      isRAFLoopRunning = false;

These lines look fishy. Did you mean to set isRAFLoopRunning = true?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I fixed this locally but didn't commit it

@acdlite acdlite force-pushed the reclaim-idle-time branch from 173b0ac to ac1abe4 Compare July 22, 2019 18:04
@acdlite acdlite requested a review from bvaughn July 22, 2019 18:10
@acdlite acdlite force-pushed the reclaim-idle-time branch from ac1abe4 to 08633f9 Compare July 22, 2019 18:19
}
// Yielding to the browser will give it a chance to paint, so we can
// reset this.
needsPaint = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still valid in the event that performWorkUntilDeadline gets called by timeout or rIC?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in that case but it is relevant in the case where forceFrameRate sets the frame length to an artificially large size. We should track the chosen frame length and the hardware frame length separately. Planning to do that in a follow up.

}
frameDeadline = getCurrentTime() + frameLength;
performWorkUntilDeadline();
}, 0);
}
}
};

cancelHostCallback = function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth checking/clearing the timeout/rIC here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

performWorkUntilDeadline will exit immediately when scheduledHostCallback is null so it's fine. I wanted to keep the callback/timeout IDs local.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I know it's a no-op in that case.

Seems reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants