GeezerVison is showing github.com — Add "nested-update" phase to Profiler API (#20163) · react/react@393c452
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 +22-11Lines changed: 22 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,7 @@ import { | 31 | 31 | enableSchedulerTracing, | 32 | 32 | enableProfilerTimer, | 33 | 33 | enableProfilerCommitHooks, | | 34 | + enableProfilerNestedUpdatePhase, | 34 | 35 | enableSuspenseServerRenderer, | 35 | 36 | enableFundamentalAPI, | 36 | 37 | enableSuspenseCallback, | @@ -94,6 +95,7 @@ import { | 94 | 95 | import {onCommitUnmount} from './ReactFiberDevToolsHook.new'; | 95 | 96 | import {resolveDefaultProps} from './ReactFiberLazyComponent.new'; | 96 | 97 | import { | | 98 | + isCurrentUpdateNested, | 97 | 99 | getCommitTime, | 98 | 100 | recordLayoutEffectDuration, | 99 | 101 | startLayoutEffectTimer, | @@ -357,22 +359,24 @@ function commitProfilerPassiveEffect( | 357 | 359 | // It does not get reset until the start of the next commit phase. | 358 | 360 | const commitTime = getCommitTime(); | 359 | 361 |
| | 362 | + let phase = finishedWork.alternate === null ? 'mount' : 'update'; | | 363 | + if (enableProfilerNestedUpdatePhase) { | | 364 | + if (isCurrentUpdateNested()) { | | 365 | + phase = 'nested-update'; | | 366 | + } | | 367 | + } | | 368 | + | 360 | 369 | if (typeof onPostCommit === 'function') { | 361 | 370 | if (enableSchedulerTracing) { | 362 | 371 | onPostCommit( | 363 | 372 | id, | 364 | | - finishedWork.alternate === null ? 'mount' : 'update', | | 373 | + phase, | 365 | 374 | passiveEffectDuration, | 366 | 375 | commitTime, | 367 | 376 | finishedRoot.memoizedInteractions, | 368 | 377 | ); | 369 | 378 | } else { | 370 | | - onPostCommit( | 371 | | - id, | 372 | | - finishedWork.alternate === null ? 'mount' : 'update', | 373 | | - passiveEffectDuration, | 374 | | - commitTime, | 375 | | - ); | | 379 | + onPostCommit(id, phase, passiveEffectDuration, commitTime); | 376 | 380 | } | 377 | 381 | } | 378 | 382 | break; | @@ -1333,11 +1337,18 @@ function commitLayoutEffectsForProfiler( | 1333 | 1337 | const OnRenderFlag = Update; | 1334 | 1338 | const OnCommitFlag = Callback; | 1335 | 1339 |
| | 1340 | + let phase = current === null ? 'mount' : 'update'; | | 1341 | + if (enableProfilerNestedUpdatePhase) { | | 1342 | + if (isCurrentUpdateNested()) { | | 1343 | + phase = 'nested-update'; | | 1344 | + } | | 1345 | + } | | 1346 | + | 1336 | 1347 | if ((flags & OnRenderFlag) !== NoFlags && typeof onRender === 'function') { | 1337 | 1348 | if (enableSchedulerTracing) { | 1338 | 1349 | onRender( | 1339 | 1350 | finishedWork.memoizedProps.id, | 1340 | | - current === null ? 'mount' : 'update', | | 1351 | + phase, | 1341 | 1352 | finishedWork.actualDuration, | 1342 | 1353 | finishedWork.treeBaseDuration, | 1343 | 1354 | finishedWork.actualStartTime, | @@ -1347,7 +1358,7 @@ function commitLayoutEffectsForProfiler( | 1347 | 1358 | } else { | 1348 | 1359 | onRender( | 1349 | 1360 | finishedWork.memoizedProps.id, | 1350 | | - current === null ? 'mount' : 'update', | | 1361 | + phase, | 1351 | 1362 | finishedWork.actualDuration, | 1352 | 1363 | finishedWork.treeBaseDuration, | 1353 | 1364 | finishedWork.actualStartTime, | @@ -1364,15 +1375,15 @@ function commitLayoutEffectsForProfiler( | 1364 | 1375 | if (enableSchedulerTracing) { | 1365 | 1376 | onCommit( | 1366 | 1377 | finishedWork.memoizedProps.id, | 1367 | | - current === null ? 'mount' : 'update', | | 1378 | + phase, | 1368 | 1379 | effectDuration, | 1369 | 1380 | commitTime, | 1370 | 1381 | finishedRoot.memoizedInteractions, | 1371 | 1382 | ); | 1372 | 1383 | } else { | 1373 | 1384 | onCommit( | 1374 | 1385 | finishedWork.memoizedProps.id, | 1375 | | - current === null ? 'mount' : 'update', | | 1386 | + phase, | 1376 | 1387 | effectDuration, | 1377 | 1388 | commitTime, | 1378 | 1389 | ); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCommitWork.old.js +22-11Lines changed: 22 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,7 @@ import { | 30 | 30 | enableSchedulerTracing, | 31 | 31 | enableProfilerTimer, | 32 | 32 | enableProfilerCommitHooks, | | 33 | + enableProfilerNestedUpdatePhase, | 33 | 34 | enableSuspenseServerRenderer, | 34 | 35 | enableFundamentalAPI, | 35 | 36 | enableSuspenseCallback, | @@ -73,6 +74,7 @@ import invariant from 'shared/invariant'; | 73 | 74 | import {onCommitUnmount} from './ReactFiberDevToolsHook.old'; | 74 | 75 | import {resolveDefaultProps} from './ReactFiberLazyComponent.old'; | 75 | 76 | import { | | 77 | + isCurrentUpdateNested, | 76 | 78 | getCommitTime, | 77 | 79 | recordLayoutEffectDuration, | 78 | 80 | startLayoutEffectTimer, | @@ -434,22 +436,24 @@ export function commitPassiveEffectDurations( | 434 | 436 | // It does not get reset until the start of the next commit phase. | 435 | 437 | const commitTime = getCommitTime(); | 436 | 438 |
| | 439 | + let phase = finishedWork.alternate === null ? 'mount' : 'update'; | | 440 | + if (enableProfilerNestedUpdatePhase) { | | 441 | + if (isCurrentUpdateNested()) { | | 442 | + phase = 'nested-update'; | | 443 | + } | | 444 | + } | | 445 | + | 437 | 446 | if (typeof onPostCommit === 'function') { | 438 | 447 | if (enableSchedulerTracing) { | 439 | 448 | onPostCommit( | 440 | 449 | id, | 441 | | - finishedWork.alternate === null ? 'mount' : 'update', | | 450 | + phase, | 442 | 451 | passiveEffectDuration, | 443 | 452 | commitTime, | 444 | 453 | finishedRoot.memoizedInteractions, | 445 | 454 | ); | 446 | 455 | } else { | 447 | | - onPostCommit( | 448 | | - id, | 449 | | - finishedWork.alternate === null ? 'mount' : 'update', | 450 | | - passiveEffectDuration, | 451 | | - commitTime, | 452 | | - ); | | 456 | + onPostCommit(id, phase, passiveEffectDuration, commitTime); | 453 | 457 | } | 454 | 458 | } | 455 | 459 |
| @@ -706,11 +710,18 @@ function commitLifeCycles( | 706 | 710 |
| 707 | 711 | const commitTime = getCommitTime(); | 708 | 712 |
| | 713 | + let phase = current === null ? 'mount' : 'update'; | | 714 | + if (enableProfilerNestedUpdatePhase) { | | 715 | + if (isCurrentUpdateNested()) { | | 716 | + phase = 'nested-update'; | | 717 | + } | | 718 | + } | | 719 | + | 709 | 720 | if (typeof onRender === 'function') { | 710 | 721 | if (enableSchedulerTracing) { | 711 | 722 | onRender( | 712 | 723 | finishedWork.memoizedProps.id, | 713 | | - current === null ? 'mount' : 'update', | | 724 | + phase, | 714 | 725 | finishedWork.actualDuration, | 715 | 726 | finishedWork.treeBaseDuration, | 716 | 727 | finishedWork.actualStartTime, | @@ -720,7 +731,7 @@ function commitLifeCycles( | 720 | 731 | } else { | 721 | 732 | onRender( | 722 | 733 | finishedWork.memoizedProps.id, | 723 | | - current === null ? 'mount' : 'update', | | 734 | + phase, | 724 | 735 | finishedWork.actualDuration, | 725 | 736 | finishedWork.treeBaseDuration, | 726 | 737 | finishedWork.actualStartTime, | @@ -734,15 +745,15 @@ function commitLifeCycles( | 734 | 745 | if (enableSchedulerTracing) { | 735 | 746 | onCommit( | 736 | 747 | finishedWork.memoizedProps.id, | 737 | | - current === null ? 'mount' : 'update', | | 748 | + phase, | 738 | 749 | effectDuration, | 739 | 750 | commitTime, | 740 | 751 | finishedRoot.memoizedInteractions, | 741 | 752 | ); | 742 | 753 | } else { | 743 | 754 | onCommit( | 744 | 755 | finishedWork.memoizedProps.id, | 745 | | - current === null ? 'mount' : 'update', | | 756 | + phase, | 746 | 757 | effectDuration, | 747 | 758 | commitTime, | 748 | 759 | ); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.new.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,7 @@ import { | 21 | 21 | enableSuspenseServerRenderer, | 22 | 22 | replayFailedUnitOfWorkWithInvokeGuardedCallback, | 23 | 23 | enableProfilerTimer, | | 24 | + enableProfilerNestedUpdatePhase, | 24 | 25 | enableSchedulerTracing, | 25 | 26 | warnAboutUnmockedScheduler, | 26 | 27 | deferRenderPhaseUpdateToNextBatch, | @@ -194,9 +195,11 @@ import { | 194 | 195 | } from './ReactFiberStack.new'; | 195 | 196 |
| 196 | 197 | import { | | 198 | + markNestedUpdateScheduled, | 197 | 199 | recordCommitTime, | 198 | 200 | startProfilerTimer, | 199 | 201 | stopProfilerTimerIfRunningAndRecordDelta, | | 202 | + syncNestedUpdateFlag, | 200 | 203 | } from './ReactProfilerTimer.new'; | 201 | 204 |
| 202 | 205 | // DEV stuff | @@ -938,6 +941,10 @@ function markRootSuspended(root, suspendedLanes) { | 938 | 941 | // This is the entry point for synchronous tasks that don't go | 939 | 942 | // through Scheduler | 940 | 943 | function performSyncWorkOnRoot(root) { | | 944 | + if (enableProfilerTimer && enableProfilerNestedUpdatePhase) { | | 945 | + syncNestedUpdateFlag(); | | 946 | + } | | 947 | + | 941 | 948 | invariant( | 942 | 949 | (executionContext & (RenderContext | CommitContext)) === NoContext, | 943 | 950 | 'Should not already be working.', | @@ -1995,6 +2002,10 @@ function commitRootImpl(root, renderPriorityLevel) { | 1995 | 2002 | } | 1996 | 2003 |
| 1997 | 2004 | if (remainingLanes === SyncLane) { | | 2005 | + if (enableProfilerTimer && enableProfilerNestedUpdatePhase) { | | 2006 | + markNestedUpdateScheduled(); | | 2007 | + } | | 2008 | + | 1998 | 2009 | // Count the number of times the root synchronously re-renders without | 1999 | 2010 | // finishing. If there are too many, it indicates an infinite update loop. | 2000 | 2011 | if (root === rootWithNestedUpdates) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.old.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,6 +22,7 @@ import { | 22 | 22 | replayFailedUnitOfWorkWithInvokeGuardedCallback, | 23 | 23 | enableProfilerTimer, | 24 | 24 | enableProfilerCommitHooks, | | 25 | + enableProfilerNestedUpdatePhase, | 25 | 26 | enableSchedulerTracing, | 26 | 27 | warnAboutUnmockedScheduler, | 27 | 28 | deferRenderPhaseUpdateToNextBatch, | @@ -207,11 +208,13 @@ import { | 207 | 208 | } from './ReactFiberStack.old'; | 208 | 209 |
| 209 | 210 | import { | | 211 | + markNestedUpdateScheduled, | 210 | 212 | recordCommitTime, | 211 | 213 | recordPassiveEffectDuration, | 212 | 214 | startPassiveEffectTimer, | 213 | 215 | startProfilerTimer, | 214 | 216 | stopProfilerTimerIfRunningAndRecordDelta, | | 217 | + syncNestedUpdateFlag, | 215 | 218 | } from './ReactProfilerTimer.old'; | 216 | 219 |
| 217 | 220 | // DEV stuff | @@ -962,6 +965,10 @@ function markRootSuspended(root, suspendedLanes) { | 962 | 965 | // This is the entry point for synchronous tasks that don't go | 963 | 966 | // through Scheduler | 964 | 967 | function performSyncWorkOnRoot(root) { | | 968 | + if (enableProfilerTimer && enableProfilerNestedUpdatePhase) { | | 969 | + syncNestedUpdateFlag(); | | 970 | + } | | 971 | + | 965 | 972 | invariant( | 966 | 973 | (executionContext & (RenderContext | CommitContext)) === NoContext, | 967 | 974 | 'Should not already be working.', | @@ -2189,6 +2196,10 @@ function commitRootImpl(root, renderPriorityLevel) { | 2189 | 2196 | } | 2190 | 2197 |
| 2191 | 2198 | if (remainingLanes === SyncLane) { | | 2199 | + if (enableProfilerTimer && enableProfilerNestedUpdatePhase) { | | 2200 | + markNestedUpdateScheduled(); | | 2201 | + } | | 2202 | + | 2192 | 2203 | // Count the number of times the root synchronously re-renders without | 2193 | 2204 | // finishing. If there are too many, it indicates an infinite update loop. | 2194 | 2205 | if (root === rootWithNestedUpdates) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactProfilerTimer.new.js +44-1Lines changed: 44 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | import type {Fiber} from './ReactInternalTypes'; | 11 | 11 |
| 12 | 12 | import { | 13 | | - enableProfilerTimer, | 14 | 13 | enableProfilerCommitHooks, | | 14 | + enableProfilerNestedUpdatePhase, | | 15 | + enableProfilerTimer, | 15 | 16 | } from 'shared/ReactFeatureFlags'; | 16 | 17 | import {Profiler} from './ReactWorkTags'; | 17 | 18 |
| @@ -23,10 +24,13 @@ const {unstable_now: now} = Scheduler; | 23 | 24 |
| 24 | 25 | export type ProfilerTimer = { | 25 | 26 | getCommitTime(): number, | | 27 | + isCurrentUpdateNested(): boolean, | | 28 | + markNestedUpdateScheduled(): void, | 26 | 29 | recordCommitTime(): void, | 27 | 30 | startProfilerTimer(fiber: Fiber): void, | 28 | 31 | stopProfilerTimerIfRunning(fiber: Fiber): void, | 29 | 32 | stopProfilerTimerIfRunningAndRecordDelta(fiber: Fiber): void, | | 33 | + syncNestedUpdateFlag(): void, | 30 | 34 | ... | 31 | 35 | }; | 32 | 36 |
| @@ -35,6 +39,42 @@ let layoutEffectStartTime: number = -1; | 35 | 39 | let profilerStartTime: number = -1; | 36 | 40 | let passiveEffectStartTime: number = -1; | 37 | 41 |
| | 42 | +/** | | 43 | + * Tracks whether the current update was a nested/cascading update (scheduled from a layout effect). | | 44 | + * | | 45 | + * The overall sequence is: | | 46 | + * 1. render | | 47 | + * 2. commit (and call `onRender`, `onCommit`) | | 48 | + * 3. check for nested updates | | 49 | + * 4. flush passive effects (and call `onPostCommit`) | | 50 | + * | | 51 | + * Nested updates are identified in step 3 above, | | 52 | + * but step 4 still applies to the work that was just committed. | | 53 | + * We use two flags to track nested updates then: | | 54 | + * one tracks whether the upcoming update is a nested update, | | 55 | + * and the other tracks whether the current update was a nested update. | | 56 | + * The first value gets synced to the second at the start of the render phase. | | 57 | + */ | | 58 | +let currentUpdateIsNested: boolean = false; | | 59 | +let nestedUpdateScheduled: boolean = false; | | 60 | + | | 61 | +function isCurrentUpdateNested(): boolean { | | 62 | + return currentUpdateIsNested; | | 63 | +} | | 64 | + | | 65 | +function markNestedUpdateScheduled(): void { | | 66 | + if (enableProfilerNestedUpdatePhase) { | | 67 | + nestedUpdateScheduled = true; | | 68 | + } | | 69 | +} | | 70 | + | | 71 | +function syncNestedUpdateFlag(): void { | | 72 | + if (enableProfilerNestedUpdatePhase) { | | 73 | + currentUpdateIsNested = nestedUpdateScheduled; | | 74 | + nestedUpdateScheduled = false; | | 75 | + } | | 76 | +} | | 77 | + | 38 | 78 | function getCommitTime(): number { | 39 | 79 | return commitTime; | 40 | 80 | } | @@ -161,6 +201,8 @@ function transferActualDuration(fiber: Fiber): void { | 161 | 201 |
| 162 | 202 | export { | 163 | 203 | getCommitTime, | | 204 | + isCurrentUpdateNested, | | 205 | + markNestedUpdateScheduled, | 164 | 206 | recordCommitTime, | 165 | 207 | recordLayoutEffectDuration, | 166 | 208 | recordPassiveEffectDuration, | @@ -169,5 +211,6 @@ export { | 169 | 211 | startProfilerTimer, | 170 | 212 | stopProfilerTimerIfRunning, | 171 | 213 | stopProfilerTimerIfRunningAndRecordDelta, | | 214 | + syncNestedUpdateFlag, | 172 | 215 | transferActualDuration, | 173 | 216 | }; |
|