Skip to content

Commit 669ddcd

Browse files
committed
Remove use of .alternate in root and recordProfilingDurations
1 parent a03254b commit 669ddcd

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
@@ -2363,7 +2363,7 @@ export function attach(
23632363
}
23642364

23652365
if (isProfilingSupported) {
2366-
recordProfilingDurations(fiberInstance);
2366+
recordProfilingDurations(fiberInstance, null);
23672367
}
23682368
return fiberInstance;
23692369
}
@@ -2929,21 +2929,22 @@ export function attach(
29292929
removeChild(instance, null);
29302930
}
29312931

2932-
function recordProfilingDurations(fiberInstance: FiberInstance) {
2932+
function recordProfilingDurations(
2933+
fiberInstance: FiberInstance,
2934+
prevFiber: null | Fiber,
2935+
) {
29332936
const id = fiberInstance.id;
29342937
const fiber = fiberInstance.data;
29352938
const {actualDuration, treeBaseDuration} = fiber;
29362939

29372940
fiberInstance.treeBaseDuration = treeBaseDuration || 0;
29382941

29392942
if (isProfiling) {
2940-
const {alternate} = fiber;
2941-
29422943
// It's important to update treeBaseDuration even if the current Fiber did not render,
29432944
// because it's possible that one of its descendants did.
29442945
if (
2945-
alternate == null ||
2946-
treeBaseDuration !== alternate.treeBaseDuration
2946+
prevFiber == null ||
2947+
treeBaseDuration !== prevFiber.treeBaseDuration
29472948
) {
29482949
// Tree base duration updates are included in the operations typed array.
29492950
// So we have to convert them from milliseconds to microseconds so we can send them as ints.
@@ -2955,7 +2956,7 @@ export function attach(
29552956
pushOperation(convertedTreeBaseDuration);
29562957
}
29572958

2958-
if (alternate == null || didFiberRender(alternate, fiber)) {
2959+
if (prevFiber == null || didFiberRender(prevFiber, fiber)) {
29592960
if (actualDuration != null) {
29602961
// The actual duration reported by React includes time spent working on children.
29612962
// This is useful information, but it's also useful to be able to exclude child durations.
@@ -2983,7 +2984,7 @@ export function attach(
29832984
);
29842985

29852986
if (recordChangeDescriptions) {
2986-
const changeDescription = getChangeDescription(alternate, fiber);
2987+
const changeDescription = getChangeDescription(prevFiber, fiber);
29872988
if (changeDescription !== null) {
29882989
if (metadata.changeDescriptions !== null) {
29892990
metadata.changeDescriptions.set(id, changeDescription);
@@ -3306,8 +3307,6 @@ export function attach(
33063307
// Register the new alternate in case it's not already in.
33073308
fiberToFiberInstanceMap.set(nextChild, fiberInstance);
33083309

3309-
// Update the Fiber so we that we always keep the current Fiber on the data.
3310-
fiberInstance.data = nextChild;
33113310
moveChild(fiberInstance, previousSiblingOfExistingInstance);
33123311

33133312
if (
@@ -3447,6 +3446,8 @@ export function attach(
34473446
const stashedPrevious = previouslyReconciledSibling;
34483447
const stashedRemaining = remainingReconcilingChildren;
34493448
if (fiberInstance !== null) {
3449+
// Update the Fiber so we that we always keep the current Fiber on the data.
3450+
fiberInstance.data = nextFiber;
34503451
if (
34513452
mostRecentlyInspectedElement !== null &&
34523453
mostRecentlyInspectedElement.id === fiberInstance.id &&
@@ -3602,7 +3603,7 @@ export function attach(
36023603
const isProfilingSupported =
36033604
nextFiber.hasOwnProperty('treeBaseDuration');
36043605
if (isProfilingSupported) {
3605-
recordProfilingDurations(fiberInstance);
3606+
recordProfilingDurations(fiberInstance, prevFiber);
36063607
}
36073608
}
36083609
if (shouldResetChildren) {
@@ -3673,11 +3674,11 @@ export function attach(
36733674
// If we have not been profiling, then we can just walk the tree and build up its current state as-is.
36743675
hook.getFiberRoots(rendererID).forEach(root => {
36753676
const current = root.current;
3676-
const alternate = current.alternate;
36773677
const newRoot = createFiberInstance(current);
36783678
rootToFiberInstanceMap.set(root, newRoot);
36793679
idToDevToolsInstanceMap.set(newRoot.id, newRoot);
36803680
fiberToFiberInstanceMap.set(current, newRoot);
3681+
const alternate = current.alternate;
36813682
if (alternate) {
36823683
fiberToFiberInstanceMap.set(alternate, newRoot);
36833684
}
@@ -3747,20 +3748,22 @@ export function attach(
37473748
priorityLevel: void | number,
37483749
) {
37493750
const current = root.current;
3750-
const alternate = current.alternate;
37513751

3752+
let prevFiber: null | Fiber = null;
37523753
let rootInstance = rootToFiberInstanceMap.get(root);
37533754
if (!rootInstance) {
37543755
rootInstance = createFiberInstance(current);
37553756
rootToFiberInstanceMap.set(root, rootInstance);
37563757
idToDevToolsInstanceMap.set(rootInstance.id, rootInstance);
37573758
fiberToFiberInstanceMap.set(current, rootInstance);
3759+
const alternate = current.alternate;
37583760
if (alternate) {
37593761
fiberToFiberInstanceMap.set(alternate, rootInstance);
37603762
}
37613763
currentRootID = rootInstance.id;
37623764
} else {
37633765
currentRootID = rootInstance.id;
3766+
prevFiber = rootInstance.data;
37643767
}
37653768

37663769
// Before the traversals, remember to start tracking
@@ -3796,13 +3799,13 @@ export function attach(
37963799
};
37973800
}
37983801

3799-
if (alternate) {
3802+
if (prevFiber !== null) {
38003803
// TODO: relying on this seems a bit fishy.
38013804
const wasMounted =
3802-
alternate.memoizedState != null &&
3803-
alternate.memoizedState.element != null &&
3805+
prevFiber.memoizedState != null &&
3806+
prevFiber.memoizedState.element != null &&
38043807
// A dehydrated root is not considered mounted
3805-
alternate.memoizedState.isDehydrated !== true;
3808+
prevFiber.memoizedState.isDehydrated !== true;
38063809
const isMounted =
38073810
current.memoizedState != null &&
38083811
current.memoizedState.element != null &&
@@ -3814,7 +3817,7 @@ export function attach(
38143817
mountFiberRecursively(current, false);
38153818
} else if (wasMounted && isMounted) {
38163819
// Update an existing root.
3817-
updateFiberRecursively(rootInstance, current, alternate, false);
3820+
updateFiberRecursively(rootInstance, current, prevFiber, false);
38183821
} else if (wasMounted && !isMounted) {
38193822
// Unmount an existing root.
38203823
unmountInstanceRecursively(rootInstance);

0 commit comments

Comments
 (0)