Stop tracking roots with pending discrete updates (#20978)
Now that discrete updates are flushed synchronously in a microtask, there's no need to track them in their on queue. They're already in the queue we use for all sync work. So we can call that directly.
Andrew Clark committed
Mar 11, 2021 at 01:29 UTC
b7e6310669ed7948d3d8729cc6c0d7904945f05b
4 files changed
+34
-190
packages/react-reconciler/src/ReactFiberLane.new.js
-10
@@ -737,16 +737,6 @@ export function markRootExpired(root: FiberRoot, expiredLanes: Lanes) {
737
root.expiredLanes |= expiredLanes & root.pendingLanes;
738
}
739
740
-export function markDiscreteUpdatesExpired(root: FiberRoot) {
741
- if (root.pendingLanes & InputDiscreteLane) {
742
- root.expiredLanes |= InputDiscreteLane;
743
- }
744
-}
745
-
746
-export function hasDiscreteLanes(lanes: Lanes) {
747
- return (lanes & InputDiscreteLane) !== NoLanes;
748
-}
749
-
740
export function markRootMutableRead(root: FiberRoot, updateLane: Lane) {
741
root.mutableReadLanes |= updateLane & root.pendingLanes;
742
}
packages/react-reconciler/src/ReactFiberLane.old.js
-10
@@ -737,16 +737,6 @@ export function markRootExpired(root: FiberRoot, expiredLanes: Lanes) {
737
root.expiredLanes |= expiredLanes & root.pendingLanes;
738
}
739
740
-export function markDiscreteUpdatesExpired(root: FiberRoot) {
741
- if (root.pendingLanes & InputDiscreteLane) {
742
- root.expiredLanes |= InputDiscreteLane;
743
- }
744
-}
745
-
746
-export function hasDiscreteLanes(lanes: Lanes) {
747
- return (lanes & InputDiscreteLane) !== NoLanes;
748
-}
749
-
740
export function markRootMutableRead(root: FiberRoot, updateLane: Lane) {
741
root.mutableReadLanes |= updateLane & root.pendingLanes;
742
}
packages/react-reconciler/src/ReactFiberWorkLoop.new.js
+17
-85
@@ -39,12 +39,10 @@ import invariant from 'shared/invariant';
39
import {
40
scheduleCallback,
41
cancelCallback,
42
- getCurrentPriorityLevel,
42
shouldYield,
43
requestPaint,
44
now,
45
ImmediatePriority as ImmediateSchedulerPriority,
47
- UserBlockingPriority as UserBlockingSchedulerPriority,
46
NormalPriority as NormalSchedulerPriority,
47
flushSyncCallbackQueue,
48
scheduleSyncCallback,
@@ -148,7 +146,6 @@ import {
146
mergeLanes,
147
removeLanes,
148
pickArbitraryLane,
151
- hasDiscreteLanes,
149
includesNonIdleWork,
150
includesOnlyRetries,
151
includesOnlyTransitions,
@@ -163,10 +160,8 @@ import {
160
markRootSuspended as markRootSuspended_dontCallThisOneDirectly,
161
markRootPinged,
162
markRootExpired,
166
- markDiscreteUpdatesExpired,
163
markRootFinished,
164
lanePriorityToSchedulerPriority,
169
- higherLanePriority,
165
} from './ReactFiberLane.new';
166
import {requestCurrentTransition, NoTransition} from './ReactFiberTransition';
167
import {beginWork as originalBeginWork} from './ReactFiberBeginWork.new';
@@ -243,14 +238,13 @@ const {
238
239
type ExecutionContext = number;
240
246
-export const NoContext = /* */ 0b0000000;
247
-const BatchedContext = /* */ 0b0000001;
248
-const EventContext = /* */ 0b0000010;
249
-const DiscreteEventContext = /* */ 0b0000100;
250
-const LegacyUnbatchedContext = /* */ 0b0001000;
251
-const RenderContext = /* */ 0b0010000;
252
-const CommitContext = /* */ 0b0100000;
253
-export const RetryAfterError = /* */ 0b1000000;
241
+export const NoContext = /* */ 0b000000;
242
+const BatchedContext = /* */ 0b000001;
243
+const EventContext = /* */ 0b000010;
244
+const LegacyUnbatchedContext = /* */ 0b000100;
245
+const RenderContext = /* */ 0b001000;
246
+const CommitContext = /* */ 0b010000;
247
+export const RetryAfterError = /* */ 0b100000;
248
249
type RootExitStatus = 0 | 1 | 2 | 3 | 4 | 5;
250
const RootIncomplete = 0;
@@ -331,8 +325,6 @@ let pendingPassiveEffectsRenderPriority: LanePriority = NoLanePriority;
325
let pendingPassiveEffectsLanes: Lanes = NoLanes;
326
let pendingPassiveProfilerEffects: Array<Fiber> = [];
327
334
-let rootsWithPendingDiscreteUpdates: Set<FiberRoot> | null = null;
335
-
328
// Use these to prevent an infinite loop of nested updates
329
const NESTED_UPDATE_LIMIT = 50;
330
let nestedUpdateCount: number = 0;
@@ -434,29 +426,17 @@ export function requestUpdateLane(fiber: Fiber): Lane {
426
return currentEventTransitionLane;
427
}
428
437
- // TODO: Remove this dependency on the Scheduler priority.
438
- // To do that, we're replacing it with an update lane priority.
439
- const schedulerPriority = getCurrentPriorityLevel();
440
-
441
- // Find the correct lane based on priorities. Ideally, this would just be
442
- // the update lane priority, but for now we're also checking for discrete
443
- // updates and falling back to the scheduler priority.
444
- let lane;
445
- if (
446
- // TODO: Temporary. We're removing the concept of discrete updates.
447
- (executionContext & DiscreteEventContext) !== NoContext &&
448
- schedulerPriority === UserBlockingSchedulerPriority
449
- ) {
450
- lane = findUpdateLane(InputDiscreteLanePriority);
451
- } else if (getCurrentUpdateLanePriority() !== NoLanePriority) {
452
- const currentLanePriority = getCurrentUpdateLanePriority();
453
- lane = findUpdateLane(currentLanePriority);
454
- } else {
455
- const eventLanePriority = getCurrentEventPriority();
456
- lane = findUpdateLane(eventLanePriority);
429
+ // Updates originating inside certain React methods, like flushSync, have
430
+ // their priority set by tracking it with a context variable.
431
+ const updateLanePriority = getCurrentUpdateLanePriority();
432
+ if (updateLanePriority !== NoLanePriority) {
433
+ return findUpdateLane(updateLanePriority);
434
}
435
459
- return lane;
436
+ // This update originated outside React. Ask the host environement for an
437
+ // appropriate priority, based on the type of event.
438
+ const eventLanePriority = getCurrentEventPriority();
439
+ return findUpdateLane(eventLanePriority);
440
}
441
442
function requestRetryLane(fiber: Fiber) {
@@ -578,23 +558,6 @@ export function scheduleUpdateOnFiber(
558
}
559
}
560
} else {
581
- const updateLanePriority = getCurrentUpdateLanePriority();
582
-
583
- // Schedule a discrete update but only if it's not Sync.
584
- if (
585
- (executionContext & DiscreteEventContext) !== NoContext &&
586
- // Only updates greater than default considered discrete, even inside a discrete event.
587
- higherLanePriority(updateLanePriority, DefaultLanePriority) !==
588
- DefaultLanePriority
589
- ) {
590
- // This is the result of a discrete event. Track the lowest priority
591
- // discrete update per root so we can flush them early, if needed.
592
- if (rootsWithPendingDiscreteUpdates === null) {
593
- rootsWithPendingDiscreteUpdates = new Set([root]);
594
- } else {
595
- rootsWithPendingDiscreteUpdates.add(root);
596
- }
597
- }
561
// Schedule other updates after in case the callback is sync.
562
ensureRootIsScheduled(root, eventTime);
563
schedulePendingInteractions(root, lane);
@@ -1096,7 +1059,7 @@ export function flushDiscreteUpdates() {
1059
// like `el.focus()`. Exit.
1060
return;
1061
}
1099
- flushPendingDiscreteUpdates();
1062
+ flushSyncCallbackQueue();
1063
// If the discrete updates scheduled passive effects, flush them now so that
1064
// they fire before the next serial event.
1065
flushPassiveEffects();
@@ -1112,21 +1075,6 @@ export function deferredUpdates<A>(fn: () => A): A {
1075
}
1076
}
1077
1115
-function flushPendingDiscreteUpdates() {
1116
- if (rootsWithPendingDiscreteUpdates !== null) {
1117
- // For each root with pending discrete updates, schedule a callback to
1118
- // immediately flush them.
1119
- const roots = rootsWithPendingDiscreteUpdates;
1120
- rootsWithPendingDiscreteUpdates = null;
1121
- roots.forEach(root => {
1122
- markDiscreteUpdatesExpired(root);
1123
- ensureRootIsScheduled(root, now());
1124
- });
1125
- }
1126
- // Now flush the immediate queue.
1127
- flushSyncCallbackQueue();
1128
-}
1129
-
1078
export function batchedUpdates<A, R>(fn: A => R, a: A): R {
1079
const prevExecutionContext = executionContext;
1080
executionContext |= BatchedContext;
@@ -1164,16 +1112,12 @@ export function discreteUpdates<A, B, C, D, R>(
1112
c: C,
1113
d: D,
1114
): R {
1167
- const prevExecutionContext = executionContext;
1168
- executionContext |= DiscreteEventContext;
1169
-
1115
const previousLanePriority = getCurrentUpdateLanePriority();
1116
try {
1117
setCurrentUpdateLanePriority(InputDiscreteLanePriority);
1118
return fn(a, b, c, d);
1119
} finally {
1120
setCurrentUpdateLanePriority(previousLanePriority);
1176
- executionContext = prevExecutionContext;
1121
if (executionContext === NoContext) {
1122
// Flush the immediate callbacks that were scheduled during this batch
1123
resetRenderTimer();
@@ -1802,18 +1746,6 @@ function commitRootImpl(root, renderPriorityLevel) {
1746
let remainingLanes = mergeLanes(finishedWork.lanes, finishedWork.childLanes);
1747
markRootFinished(root, remainingLanes);
1748
1805
- // Clear already finished discrete updates in case that a later call of
1806
- // `flushDiscreteUpdates` starts a useless render pass which may cancels
1807
- // a scheduled timeout.
1808
- if (rootsWithPendingDiscreteUpdates !== null) {
1809
- if (
1810
- !hasDiscreteLanes(remainingLanes) &&
1811
- rootsWithPendingDiscreteUpdates.has(root)
1812
- ) {
1813
- rootsWithPendingDiscreteUpdates.delete(root);
1814
- }
1815
- }
1816
-
1749
if (root === workInProgressRoot) {
1750
// We can reset these now that they are finished.
1751
workInProgressRoot = null;
packages/react-reconciler/src/ReactFiberWorkLoop.old.js
+17
-85
@@ -39,12 +39,10 @@ import invariant from 'shared/invariant';
39
import {
40
scheduleCallback,
41
cancelCallback,
42
- getCurrentPriorityLevel,
42
shouldYield,
43
requestPaint,
44
now,
45
ImmediatePriority as ImmediateSchedulerPriority,
47
- UserBlockingPriority as UserBlockingSchedulerPriority,
46
NormalPriority as NormalSchedulerPriority,
47
flushSyncCallbackQueue,
48
scheduleSyncCallback,
@@ -148,7 +146,6 @@ import {
146
mergeLanes,
147
removeLanes,
148
pickArbitraryLane,
151
- hasDiscreteLanes,
149
includesNonIdleWork,
150
includesOnlyRetries,
151
includesOnlyTransitions,
@@ -163,10 +160,8 @@ import {
160
markRootSuspended as markRootSuspended_dontCallThisOneDirectly,
161
markRootPinged,
162
markRootExpired,
166
- markDiscreteUpdatesExpired,
163
markRootFinished,
164
lanePriorityToSchedulerPriority,
169
- higherLanePriority,
165
} from './ReactFiberLane.old';
166
import {requestCurrentTransition, NoTransition} from './ReactFiberTransition';
167
import {beginWork as originalBeginWork} from './ReactFiberBeginWork.old';
@@ -243,14 +238,13 @@ const {
238
239
type ExecutionContext = number;
240
246
-export const NoContext = /* */ 0b0000000;
247
-const BatchedContext = /* */ 0b0000001;
248
-const EventContext = /* */ 0b0000010;
249
-const DiscreteEventContext = /* */ 0b0000100;
250
-const LegacyUnbatchedContext = /* */ 0b0001000;
251
-const RenderContext = /* */ 0b0010000;
252
-const CommitContext = /* */ 0b0100000;
253
-export const RetryAfterError = /* */ 0b1000000;
241
+export const NoContext = /* */ 0b000000;
242
+const BatchedContext = /* */ 0b000001;
243
+const EventContext = /* */ 0b000010;
244
+const LegacyUnbatchedContext = /* */ 0b000100;
245
+const RenderContext = /* */ 0b001000;
246
+const CommitContext = /* */ 0b010000;
247
+export const RetryAfterError = /* */ 0b100000;
248
249
type RootExitStatus = 0 | 1 | 2 | 3 | 4 | 5;
250
const RootIncomplete = 0;
@@ -331,8 +325,6 @@ let pendingPassiveEffectsRenderPriority: LanePriority = NoLanePriority;
325
let pendingPassiveEffectsLanes: Lanes = NoLanes;
326
let pendingPassiveProfilerEffects: Array<Fiber> = [];
327
334
-let rootsWithPendingDiscreteUpdates: Set<FiberRoot> | null = null;
335
-
328
// Use these to prevent an infinite loop of nested updates
329
const NESTED_UPDATE_LIMIT = 50;
330
let nestedUpdateCount: number = 0;
@@ -434,29 +426,17 @@ export function requestUpdateLane(fiber: Fiber): Lane {
426
return currentEventTransitionLane;
427
}
428
437
- // TODO: Remove this dependency on the Scheduler priority.
438
- // To do that, we're replacing it with an update lane priority.
439
- const schedulerPriority = getCurrentPriorityLevel();
440
-
441
- // Find the correct lane based on priorities. Ideally, this would just be
442
- // the update lane priority, but for now we're also checking for discrete
443
- // updates and falling back to the scheduler priority.
444
- let lane;
445
- if (
446
- // TODO: Temporary. We're removing the concept of discrete updates.
447
- (executionContext & DiscreteEventContext) !== NoContext &&
448
- schedulerPriority === UserBlockingSchedulerPriority
449
- ) {
450
- lane = findUpdateLane(InputDiscreteLanePriority);
451
- } else if (getCurrentUpdateLanePriority() !== NoLanePriority) {
452
- const currentLanePriority = getCurrentUpdateLanePriority();
453
- lane = findUpdateLane(currentLanePriority);
454
- } else {
455
- const eventLanePriority = getCurrentEventPriority();
456
- lane = findUpdateLane(eventLanePriority);
429
+ // Updates originating inside certain React methods, like flushSync, have
430
+ // their priority set by tracking it with a context variable.
431
+ const updateLanePriority = getCurrentUpdateLanePriority();
432
+ if (updateLanePriority !== NoLanePriority) {
433
+ return findUpdateLane(updateLanePriority);
434
}
435
459
- return lane;
436
+ // This update originated outside React. Ask the host environement for an
437
+ // appropriate priority, based on the type of event.
438
+ const eventLanePriority = getCurrentEventPriority();
439
+ return findUpdateLane(eventLanePriority);
440
}
441
442
function requestRetryLane(fiber: Fiber) {
@@ -578,23 +558,6 @@ export function scheduleUpdateOnFiber(
558
}
559
}
560
} else {
581
- const updateLanePriority = getCurrentUpdateLanePriority();
582
-
583
- // Schedule a discrete update but only if it's not Sync.
584
- if (
585
- (executionContext & DiscreteEventContext) !== NoContext &&
586
- // Only updates greater than default considered discrete, even inside a discrete event.
587
- higherLanePriority(updateLanePriority, DefaultLanePriority) !==
588
- DefaultLanePriority
589
- ) {
590
- // This is the result of a discrete event. Track the lowest priority
591
- // discrete update per root so we can flush them early, if needed.
592
- if (rootsWithPendingDiscreteUpdates === null) {
593
- rootsWithPendingDiscreteUpdates = new Set([root]);
594
- } else {
595
- rootsWithPendingDiscreteUpdates.add(root);
596
- }
597
- }
561
// Schedule other updates after in case the callback is sync.
562
ensureRootIsScheduled(root, eventTime);
563
schedulePendingInteractions(root, lane);
@@ -1096,7 +1059,7 @@ export function flushDiscreteUpdates() {
1059
// like `el.focus()`. Exit.
1060
return;
1061
}
1099
- flushPendingDiscreteUpdates();
1062
+ flushSyncCallbackQueue();
1063
// If the discrete updates scheduled passive effects, flush them now so that
1064
// they fire before the next serial event.
1065
flushPassiveEffects();
@@ -1112,21 +1075,6 @@ export function deferredUpdates<A>(fn: () => A): A {
1075
}
1076
}
1077
1115
-function flushPendingDiscreteUpdates() {
1116
- if (rootsWithPendingDiscreteUpdates !== null) {
1117
- // For each root with pending discrete updates, schedule a callback to
1118
- // immediately flush them.
1119
- const roots = rootsWithPendingDiscreteUpdates;
1120
- rootsWithPendingDiscreteUpdates = null;
1121
- roots.forEach(root => {
1122
- markDiscreteUpdatesExpired(root);
1123
- ensureRootIsScheduled(root, now());
1124
- });
1125
- }
1126
- // Now flush the immediate queue.
1127
- flushSyncCallbackQueue();
1128
-}
1129
-
1078
export function batchedUpdates<A, R>(fn: A => R, a: A): R {
1079
const prevExecutionContext = executionContext;
1080
executionContext |= BatchedContext;
@@ -1164,16 +1112,12 @@ export function discreteUpdates<A, B, C, D, R>(
1112
c: C,
1113
d: D,
1114
): R {
1167
- const prevExecutionContext = executionContext;
1168
- executionContext |= DiscreteEventContext;
1169
-
1115
const previousLanePriority = getCurrentUpdateLanePriority();
1116
try {
1117
setCurrentUpdateLanePriority(InputDiscreteLanePriority);
1118
return fn(a, b, c, d);
1119
} finally {
1120
setCurrentUpdateLanePriority(previousLanePriority);
1176
- executionContext = prevExecutionContext;
1121
if (executionContext === NoContext) {
1122
// Flush the immediate callbacks that were scheduled during this batch
1123
resetRenderTimer();
@@ -1802,18 +1746,6 @@ function commitRootImpl(root, renderPriorityLevel) {
1746
let remainingLanes = mergeLanes(finishedWork.lanes, finishedWork.childLanes);
1747
markRootFinished(root, remainingLanes);
1748
1805
- // Clear already finished discrete updates in case that a later call of
1806
- // `flushDiscreteUpdates` starts a useless render pass which may cancels
1807
- // a scheduled timeout.
1808
- if (rootsWithPendingDiscreteUpdates !== null) {
1809
- if (
1810
- !hasDiscreteLanes(remainingLanes) &&
1811
- rootsWithPendingDiscreteUpdates.has(root)
1812
- ) {
1813
- rootsWithPendingDiscreteUpdates.delete(root);
1814
- }
1815
- }
1816
-
1749
if (root === workInProgressRoot) {
1750
// We can reset these now that they are finished.
1751
workInProgressRoot = null;