Skip to content

Commit baf4746

Browse files
authored
[DevTools] Remove use of .alternate in root and recordProfilingDurations (#30895)
Ideally we shouldn't use the `.alternate` to access previous state because ideally Fibers shouldn't have alternates. The only case it's ok to use it is when it is used to identity the stateful part of a component's identity. In a non-alternate Fiber model there would instead be another object that represents instance but in the current model it's modeled by the pair. It's not ok is to get the previous state of the tree since that would not live on the stateful part. We don't generally need this though because we have the previous state on instance.data before updating it, or passed from above.
1 parent d76a565 commit baf4746

File tree

1 file changed

+21
-18
lines changed
  • packages/react-devtools-shared/src/backend/fiber

1 file changed

+21
-18
lines changed

packages/react-devtools-shared/src/backend/fiber/renderer.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,7 +2371,7 @@ export function attach(
23712371
}
23722372

23732373
if (isProfilingSupported) {
2374-
recordProfilingDurations(fiberInstance);
2374+
recordProfilingDurations(fiberInstance, null);
23752375
}
23762376
return fiberInstance;
23772377
}
@@ -2937,21 +2937,22 @@ export function attach(
29372937
removeChild(instance, null);
29382938
}
29392939

2940-
function recordProfilingDurations(fiberInstance: FiberInstance) {
2940+
function recordProfilingDurations(
2941+
fiberInstance: FiberInstance,
2942+
prevFiber: null | Fiber,
2943+
) {
29412944
const id = fiberInstance.id;
29422945
const fiber = fiberInstance.data;
29432946
const {actualDuration, treeBaseDuration} = fiber;
29442947

29452948
fiberInstance.treeBaseDuration = treeBaseDuration || 0;
29462949

29472950
if (isProfiling) {
2948-
const {alternate} = fiber;
2949-
29502951
// It's important to update treeBaseDuration even if the current Fiber did not render,
29512952
// because it's possible that one of its descendants did.
29522953
if (
2953-
alternate == null ||
2954-
treeBaseDuration !== alternate.treeBaseDuration
2954+
prevFiber == null ||
2955+
treeBaseDuration !== prevFiber.treeBaseDuration
29552956
) {
29562957
// Tree base duration updates are included in the operations typed array.
29572958
// So we have to convert them from milliseconds to microseconds so we can send them as ints.
@@ -2963,7 +2964,7 @@ export function attach(
29632964
pushOperation(convertedTreeBaseDuration);
29642965
}
29652966

2966-
if (alternate == null || didFiberRender(alternate, fiber)) {
2967+
if (prevFiber == null || didFiberRender(prevFiber, fiber)) {
29672968
if (actualDuration != null) {
29682969
// The actual duration reported by React includes time spent working on children.
29692970
// This is useful information, but it's also useful to be able to exclude child durations.
@@ -2991,7 +2992,7 @@ export function attach(
29912992
);
29922993

29932994
if (recordChangeDescriptions) {
2994-
const changeDescription = getChangeDescription(alternate, fiber);
2995+
const changeDescription = getChangeDescription(prevFiber, fiber);
29952996
if (changeDescription !== null) {
29962997
if (metadata.changeDescriptions !== null) {
29972998
metadata.changeDescriptions.set(id, changeDescription);
@@ -3314,8 +3315,6 @@ export function attach(
33143315
// Register the new alternate in case it's not already in.
33153316
fiberToFiberInstanceMap.set(nextChild, fiberInstance);
33163317

3317-
// Update the Fiber so we that we always keep the current Fiber on the data.
3318-
fiberInstance.data = nextChild;
33193318
moveChild(fiberInstance, previousSiblingOfExistingInstance);
33203319

33213320
if (
@@ -3455,6 +3454,8 @@ export function attach(
34553454
const stashedPrevious = previouslyReconciledSibling;
34563455
const stashedRemaining = remainingReconcilingChildren;
34573456
if (fiberInstance !== null) {
3457+
// Update the Fiber so we that we always keep the current Fiber on the data.
3458+
fiberInstance.data = nextFiber;
34583459
if (
34593460
mostRecentlyInspectedElement !== null &&
34603461
mostRecentlyInspectedElement.id === fiberInstance.id &&
@@ -3610,7 +3611,7 @@ export function attach(
36103611
const isProfilingSupported =
36113612
nextFiber.hasOwnProperty('treeBaseDuration');
36123613
if (isProfilingSupported) {
3613-
recordProfilingDurations(fiberInstance);
3614+
recordProfilingDurations(fiberInstance, prevFiber);
36143615
}
36153616
}
36163617
if (shouldResetChildren) {
@@ -3681,11 +3682,11 @@ export function attach(
36813682
// If we have not been profiling, then we can just walk the tree and build up its current state as-is.
36823683
hook.getFiberRoots(rendererID).forEach(root => {
36833684
const current = root.current;
3684-
const alternate = current.alternate;
36853685
const newRoot = createFiberInstance(current);
36863686
rootToFiberInstanceMap.set(root, newRoot);
36873687
idToDevToolsInstanceMap.set(newRoot.id, newRoot);
36883688
fiberToFiberInstanceMap.set(current, newRoot);
3689+
const alternate = current.alternate;
36893690
if (alternate) {
36903691
fiberToFiberInstanceMap.set(alternate, newRoot);
36913692
}
@@ -3755,20 +3756,22 @@ export function attach(
37553756
priorityLevel: void | number,
37563757
) {
37573758
const current = root.current;
3758-
const alternate = current.alternate;
37593759

3760+
let prevFiber: null | Fiber = null;
37603761
let rootInstance = rootToFiberInstanceMap.get(root);
37613762
if (!rootInstance) {
37623763
rootInstance = createFiberInstance(current);
37633764
rootToFiberInstanceMap.set(root, rootInstance);
37643765
idToDevToolsInstanceMap.set(rootInstance.id, rootInstance);
37653766
fiberToFiberInstanceMap.set(current, rootInstance);
3767+
const alternate = current.alternate;
37663768
if (alternate) {
37673769
fiberToFiberInstanceMap.set(alternate, rootInstance);
37683770
}
37693771
currentRootID = rootInstance.id;
37703772
} else {
37713773
currentRootID = rootInstance.id;
3774+
prevFiber = rootInstance.data;
37723775
}
37733776

37743777
// Before the traversals, remember to start tracking
@@ -3804,13 +3807,13 @@ export function attach(
38043807
};
38053808
}
38063809

3807-
if (alternate) {
3810+
if (prevFiber !== null) {
38083811
// TODO: relying on this seems a bit fishy.
38093812
const wasMounted =
3810-
alternate.memoizedState != null &&
3811-
alternate.memoizedState.element != null &&
3813+
prevFiber.memoizedState != null &&
3814+
prevFiber.memoizedState.element != null &&
38123815
// A dehydrated root is not considered mounted
3813-
alternate.memoizedState.isDehydrated !== true;
3816+
prevFiber.memoizedState.isDehydrated !== true;
38143817
const isMounted =
38153818
current.memoizedState != null &&
38163819
current.memoizedState.element != null &&
@@ -3822,7 +3825,7 @@ export function attach(
38223825
mountFiberRecursively(current, false);
38233826
} else if (wasMounted && isMounted) {
38243827
// Update an existing root.
3825-
updateFiberRecursively(rootInstance, current, alternate, false);
3828+
updateFiberRecursively(rootInstance, current, prevFiber, false);
38263829
} else if (wasMounted && !isMounted) {
38273830
// Unmount an existing root.
38283831
unmountInstanceRecursively(rootInstance);

0 commit comments

Comments
 (0)