GeezerVison is showing github.com — Properly reset Profiler nested-update flag (#20253) · react/react@7548dd5
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/ReactFiberWorkLoop.new.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -199,6 +199,7 @@ import { | 199 | 199 | import { | 200 | 200 | markNestedUpdateScheduled, | 201 | 201 | recordCommitTime, | | 202 | + resetNestedUpdateFlag, | 202 | 203 | startProfilerTimer, | 203 | 204 | stopProfilerTimerIfRunningAndRecordDelta, | 204 | 205 | syncNestedUpdateFlag, | @@ -746,6 +747,10 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { | 746 | 747 | // This is the entry point for every concurrent task, i.e. anything that | 747 | 748 | // goes through Scheduler. | 748 | 749 | function performConcurrentWorkOnRoot(root) { | | 750 | + if (enableProfilerTimer && enableProfilerNestedUpdatePhase) { | | 751 | + resetNestedUpdateFlag(); | | 752 | + } | | 753 | + | 749 | 754 | // Since we know we're in a React event, we can clear the current | 750 | 755 | // event time. The next update will compute a new event time. | 751 | 756 | currentEventTime = NoTimestamp; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.old.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -213,6 +213,7 @@ import { | 213 | 213 | markNestedUpdateScheduled, | 214 | 214 | recordCommitTime, | 215 | 215 | recordPassiveEffectDuration, | | 216 | + resetNestedUpdateFlag, | 216 | 217 | startPassiveEffectTimer, | 217 | 218 | startProfilerTimer, | 218 | 219 | stopProfilerTimerIfRunningAndRecordDelta, | @@ -770,6 +771,10 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { | 770 | 771 | // This is the entry point for every concurrent task, i.e. anything that | 771 | 772 | // goes through Scheduler. | 772 | 773 | function performConcurrentWorkOnRoot(root) { | | 774 | + if (enableProfilerTimer && enableProfilerNestedUpdatePhase) { | | 775 | + resetNestedUpdateFlag(); | | 776 | + } | | 777 | + | 773 | 778 | // Since we know we're in a React event, we can clear the current | 774 | 779 | // event time. The next update will compute a new event time. | 775 | 780 | currentEventTime = NoTimestamp; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactProfilerTimer.new.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -68,6 +68,13 @@ function markNestedUpdateScheduled(): void { | 68 | 68 | } | 69 | 69 | } | 70 | 70 |
| | 71 | +function resetNestedUpdateFlag(): void { | | 72 | + if (enableProfilerNestedUpdatePhase) { | | 73 | + currentUpdateIsNested = false; | | 74 | + nestedUpdateScheduled = false; | | 75 | + } | | 76 | +} | | 77 | + | 71 | 78 | function syncNestedUpdateFlag(): void { | 72 | 79 | if (enableProfilerNestedUpdatePhase) { | 73 | 80 | currentUpdateIsNested = nestedUpdateScheduled; | @@ -206,6 +213,7 @@ export { | 206 | 213 | recordCommitTime, | 207 | 214 | recordLayoutEffectDuration, | 208 | 215 | recordPassiveEffectDuration, | | 216 | + resetNestedUpdateFlag, | 209 | 217 | startLayoutEffectTimer, | 210 | 218 | startPassiveEffectTimer, | 211 | 219 | startProfilerTimer, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactProfilerTimer.old.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -68,6 +68,13 @@ function markNestedUpdateScheduled(): void { | 68 | 68 | } | 69 | 69 | } | 70 | 70 |
| | 71 | +function resetNestedUpdateFlag(): void { | | 72 | + if (enableProfilerNestedUpdatePhase) { | | 73 | + currentUpdateIsNested = false; | | 74 | + nestedUpdateScheduled = false; | | 75 | + } | | 76 | +} | | 77 | + | 71 | 78 | function syncNestedUpdateFlag(): void { | 72 | 79 | if (enableProfilerNestedUpdatePhase) { | 73 | 80 | currentUpdateIsNested = nestedUpdateScheduled; | @@ -206,6 +213,7 @@ export { | 206 | 213 | recordCommitTime, | 207 | 214 | recordLayoutEffectDuration, | 208 | 215 | recordPassiveEffectDuration, | | 216 | + resetNestedUpdateFlag, | 209 | 217 | startLayoutEffectTimer, | 210 | 218 | startPassiveEffectTimer, | 211 | 219 | startProfilerTimer, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react/src/__tests__/ReactProfiler-test.internal.js +48Lines changed: 48 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -700,6 +700,54 @@ describe('Profiler', () => { | 700 | 700 | expect(updateCall[5]).toBe(43); // commit time | 701 | 701 | }); | 702 | 702 |
| | 703 | + it('should clear nested-update flag when multiple cascading renders are scheduled', () => { | | 704 | + loadModules({ | | 705 | + enableSchedulerTracing, | | 706 | + useNoopRenderer: true, | | 707 | + }); | | 708 | + | | 709 | + function Component() { | | 710 | + const [didMount, setDidMount] = React.useState(false); | | 711 | + const [didMountAndUpdate, setDidMountAndUpdate] = React.useState( | | 712 | + false, | | 713 | + ); | | 714 | + | | 715 | + React.useLayoutEffect(() => { | | 716 | + setDidMount(true); | | 717 | + }, []); | | 718 | + | | 719 | + React.useEffect(() => { | | 720 | + if (didMount && !didMountAndUpdate) { | | 721 | + setDidMountAndUpdate(true); | | 722 | + } | | 723 | + }, [didMount, didMountAndUpdate]); | | 724 | + | | 725 | + Scheduler.unstable_yieldValue(`${didMount}:${didMountAndUpdate}`); | | 726 | + | | 727 | + return null; | | 728 | + } | | 729 | + | | 730 | + const onRender = jest.fn(); | | 731 | + | | 732 | + ReactNoop.act(() => { | | 733 | + ReactNoop.render( | | 734 | + <React.Profiler id="root" onRender={onRender}> | | 735 | + <Component /> | | 736 | + </React.Profiler>, | | 737 | + ); | | 738 | + }); | | 739 | + expect(Scheduler).toHaveYielded([ | | 740 | + 'false:false', | | 741 | + 'true:false', | | 742 | + 'true:true', | | 743 | + ]); | | 744 | + | | 745 | + expect(onRender).toHaveBeenCalledTimes(3); | | 746 | + expect(onRender.mock.calls[0][1]).toBe('mount'); | | 747 | + expect(onRender.mock.calls[1][1]).toBe('nested-update'); | | 748 | + expect(onRender.mock.calls[2][1]).toBe('update'); | | 749 | + }); | | 750 | + | 703 | 751 | describe('with regard to interruptions', () => { | 704 | 752 | it('should accumulate actual time after a scheduling interruptions', () => { | 705 | 753 | const callback = jest.fn(); | |
|