GeezerVison is showing github.com — Track which fibers scheduled the current render work (#15658) · react/react@dc108b0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCommitWork.new.js +38-2Lines changed: 38 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,6 +37,7 @@ import { | 37 | 37 | enableStrictEffects, | 38 | 38 | deletedTreeCleanUpLevel, | 39 | 39 | enableSuspenseLayoutEffectSemantics, | | 40 | + enableUpdaterTracking, | 40 | 41 | } from 'shared/ReactFeatureFlags'; | 41 | 42 | import { | 42 | 43 | FunctionComponent, | @@ -89,7 +90,7 @@ import { | 89 | 90 | resetCurrentFiber as resetCurrentDebugFiberInDEV, | 90 | 91 | setCurrentFiber as setCurrentDebugFiberInDEV, | 91 | 92 | } from './ReactCurrentFiber'; | 92 | | - | | 93 | +import {isDevToolsPresent} from './ReactFiberDevToolsHook.new'; | 93 | 94 | import {onCommitUnmount} from './ReactFiberDevToolsHook.new'; | 94 | 95 | import {resolveDefaultProps} from './ReactFiberLazyComponent.new'; | 95 | 96 | import { | @@ -137,6 +138,7 @@ import { | 137 | 138 | resolveRetryWakeable, | 138 | 139 | markCommitTimeOfFallback, | 139 | 140 | enqueuePendingPassiveProfilerEffect, | | 141 | + restorePendingUpdaters, | 140 | 142 | } from './ReactFiberWorkLoop.new'; | 141 | 143 | import { | 142 | 144 | NoFlags as NoHookEffect, | @@ -162,6 +164,10 @@ const PossiblyWeakSet = typeof WeakSet === 'function' ? WeakSet : Set; | 162 | 164 |
| 163 | 165 | let nextEffect: Fiber | null = null; | 164 | 166 |
| | 167 | +// Used for Profiling builds to track updaters. | | 168 | +let inProgressLanes: Lanes | null = null; | | 169 | +let inProgressRoot: FiberRoot | null = null; | | 170 | + | 165 | 171 | const callComponentWillUnmountWithTimer = function(current, instance) { | 166 | 172 | instance.props = current.memoizedProps; | 167 | 173 | instance.state = current.memoizedState; | @@ -2094,6 +2100,20 @@ function attachSuspenseRetryListeners(finishedWork: Fiber) { | 2094 | 2100 | } | 2095 | 2101 | } | 2096 | 2102 | retryCache.add(wakeable); | | 2103 | + | | 2104 | + if (enableUpdaterTracking) { | | 2105 | + if (isDevToolsPresent) { | | 2106 | + if (inProgressLanes !== null && inProgressRoot !== null) { | | 2107 | + // If we have pending work still, associate the original updaters with it. | | 2108 | + restorePendingUpdaters(inProgressRoot, inProgressLanes); | | 2109 | + } else { | | 2110 | + throw Error( | | 2111 | + 'Expected finished root and lanes to be set. This is a bug in React.', | | 2112 | + ); | | 2113 | + } | | 2114 | + } | | 2115 | + } | | 2116 | + | 2097 | 2117 | wakeable.then(retry, retry); | 2098 | 2118 | } | 2099 | 2119 | }); | @@ -2124,9 +2144,19 @@ function commitResetTextContent(current: Fiber) { | 2124 | 2144 | resetTextContent(current.stateNode); | 2125 | 2145 | } | 2126 | 2146 |
| 2127 | | -export function commitMutationEffects(root: FiberRoot, firstChild: Fiber) { | | 2147 | +export function commitMutationEffects( | | 2148 | + root: FiberRoot, | | 2149 | + firstChild: Fiber, | | 2150 | + committedLanes: Lanes, | | 2151 | +) { | | 2152 | + inProgressLanes = committedLanes; | | 2153 | + inProgressRoot = root; | 2128 | 2154 | nextEffect = firstChild; | | 2155 | + | 2129 | 2156 | commitMutationEffects_begin(root); | | 2157 | + | | 2158 | + inProgressLanes = null; | | 2159 | + inProgressRoot = null; | 2130 | 2160 | } | 2131 | 2161 |
| 2132 | 2162 | function commitMutationEffects_begin(root: FiberRoot) { | @@ -2280,8 +2310,14 @@ export function commitLayoutEffects( | 2280 | 2310 | root: FiberRoot, | 2281 | 2311 | committedLanes: Lanes, | 2282 | 2312 | ): void { | | 2313 | + inProgressLanes = committedLanes; | | 2314 | + inProgressRoot = root; | 2283 | 2315 | nextEffect = finishedWork; | | 2316 | + | 2284 | 2317 | commitLayoutEffects_begin(finishedWork, root, committedLanes); | | 2318 | + | | 2319 | + inProgressLanes = null; | | 2320 | + inProgressRoot = null; | 2285 | 2321 | } | 2286 | 2322 |
| 2287 | 2323 | function commitLayoutEffects_begin( | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCommitWork.old.js +38-2Lines changed: 38 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,6 +37,7 @@ import { | 37 | 37 | enableStrictEffects, | 38 | 38 | deletedTreeCleanUpLevel, | 39 | 39 | enableSuspenseLayoutEffectSemantics, | | 40 | + enableUpdaterTracking, | 40 | 41 | } from 'shared/ReactFeatureFlags'; | 41 | 42 | import { | 42 | 43 | FunctionComponent, | @@ -89,7 +90,7 @@ import { | 89 | 90 | resetCurrentFiber as resetCurrentDebugFiberInDEV, | 90 | 91 | setCurrentFiber as setCurrentDebugFiberInDEV, | 91 | 92 | } from './ReactCurrentFiber'; | 92 | | - | | 93 | +import {isDevToolsPresent} from './ReactFiberDevToolsHook.old'; | 93 | 94 | import {onCommitUnmount} from './ReactFiberDevToolsHook.old'; | 94 | 95 | import {resolveDefaultProps} from './ReactFiberLazyComponent.old'; | 95 | 96 | import { | @@ -137,6 +138,7 @@ import { | 137 | 138 | resolveRetryWakeable, | 138 | 139 | markCommitTimeOfFallback, | 139 | 140 | enqueuePendingPassiveProfilerEffect, | | 141 | + restorePendingUpdaters, | 140 | 142 | } from './ReactFiberWorkLoop.old'; | 141 | 143 | import { | 142 | 144 | NoFlags as NoHookEffect, | @@ -162,6 +164,10 @@ const PossiblyWeakSet = typeof WeakSet === 'function' ? WeakSet : Set; | 162 | 164 |
| 163 | 165 | let nextEffect: Fiber | null = null; | 164 | 166 |
| | 167 | +// Used for Profiling builds to track updaters. | | 168 | +let inProgressLanes: Lanes | null = null; | | 169 | +let inProgressRoot: FiberRoot | null = null; | | 170 | + | 165 | 171 | const callComponentWillUnmountWithTimer = function(current, instance) { | 166 | 172 | instance.props = current.memoizedProps; | 167 | 173 | instance.state = current.memoizedState; | @@ -2094,6 +2100,20 @@ function attachSuspenseRetryListeners(finishedWork: Fiber) { | 2094 | 2100 | } | 2095 | 2101 | } | 2096 | 2102 | retryCache.add(wakeable); | | 2103 | + | | 2104 | + if (enableUpdaterTracking) { | | 2105 | + if (isDevToolsPresent) { | | 2106 | + if (inProgressLanes !== null && inProgressRoot !== null) { | | 2107 | + // If we have pending work still, associate the original updaters with it. | | 2108 | + restorePendingUpdaters(inProgressRoot, inProgressLanes); | | 2109 | + } else { | | 2110 | + throw Error( | | 2111 | + 'Expected finished root and lanes to be set. This is a bug in React.', | | 2112 | + ); | | 2113 | + } | | 2114 | + } | | 2115 | + } | | 2116 | + | 2097 | 2117 | wakeable.then(retry, retry); | 2098 | 2118 | } | 2099 | 2119 | }); | @@ -2124,9 +2144,19 @@ function commitResetTextContent(current: Fiber) { | 2124 | 2144 | resetTextContent(current.stateNode); | 2125 | 2145 | } | 2126 | 2146 |
| 2127 | | -export function commitMutationEffects(root: FiberRoot, firstChild: Fiber) { | | 2147 | +export function commitMutationEffects( | | 2148 | + root: FiberRoot, | | 2149 | + firstChild: Fiber, | | 2150 | + committedLanes: Lanes, | | 2151 | +) { | | 2152 | + inProgressLanes = committedLanes; | | 2153 | + inProgressRoot = root; | 2128 | 2154 | nextEffect = firstChild; | | 2155 | + | 2129 | 2156 | commitMutationEffects_begin(root); | | 2157 | + | | 2158 | + inProgressLanes = null; | | 2159 | + inProgressRoot = null; | 2130 | 2160 | } | 2131 | 2161 |
| 2132 | 2162 | function commitMutationEffects_begin(root: FiberRoot) { | @@ -2280,8 +2310,14 @@ export function commitLayoutEffects( | 2280 | 2310 | root: FiberRoot, | 2281 | 2311 | committedLanes: Lanes, | 2282 | 2312 | ): void { | | 2313 | + inProgressLanes = committedLanes; | | 2314 | + inProgressRoot = root; | 2283 | 2315 | nextEffect = finishedWork; | | 2316 | + | 2284 | 2317 | commitLayoutEffects_begin(finishedWork, root, committedLanes); | | 2318 | + | | 2319 | + inProgressLanes = null; | | 2320 | + inProgressRoot = null; | 2285 | 2321 | } | 2286 | 2322 |
| 2287 | 2323 | function commitLayoutEffects_begin( | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberLane.new.js +57-1Lines changed: 57 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,7 +35,12 @@ export type Lanes = number; | 35 | 35 | export type Lane = number; | 36 | 36 | export type LaneMap<T> = Array<T>; | 37 | 37 |
| 38 | | -import {enableCache, enableSchedulingProfiler} from 'shared/ReactFeatureFlags'; | | 38 | +import { | | 39 | + enableCache, | | 40 | + enableSchedulingProfiler, | | 41 | + enableUpdaterTracking, | | 42 | +} from 'shared/ReactFeatureFlags'; | | 43 | +import {isDevToolsPresent} from './ReactFiberDevToolsHook.new'; | 39 | 44 |
| 40 | 45 | // Lane values below should be kept in sync with getLabelsForLanes(), used by react-devtools-scheduling-profiler. | 41 | 46 | // If those values are changed that package should be rebuilt and redeployed. | @@ -742,6 +747,57 @@ export function getBumpedLaneForHydration( | 742 | 747 | return lane; | 743 | 748 | } | 744 | 749 |
| | 750 | +export function addFiberToLanesMap( | | 751 | + root: FiberRoot, | | 752 | + fiber: Fiber, | | 753 | + lanes: Lanes | Lane, | | 754 | +) { | | 755 | + if (!enableUpdaterTracking) { | | 756 | + return; | | 757 | + } | | 758 | + if (!isDevToolsPresent) { | | 759 | + return; | | 760 | + } | | 761 | + const pendingUpdatersLaneMap = root.pendingUpdatersLaneMap; | | 762 | + while (lanes > 0) { | | 763 | + const index = laneToIndex(lanes); | | 764 | + const lane = 1 << index; | | 765 | + | | 766 | + const updaters = pendingUpdatersLaneMap[index]; | | 767 | + updaters.add(fiber); | | 768 | + | | 769 | + lanes &= ~lane; | | 770 | + } | | 771 | +} | | 772 | + | | 773 | +export function movePendingFibersToMemoized(root: FiberRoot, lanes: Lanes) { | | 774 | + if (!enableUpdaterTracking) { | | 775 | + return; | | 776 | + } | | 777 | + if (!isDevToolsPresent) { | | 778 | + return; | | 779 | + } | | 780 | + const pendingUpdatersLaneMap = root.pendingUpdatersLaneMap; | | 781 | + const memoizedUpdaters = root.memoizedUpdaters; | | 782 | + while (lanes > 0) { | | 783 | + const index = laneToIndex(lanes); | | 784 | + const lane = 1 << index; | | 785 | + | | 786 | + const updaters = pendingUpdatersLaneMap[index]; | | 787 | + if (updaters.size > 0) { | | 788 | + updaters.forEach(fiber => { | | 789 | + const alternate = fiber.alternate; | | 790 | + if (alternate === null || !memoizedUpdaters.has(alternate)) { | | 791 | + memoizedUpdaters.add(fiber); | | 792 | + } | | 793 | + }); | | 794 | + updaters.clear(); | | 795 | + } | | 796 | + | | 797 | + lanes &= ~lane; | | 798 | + } | | 799 | +} | | 800 | + | 745 | 801 | const clz32 = Math.clz32 ? Math.clz32 : clz32Fallback; | 746 | 802 |
| 747 | 803 | // Count leading zeros. Only used on lanes, so assume input is an integer. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberLane.old.js +57-1Lines changed: 57 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,7 +35,12 @@ export type Lanes = number; | 35 | 35 | export type Lane = number; | 36 | 36 | export type LaneMap<T> = Array<T>; | 37 | 37 |
| 38 | | -import {enableCache, enableSchedulingProfiler} from 'shared/ReactFeatureFlags'; | | 38 | +import { | | 39 | + enableCache, | | 40 | + enableSchedulingProfiler, | | 41 | + enableUpdaterTracking, | | 42 | +} from 'shared/ReactFeatureFlags'; | | 43 | +import {isDevToolsPresent} from './ReactFiberDevToolsHook.old'; | 39 | 44 |
| 40 | 45 | // Lane values below should be kept in sync with getLabelsForLanes(), used by react-devtools-scheduling-profiler. | 41 | 46 | // If those values are changed that package should be rebuilt and redeployed. | @@ -742,6 +747,57 @@ export function getBumpedLaneForHydration( | 742 | 747 | return lane; | 743 | 748 | } | 744 | 749 |
| | 750 | +export function addFiberToLanesMap( | | 751 | + root: FiberRoot, | | 752 | + fiber: Fiber, | | 753 | + lanes: Lanes | Lane, | | 754 | +) { | | 755 | + if (!enableUpdaterTracking) { | | 756 | + return; | | 757 | + } | | 758 | + if (!isDevToolsPresent) { | | 759 | + return; | | 760 | + } | | 761 | + const pendingUpdatersLaneMap = root.pendingUpdatersLaneMap; | | 762 | + while (lanes > 0) { | | 763 | + const index = laneToIndex(lanes); | | 764 | + const lane = 1 << index; | | 765 | + | | 766 | + const updaters = pendingUpdatersLaneMap[index]; | | 767 | + updaters.add(fiber); | | 768 | + | | 769 | + lanes &= ~lane; | | 770 | + } | | 771 | +} | | 772 | + | | 773 | +export function movePendingFibersToMemoized(root: FiberRoot, lanes: Lanes) { | | 774 | + if (!enableUpdaterTracking) { | | 775 | + return; | | 776 | + } | | 777 | + if (!isDevToolsPresent) { | | 778 | + return; | | 779 | + } | | 780 | + const pendingUpdatersLaneMap = root.pendingUpdatersLaneMap; | | 781 | + const memoizedUpdaters = root.memoizedUpdaters; | | 782 | + while (lanes > 0) { | | 783 | + const index = laneToIndex(lanes); | | 784 | + const lane = 1 << index; | | 785 | + | | 786 | + const updaters = pendingUpdatersLaneMap[index]; | | 787 | + if (updaters.size > 0) { | | 788 | + updaters.forEach(fiber => { | | 789 | + const alternate = fiber.alternate; | | 790 | + if (alternate === null || !memoizedUpdaters.has(alternate)) { | | 791 | + memoizedUpdaters.add(fiber); | | 792 | + } | | 793 | + }); | | 794 | + updaters.clear(); | | 795 | + } | | 796 | + | | 797 | + lanes &= ~lane; | | 798 | + } | | 799 | +} | | 800 | + | 745 | 801 | const clz32 = Math.clz32 ? Math.clz32 : clz32Fallback; | 746 | 802 |
| 747 | 803 | // Count leading zeros. Only used on lanes, so assume input is an integer. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberRoot.new.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ import { | 16 | 16 | NoLane, | 17 | 17 | NoLanes, | 18 | 18 | NoTimestamp, | | 19 | + TotalLanes, | 19 | 20 | createLaneMap, | 20 | 21 | } from './ReactFiberLane.new'; | 21 | 22 | import { | @@ -24,6 +25,7 @@ import { | 24 | 25 | enableCache, | 25 | 26 | enableProfilerCommitHooks, | 26 | 27 | enableProfilerTimer, | | 28 | + enableUpdaterTracking, | 27 | 29 | } from 'shared/ReactFeatureFlags'; | 28 | 30 | import {unstable_getThreadID} from 'scheduler/tracing'; | 29 | 31 | import {initializeUpdateQueue} from './ReactUpdateQueue.new'; | @@ -77,6 +79,14 @@ function FiberRootNode(containerInfo, tag, hydrate) { | 77 | 79 | this.passiveEffectDuration = 0; | 78 | 80 | } | 79 | 81 |
| | 82 | + if (enableUpdaterTracking) { | | 83 | + this.memoizedUpdaters = new Set(); | | 84 | + const pendingUpdatersLaneMap = (this.pendingUpdatersLaneMap = []); | | 85 | + for (let i = 0; i < TotalLanes; i++) { | | 86 | + pendingUpdatersLaneMap.push(new Set()); | | 87 | + } | | 88 | + } | | 89 | + | 80 | 90 | if (__DEV__) { | 81 | 91 | switch (tag) { | 82 | 92 | case ConcurrentRoot: | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberRoot.old.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ import { | 16 | 16 | NoLane, | 17 | 17 | NoLanes, | 18 | 18 | NoTimestamp, | | 19 | + TotalLanes, | 19 | 20 | createLaneMap, | 20 | 21 | } from './ReactFiberLane.old'; | 21 | 22 | import { | @@ -24,6 +25,7 @@ import { | 24 | 25 | enableCache, | 25 | 26 | enableProfilerCommitHooks, | 26 | 27 | enableProfilerTimer, | | 28 | + enableUpdaterTracking, | 27 | 29 | } from 'shared/ReactFeatureFlags'; | 28 | 30 | import {unstable_getThreadID} from 'scheduler/tracing'; | 29 | 31 | import {initializeUpdateQueue} from './ReactUpdateQueue.old'; | @@ -77,6 +79,14 @@ function FiberRootNode(containerInfo, tag, hydrate) { | 77 | 79 | this.passiveEffectDuration = 0; | 78 | 80 | } | 79 | 81 |
| | 82 | + if (enableUpdaterTracking) { | | 83 | + this.memoizedUpdaters = new Set(); | | 84 | + const pendingUpdatersLaneMap = (this.pendingUpdatersLaneMap = []); | | 85 | + for (let i = 0; i < TotalLanes; i++) { | | 86 | + pendingUpdatersLaneMap.push(new Set()); | | 87 | + } | | 88 | + } | | 89 | + | 80 | 90 | if (__DEV__) { | 81 | 91 | switch (tag) { | 82 | 92 | case ConcurrentRoot: | |
|