From 669ddcd224fe96186b9fae324dda6d97dae7273f Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Thu, 5 Sep 2024 22:38:46 -0400 Subject: [PATCH] Remove use of .alternate in root and recordProfilingDurations --- .../src/backend/fiber/renderer.js | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/packages/react-devtools-shared/src/backend/fiber/renderer.js b/packages/react-devtools-shared/src/backend/fiber/renderer.js index 4fada3907f0ee..fb9a987d2573b 100644 --- a/packages/react-devtools-shared/src/backend/fiber/renderer.js +++ b/packages/react-devtools-shared/src/backend/fiber/renderer.js @@ -2363,7 +2363,7 @@ export function attach( } if (isProfilingSupported) { - recordProfilingDurations(fiberInstance); + recordProfilingDurations(fiberInstance, null); } return fiberInstance; } @@ -2929,7 +2929,10 @@ export function attach( removeChild(instance, null); } - function recordProfilingDurations(fiberInstance: FiberInstance) { + function recordProfilingDurations( + fiberInstance: FiberInstance, + prevFiber: null | Fiber, + ) { const id = fiberInstance.id; const fiber = fiberInstance.data; const {actualDuration, treeBaseDuration} = fiber; @@ -2937,13 +2940,11 @@ export function attach( fiberInstance.treeBaseDuration = treeBaseDuration || 0; if (isProfiling) { - const {alternate} = fiber; - // It's important to update treeBaseDuration even if the current Fiber did not render, // because it's possible that one of its descendants did. if ( - alternate == null || - treeBaseDuration !== alternate.treeBaseDuration + prevFiber == null || + treeBaseDuration !== prevFiber.treeBaseDuration ) { // Tree base duration updates are included in the operations typed array. // So we have to convert them from milliseconds to microseconds so we can send them as ints. @@ -2955,7 +2956,7 @@ export function attach( pushOperation(convertedTreeBaseDuration); } - if (alternate == null || didFiberRender(alternate, fiber)) { + if (prevFiber == null || didFiberRender(prevFiber, fiber)) { if (actualDuration != null) { // The actual duration reported by React includes time spent working on children. // This is useful information, but it's also useful to be able to exclude child durations. @@ -2983,7 +2984,7 @@ export function attach( ); if (recordChangeDescriptions) { - const changeDescription = getChangeDescription(alternate, fiber); + const changeDescription = getChangeDescription(prevFiber, fiber); if (changeDescription !== null) { if (metadata.changeDescriptions !== null) { metadata.changeDescriptions.set(id, changeDescription); @@ -3306,8 +3307,6 @@ export function attach( // Register the new alternate in case it's not already in. fiberToFiberInstanceMap.set(nextChild, fiberInstance); - // Update the Fiber so we that we always keep the current Fiber on the data. - fiberInstance.data = nextChild; moveChild(fiberInstance, previousSiblingOfExistingInstance); if ( @@ -3447,6 +3446,8 @@ export function attach( const stashedPrevious = previouslyReconciledSibling; const stashedRemaining = remainingReconcilingChildren; if (fiberInstance !== null) { + // Update the Fiber so we that we always keep the current Fiber on the data. + fiberInstance.data = nextFiber; if ( mostRecentlyInspectedElement !== null && mostRecentlyInspectedElement.id === fiberInstance.id && @@ -3602,7 +3603,7 @@ export function attach( const isProfilingSupported = nextFiber.hasOwnProperty('treeBaseDuration'); if (isProfilingSupported) { - recordProfilingDurations(fiberInstance); + recordProfilingDurations(fiberInstance, prevFiber); } } if (shouldResetChildren) { @@ -3673,11 +3674,11 @@ export function attach( // If we have not been profiling, then we can just walk the tree and build up its current state as-is. hook.getFiberRoots(rendererID).forEach(root => { const current = root.current; - const alternate = current.alternate; const newRoot = createFiberInstance(current); rootToFiberInstanceMap.set(root, newRoot); idToDevToolsInstanceMap.set(newRoot.id, newRoot); fiberToFiberInstanceMap.set(current, newRoot); + const alternate = current.alternate; if (alternate) { fiberToFiberInstanceMap.set(alternate, newRoot); } @@ -3747,20 +3748,22 @@ export function attach( priorityLevel: void | number, ) { const current = root.current; - const alternate = current.alternate; + let prevFiber: null | Fiber = null; let rootInstance = rootToFiberInstanceMap.get(root); if (!rootInstance) { rootInstance = createFiberInstance(current); rootToFiberInstanceMap.set(root, rootInstance); idToDevToolsInstanceMap.set(rootInstance.id, rootInstance); fiberToFiberInstanceMap.set(current, rootInstance); + const alternate = current.alternate; if (alternate) { fiberToFiberInstanceMap.set(alternate, rootInstance); } currentRootID = rootInstance.id; } else { currentRootID = rootInstance.id; + prevFiber = rootInstance.data; } // Before the traversals, remember to start tracking @@ -3796,13 +3799,13 @@ export function attach( }; } - if (alternate) { + if (prevFiber !== null) { // TODO: relying on this seems a bit fishy. const wasMounted = - alternate.memoizedState != null && - alternate.memoizedState.element != null && + prevFiber.memoizedState != null && + prevFiber.memoizedState.element != null && // A dehydrated root is not considered mounted - alternate.memoizedState.isDehydrated !== true; + prevFiber.memoizedState.isDehydrated !== true; const isMounted = current.memoizedState != null && current.memoizedState.element != null && @@ -3814,7 +3817,7 @@ export function attach( mountFiberRecursively(current, false); } else if (wasMounted && isMounted) { // Update an existing root. - updateFiberRecursively(rootInstance, current, alternate, false); + updateFiberRecursively(rootInstance, current, prevFiber, false); } else if (wasMounted && !isMounted) { // Unmount an existing root. unmountInstanceRecursively(rootInstance);