GeezerVison is showing github.com — Don't call onCommit et al if there are no effects (#19863) · react/react@81aaee5
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/ReactFiberBeginWork.new.js -15Lines changed: 0 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,8 +60,6 @@ import { | 60 | 60 | Hydrating, | 61 | 61 | ContentReset, | 62 | 62 | DidCapture, | 63 | | - Update, | 64 | | - Passive, | 65 | 63 | Ref, | 66 | 64 | Deletion, | 67 | 65 | ForceUpdateForLegacySuspense, | @@ -675,9 +673,6 @@ function updateProfiler( | 675 | 673 | renderLanes: Lanes, | 676 | 674 | ) { | 677 | 675 | if (enableProfilerTimer) { | 678 | | - // TODO: Only call onRender et al if subtree has effects | 679 | | - workInProgress.flags |= Update | Passive; | 680 | | - | 681 | 676 | // Reset effect durations for the next eventual effect phase. | 682 | 677 | // These are reset during render to allow the DevTools commit hook a chance to read them, | 683 | 678 | const stateNode = workInProgress.stateNode; | @@ -3117,16 +3112,6 @@ function beginWork( | 3117 | 3112 | } | 3118 | 3113 | case Profiler: | 3119 | 3114 | if (enableProfilerTimer) { | 3120 | | - // Profiler should only call onRender when one of its descendants actually rendered. | 3121 | | - // TODO: Only call onRender et al if subtree has effects | 3122 | | - const hasChildWork = includesSomeLane( | 3123 | | - renderLanes, | 3124 | | - workInProgress.childLanes, | 3125 | | - ); | 3126 | | - if (hasChildWork) { | 3127 | | - workInProgress.flags |= Passive | Update; | 3128 | | - } | 3129 | | - | 3130 | 3115 | // Reset effect durations for the next eventual effect phase. | 3131 | 3116 | // These are reset during render to allow the DevTools commit hook a chance to read them, | 3132 | 3117 | const stateNode = workInProgress.stateNode; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCommitWork.new.js +13-2Lines changed: 13 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -68,6 +68,7 @@ import { | 68 | 68 | Placement, | 69 | 69 | Snapshot, | 70 | 70 | Update, | | 71 | + Callback, | 71 | 72 | PassiveMask, | 72 | 73 | } from './ReactFiberFlags'; | 73 | 74 | import getComponentName from 'shared/getComponentName'; | @@ -676,10 +677,17 @@ function commitLifeCycles( | 676 | 677 | if (enableProfilerTimer) { | 677 | 678 | const {onCommit, onRender} = finishedWork.memoizedProps; | 678 | 679 | const {effectDuration} = finishedWork.stateNode; | | 680 | + const flags = finishedWork.flags; | 679 | 681 |
| 680 | 682 | const commitTime = getCommitTime(); | 681 | 683 |
| 682 | | - if (typeof onRender === 'function') { | | 684 | + const OnRenderFlag = Update; | | 685 | + const OnCommitFlag = Callback; | | 686 | + | | 687 | + if ( | | 688 | + (flags & OnRenderFlag) !== NoFlags && | | 689 | + typeof onRender === 'function' | | 690 | + ) { | 683 | 691 | if (enableSchedulerTracing) { | 684 | 692 | onRender( | 685 | 693 | finishedWork.memoizedProps.id, | @@ -703,7 +711,10 @@ function commitLifeCycles( | 703 | 711 | } | 704 | 712 |
| 705 | 713 | if (enableProfilerCommitHooks) { | 706 | | - if (typeof onCommit === 'function') { | | 714 | + if ( | | 715 | + (flags & OnCommitFlag) !== NoFlags && | | 716 | + typeof onCommit === 'function' | | 717 | + ) { | 707 | 718 | if (enableSchedulerTracing) { | 708 | 719 | onCommit( | 709 | 720 | finishedWork.memoizedProps.id, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCompleteWork.new.js +54-1Lines changed: 54 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -67,10 +67,15 @@ import { | 67 | 67 | import { | 68 | 68 | Ref, | 69 | 69 | Update, | | 70 | + Callback, | | 71 | + Passive, | | 72 | + Deletion, | 70 | 73 | NoFlags, | 71 | 74 | DidCapture, | 72 | 75 | Snapshot, | 73 | 76 | MutationMask, | | 77 | + LayoutMask, | | 78 | + PassiveMask, | 74 | 79 | StaticMask, | 75 | 80 | } from './ReactFiberFlags'; | 76 | 81 | import invariant from 'shared/invariant'; | @@ -787,6 +792,8 @@ function bubbleProperties(completedWork: Fiber) { | 787 | 792 | } | 788 | 793 |
| 789 | 794 | completedWork.childLanes = newChildLanes; | | 795 | + | | 796 | + return didBailout; | 790 | 797 | } | 791 | 798 |
| 792 | 799 | function completeWork( | @@ -804,7 +811,6 @@ function completeWork( | 804 | 811 | case ForwardRef: | 805 | 812 | case Fragment: | 806 | 813 | case Mode: | 807 | | - case Profiler: | 808 | 814 | case ContextConsumer: | 809 | 815 | case MemoComponent: | 810 | 816 | bubbleProperties(workInProgress); | @@ -966,6 +972,53 @@ function completeWork( | 966 | 972 | bubbleProperties(workInProgress); | 967 | 973 | return null; | 968 | 974 | } | | 975 | + case Profiler: { | | 976 | + const didBailout = bubbleProperties(workInProgress); | | 977 | + if (!didBailout) { | | 978 | + // Use subtreeFlags to determine which commit callbacks should fire. | | 979 | + // TODO: Move this logic to the commit phase, since we already check if | | 980 | + // a fiber's subtree contains effects. Refactor the commit phase's | | 981 | + // depth-first traversal so that we can put work tag-specific logic | | 982 | + // before or after committing a subtree's effects. | | 983 | + const OnRenderFlag = Update; | | 984 | + const OnCommitFlag = Callback; | | 985 | + const OnPostCommitFlag = Passive; | | 986 | + const subtreeFlags = workInProgress.subtreeFlags; | | 987 | + const flags = workInProgress.flags; | | 988 | + let newFlags = flags; | | 989 | + | | 990 | + // Call onRender any time this fiber or its subtree are worked on, even | | 991 | + // if there are no effects | | 992 | + newFlags |= OnRenderFlag; | | 993 | + | | 994 | + // Call onCommit only if the subtree contains layout work, or if it | | 995 | + // contains deletions, since those might result in unmount work, which | | 996 | + // we include in the same measure. | | 997 | + // TODO: Can optimize by using a static flag to track whether a tree | | 998 | + // contains layout effects, like we do for passive effects. | | 999 | + if ( | | 1000 | + (flags & (LayoutMask | Deletion)) !== NoFlags || | | 1001 | + (subtreeFlags & (LayoutMask | Deletion)) !== NoFlags | | 1002 | + ) { | | 1003 | + newFlags |= OnCommitFlag; | | 1004 | + } | | 1005 | + | | 1006 | + // Call onPostCommit only if the subtree contains passive work. | | 1007 | + // Don't have to check for deletions, because Deletion is already | | 1008 | + // a passive flag. | | 1009 | + if ( | | 1010 | + (flags & PassiveMask) !== NoFlags || | | 1011 | + (subtreeFlags & PassiveMask) !== NoFlags | | 1012 | + ) { | | 1013 | + newFlags |= OnPostCommitFlag; | | 1014 | + } | | 1015 | + workInProgress.flags = newFlags; | | 1016 | + } else { | | 1017 | + // This fiber and its subtree bailed out, so don't fire any callbacks. | | 1018 | + } | | 1019 | + | | 1020 | + return null; | | 1021 | + } | 969 | 1022 | case SuspenseComponent: { | 970 | 1023 | popSuspenseContext(workInProgress); | 971 | 1024 | const nextState: null | SuspenseState = workInProgress.memoizedState; | |
|