Skip to content

Commit

Permalink
Remove callbackId field from FiberRoot (#19458)
Browse files Browse the repository at this point in the history
The old expiration times implementation used this field to infer when
the priority of a task had changed at a more granular level than a
Scheduler priority level.

Now that we have the LanePriority type, which is React-specific, we no
longer need the `callbackId` field.
  • Loading branch information
acdlite authored Jul 27, 2020
1 parent c24b641 commit 96ac799
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 47 deletions.
1 change: 0 additions & 1 deletion packages/react-reconciler/src/ReactFiberRoot.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function FiberRootNode(containerInfo, tag, hydrate) {
this.pendingContext = null;
this.hydrate = hydrate;
this.callbackNode = null;
this.callbackId = NoLanes;
this.callbackPriority = NoLanePriority;
this.eventTimes = createLaneMap(NoLanes);
this.expirationTimes = createLaneMap(NoTimestamp);
Expand Down
1 change: 0 additions & 1 deletion packages/react-reconciler/src/ReactFiberRoot.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function FiberRootNode(containerInfo, tag, hydrate) {
this.pendingContext = null;
this.hydrate = hydrate;
this.callbackNode = null;
this.callbackId = NoLanes;
this.callbackPriority = NoLanePriority;
this.eventTimes = createLaneMap(NoLanes);
this.expirationTimes = createLaneMap(NoTimestamp);
Expand Down
33 changes: 13 additions & 20 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,10 @@ function markUpdateLaneFromFiberToRoot(
}

// Use this function to schedule a task for a root. There's only one task per
// root; if a task was already scheduled, we'll check to make sure the
// expiration time of the existing task is the same as the expiration time of
// the next level that the root has work on. This function is called on every
// update, and right before exiting a task.
// root; if a task was already scheduled, we'll check to make sure the priority
// of the existing task is the same as the priority of the next level that the
// root has work on. This function is called on every update, and right before
// exiting a task.
function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
const existingCallbackNode = root.callbackNode;

Expand All @@ -689,37 +689,32 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
markStarvedLanesAsExpired(root, currentTime);

// Determine the next lanes to work on, and their priority.
const newCallbackId = getNextLanes(
const nextLanes = getNextLanes(
root,
root === workInProgressRoot ? workInProgressRootRenderLanes : NoLanes,
);
// This returns the priority level computed during the `getNextLanes` call.
const newCallbackPriority = returnNextLanesPriority();

if (newCallbackId === NoLanes) {
if (nextLanes === NoLanes) {
// Special case: There's nothing to work on.
if (existingCallbackNode !== null) {
cancelCallback(existingCallbackNode);
root.callbackNode = null;
root.callbackPriority = NoLanePriority;
root.callbackId = NoLanes;
}
return;
}

// Check if there's an existing task. We may be able to reuse it.
const existingCallbackId = root.callbackId;
const existingCallbackPriority = root.callbackPriority;
if (existingCallbackId !== NoLanes) {
if (newCallbackId === existingCallbackId) {
// This task is already scheduled. Let's check its priority.
if (existingCallbackPriority === newCallbackPriority) {
// The priority hasn't changed. Exit.
return;
}
// The task ID is the same but the priority changed. Cancel the existing
// callback. We'll schedule a new one below.
if (existingCallbackNode !== null) {
const existingCallbackPriority = root.callbackPriority;
if (existingCallbackPriority === newCallbackPriority) {
// The priority hasn't changed. We can reuse the existing task. Exit.
return;
}
// The priority changed. Cancel the existing callback. We'll schedule a new
// one below.
cancelCallback(existingCallbackNode);
}

Expand All @@ -741,7 +736,6 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
);
}

root.callbackId = newCallbackId;
root.callbackPriority = newCallbackPriority;
root.callbackNode = newCallbackNode;
}
Expand Down Expand Up @@ -2041,7 +2035,6 @@ function commitRootImpl(root, renderPriorityLevel) {
// commitRoot never returns a continuation; it always finishes synchronously.
// So we can clear these now to allow a new callback to be scheduled.
root.callbackNode = null;
root.callbackId = NoLanes;

// Update the first and last pending times on this root. The new first
// pending time is whatever is left on the root fiber.
Expand Down
33 changes: 13 additions & 20 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,10 @@ function markUpdateLaneFromFiberToRoot(
}

// Use this function to schedule a task for a root. There's only one task per
// root; if a task was already scheduled, we'll check to make sure the
// expiration time of the existing task is the same as the expiration time of
// the next level that the root has work on. This function is called on every
// update, and right before exiting a task.
// root; if a task was already scheduled, we'll check to make sure the priority
// of the existing task is the same as the priority of the next level that the
// root has work on. This function is called on every update, and right before
// exiting a task.
function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
const existingCallbackNode = root.callbackNode;

Expand All @@ -682,37 +682,32 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
markStarvedLanesAsExpired(root, currentTime);

// Determine the next lanes to work on, and their priority.
const newCallbackId = getNextLanes(
const nextLanes = getNextLanes(
root,
root === workInProgressRoot ? workInProgressRootRenderLanes : NoLanes,
);
// This returns the priority level computed during the `getNextLanes` call.
const newCallbackPriority = returnNextLanesPriority();

if (newCallbackId === NoLanes) {
if (nextLanes === NoLanes) {
// Special case: There's nothing to work on.
if (existingCallbackNode !== null) {
cancelCallback(existingCallbackNode);
root.callbackNode = null;
root.callbackPriority = NoLanePriority;
root.callbackId = NoLanes;
}
return;
}

// Check if there's an existing task. We may be able to reuse it.
const existingCallbackId = root.callbackId;
const existingCallbackPriority = root.callbackPriority;
if (existingCallbackId !== NoLanes) {
if (newCallbackId === existingCallbackId) {
// This task is already scheduled. Let's check its priority.
if (existingCallbackPriority === newCallbackPriority) {
// The priority hasn't changed. Exit.
return;
}
// The task ID is the same but the priority changed. Cancel the existing
// callback. We'll schedule a new one below.
if (existingCallbackNode !== null) {
const existingCallbackPriority = root.callbackPriority;
if (existingCallbackPriority === newCallbackPriority) {
// The priority hasn't changed. We can reuse the existing task. Exit.
return;
}
// The priority changed. Cancel the existing callback. We'll schedule a new
// one below.
cancelCallback(existingCallbackNode);
}

Expand All @@ -734,7 +729,6 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) {
);
}

root.callbackId = newCallbackId;
root.callbackPriority = newCallbackPriority;
root.callbackNode = newCallbackNode;
}
Expand Down Expand Up @@ -1959,7 +1953,6 @@ function commitRootImpl(root, renderPriorityLevel) {
// commitRoot never returns a continuation; it always finishes synchronously.
// So we can clear these now to allow a new callback to be scheduled.
root.callbackNode = null;
root.callbackId = NoLanes;

// Update the first and last pending times on this root. The new first
// pending time is whatever is left on the root fiber.
Expand Down
8 changes: 3 additions & 5 deletions packages/react-reconciler/src/ReactInternalTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,15 @@ type BaseFiberRootProperties = {|
pendingContext: Object | null,
// Determines if we should attempt to hydrate on the initial mount
+hydrate: boolean,
// Node returned by Scheduler.scheduleCallback
callbackNode: *,

// Used by useMutableSource hook to avoid tearing during hydration.
mutableSourceEagerHydrationData?: Array<
MutableSource<any> | MutableSourceVersion,
> | null,

// Represents the next task that the root should work on, or the current one
// if it's already working.
callbackId: Lanes,
// Node returned by Scheduler.scheduleCallback. Represents the next rendering
// task that the root will work on.
callbackNode: *,
callbackPriority: LanePriority,
eventTimes: LaneMap<number>,
expirationTimes: LaneMap<number>,
Expand Down

0 comments on commit 96ac799

Please sign in to comment.