Skip to content

Commit 9abc278

Browse files
committed
Remove wasteful checks from shouldYield
`shouldYield` will currently return `true` if there's a higher priority task in the Scheduler queue. Since we yield every 5ms anyway, this doesn't really have any practical benefit. On the contrary, the extra checks on every `shouldYield` call are wasteful.
1 parent fe6d052 commit 9abc278

File tree

3 files changed

+10
-30
lines changed

3 files changed

+10
-30
lines changed

packages/react/src/__tests__/ReactProfiler-test.internal.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -2481,15 +2481,11 @@ describe('Profiler', () => {
24812481
// Errors that happen inside of a subscriber should throw,
24822482
throwInOnWorkStarted = true;
24832483
expect(Scheduler).toFlushAndThrow('Expected error onWorkStarted');
2484-
// Rendering was interrupted by the error that was thrown
2485-
expect(Scheduler).toHaveYielded([]);
2486-
// Rendering continues in the next task
2487-
expect(Scheduler).toFlushAndYield(['Component:text']);
24882484
throwInOnWorkStarted = false;
2485+
// Rendering was interrupted by the error that was thrown, then
2486+
// continued and finished in the next task.
2487+
expect(Scheduler).toHaveYielded(['Component:text']);
24892488
expect(onWorkStarted).toHaveBeenCalled();
2490-
2491-
// But the React work should have still been processed.
2492-
expect(Scheduler).toFlushAndYield([]);
24932489
const tree = renderer.toTree();
24942490
expect(tree.type).toBe(Component);
24952491
expect(tree.props.children).toBe('text');

packages/scheduler/src/Scheduler.js

+1-16
Original file line numberDiff line numberDiff line change
@@ -393,21 +393,6 @@ function unstable_getCurrentPriorityLevel() {
393393
return currentPriorityLevel;
394394
}
395395

396-
function unstable_shouldYield() {
397-
const currentTime = getCurrentTime();
398-
advanceTimers(currentTime);
399-
const firstTask = peek(taskQueue);
400-
return (
401-
(firstTask !== currentTask &&
402-
currentTask !== null &&
403-
firstTask !== null &&
404-
firstTask.callback !== null &&
405-
firstTask.startTime <= currentTime &&
406-
firstTask.expirationTime < currentTask.expirationTime) ||
407-
shouldYieldToHost()
408-
);
409-
}
410-
411396
const unstable_requestPaint = requestPaint;
412397

413398
export {
@@ -422,7 +407,7 @@ export {
422407
unstable_cancelCallback,
423408
unstable_wrapCallback,
424409
unstable_getCurrentPriorityLevel,
425-
unstable_shouldYield,
410+
shouldYieldToHost as unstable_shouldYield,
426411
unstable_requestPaint,
427412
unstable_continueExecution,
428413
unstable_pauseExecution,

packages/scheduler/src/__tests__/Scheduler-test.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ describe('Scheduler', () => {
249249
});
250250

251251
it(
252-
'continuations are interrupted by higher priority work scheduled ' +
252+
'continuations do not block higher priority work scheduled ' +
253253
'inside an executing callback',
254254
() => {
255255
const tasks = [
@@ -272,8 +272,8 @@ describe('Scheduler', () => {
272272
Scheduler.unstable_yieldValue('High pri');
273273
});
274274
}
275-
if (tasks.length > 0 && shouldYield()) {
276-
Scheduler.unstable_yieldValue('Yield!');
275+
if (tasks.length > 0) {
276+
// Return a continuation
277277
return work;
278278
}
279279
}
@@ -283,9 +283,8 @@ describe('Scheduler', () => {
283283
'A',
284284
'B',
285285
'Schedule high pri',
286-
// Even though there's time left in the frame, the low pri callback
287-
// should yield to the high pri callback
288-
'Yield!',
286+
// The high pri callback should fire before the continuation of the
287+
// lower pri work
289288
'High pri',
290289
// Continue low pri work
291290
'C',
@@ -662,7 +661,7 @@ describe('Scheduler', () => {
662661
const [label, ms] = task;
663662
Scheduler.unstable_advanceTime(ms);
664663
Scheduler.unstable_yieldValue(label);
665-
if (tasks.length > 0 && shouldYield()) {
664+
if (tasks.length > 0) {
666665
return work;
667666
}
668667
}

0 commit comments

Comments
 (0)