Skip to content

Commit 54b5b32

Browse files
committed
Move Update flag check into each switch case
The fiber tag is more specific than the effect flag, so we should always refine the type of work first, to minimize redundant checks. In the next step I'll move all other other flag checks in this function into the same switch statement.
1 parent e66e7a0 commit 54b5b32

File tree

2 files changed

+72
-72
lines changed

2 files changed

+72
-72
lines changed

packages/react-reconciler/src/ReactFiberCommitWork.new.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,13 +2065,14 @@ function commitMutationEffectsOnFiber(
20652065
finishedWork.flags &= ~Hydrating;
20662066
}
20672067

2068+
// All logic in these branches should be wrapped in a flag check.
20682069
// TODO: Move the ad-hoc flag checks above into the main switch statement.
2069-
if (flags & Update) {
2070-
switch (finishedWork.tag) {
2071-
case FunctionComponent:
2072-
case ForwardRef:
2073-
case MemoComponent:
2074-
case SimpleMemoComponent: {
2070+
switch (finishedWork.tag) {
2071+
case FunctionComponent:
2072+
case ForwardRef:
2073+
case MemoComponent:
2074+
case SimpleMemoComponent: {
2075+
if (flags & Update) {
20752076
commitHookEffectListUnmount(
20762077
HookInsertion | HookHasEffect,
20772078
finishedWork,
@@ -2105,12 +2106,11 @@ function commitMutationEffectsOnFiber(
21052106
finishedWork.return,
21062107
);
21072108
}
2108-
return;
2109-
}
2110-
case ClassComponent: {
2111-
return;
21122109
}
2113-
case HostComponent: {
2110+
return;
2111+
}
2112+
case HostComponent: {
2113+
if (flags & Update) {
21142114
if (supportsMutation) {
21152115
const instance: Instance = finishedWork.stateNode;
21162116
if (instance != null) {
@@ -2137,9 +2137,11 @@ function commitMutationEffectsOnFiber(
21372137
}
21382138
}
21392139
}
2140-
return;
21412140
}
2142-
case HostText: {
2141+
return;
2142+
}
2143+
case HostText: {
2144+
if (flags & Update) {
21432145
if (supportsMutation) {
21442146
if (finishedWork.stateNode === null) {
21452147
throw new Error(
@@ -2157,9 +2159,11 @@ function commitMutationEffectsOnFiber(
21572159
current !== null ? current.memoizedProps : newText;
21582160
commitTextUpdate(textInstance, oldText, newText);
21592161
}
2160-
return;
21612162
}
2162-
case HostRoot: {
2163+
return;
2164+
}
2165+
case HostRoot: {
2166+
if (flags & Update) {
21632167
if (supportsMutation && supportsHydration) {
21642168
if (current !== null) {
21652169
const prevRootState: RootState = current.memoizedState;
@@ -2173,45 +2177,41 @@ function commitMutationEffectsOnFiber(
21732177
const pendingChildren = root.pendingChildren;
21742178
replaceContainerChildren(containerInfo, pendingChildren);
21752179
}
2176-
return;
21772180
}
2178-
case HostPortal: {
2181+
return;
2182+
}
2183+
case HostPortal: {
2184+
if (flags & Update) {
21792185
if (supportsPersistence) {
21802186
const portal = finishedWork.stateNode;
21812187
const containerInfo = portal.containerInfo;
21822188
const pendingChildren = portal.pendingChildren;
21832189
replaceContainerChildren(containerInfo, pendingChildren);
21842190
}
2185-
return;
2186-
}
2187-
case Profiler: {
2188-
return;
21892191
}
2190-
case SuspenseComponent: {
2192+
return;
2193+
}
2194+
case SuspenseComponent: {
2195+
if (flags & Update) {
21912196
commitSuspenseCallback(finishedWork);
21922197
attachSuspenseRetryListeners(finishedWork);
2193-
return;
21942198
}
2195-
case SuspenseListComponent: {
2199+
return;
2200+
}
2201+
case SuspenseListComponent: {
2202+
if (flags & Update) {
21962203
attachSuspenseRetryListeners(finishedWork);
2197-
return;
2198-
}
2199-
case IncompleteClassComponent: {
2200-
return;
22012204
}
2202-
case ScopeComponent: {
2205+
return;
2206+
}
2207+
case ScopeComponent: {
2208+
if (flags & Update) {
22032209
if (enableScopeAPI) {
22042210
const scopeInstance = finishedWork.stateNode;
22052211
prepareScopeUpdate(scopeInstance, finishedWork);
22062212
}
2207-
return;
2208-
}
2209-
default: {
2210-
throw new Error(
2211-
'This unit of work tag should not have side-effects. This error is ' +
2212-
'likely caused by a bug in React. Please file an issue.',
2213-
);
22142213
}
2214+
return;
22152215
}
22162216
}
22172217
}

packages/react-reconciler/src/ReactFiberCommitWork.old.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,13 +2065,14 @@ function commitMutationEffectsOnFiber(
20652065
finishedWork.flags &= ~Hydrating;
20662066
}
20672067

2068+
// All logic in these branches should be wrapped in a flag check.
20682069
// TODO: Move the ad-hoc flag checks above into the main switch statement.
2069-
if (flags & Update) {
2070-
switch (finishedWork.tag) {
2071-
case FunctionComponent:
2072-
case ForwardRef:
2073-
case MemoComponent:
2074-
case SimpleMemoComponent: {
2070+
switch (finishedWork.tag) {
2071+
case FunctionComponent:
2072+
case ForwardRef:
2073+
case MemoComponent:
2074+
case SimpleMemoComponent: {
2075+
if (flags & Update) {
20752076
commitHookEffectListUnmount(
20762077
HookInsertion | HookHasEffect,
20772078
finishedWork,
@@ -2105,12 +2106,11 @@ function commitMutationEffectsOnFiber(
21052106
finishedWork.return,
21062107
);
21072108
}
2108-
return;
2109-
}
2110-
case ClassComponent: {
2111-
return;
21122109
}
2113-
case HostComponent: {
2110+
return;
2111+
}
2112+
case HostComponent: {
2113+
if (flags & Update) {
21142114
if (supportsMutation) {
21152115
const instance: Instance = finishedWork.stateNode;
21162116
if (instance != null) {
@@ -2137,9 +2137,11 @@ function commitMutationEffectsOnFiber(
21372137
}
21382138
}
21392139
}
2140-
return;
21412140
}
2142-
case HostText: {
2141+
return;
2142+
}
2143+
case HostText: {
2144+
if (flags & Update) {
21432145
if (supportsMutation) {
21442146
if (finishedWork.stateNode === null) {
21452147
throw new Error(
@@ -2157,9 +2159,11 @@ function commitMutationEffectsOnFiber(
21572159
current !== null ? current.memoizedProps : newText;
21582160
commitTextUpdate(textInstance, oldText, newText);
21592161
}
2160-
return;
21612162
}
2162-
case HostRoot: {
2163+
return;
2164+
}
2165+
case HostRoot: {
2166+
if (flags & Update) {
21632167
if (supportsMutation && supportsHydration) {
21642168
if (current !== null) {
21652169
const prevRootState: RootState = current.memoizedState;
@@ -2173,45 +2177,41 @@ function commitMutationEffectsOnFiber(
21732177
const pendingChildren = root.pendingChildren;
21742178
replaceContainerChildren(containerInfo, pendingChildren);
21752179
}
2176-
return;
21772180
}
2178-
case HostPortal: {
2181+
return;
2182+
}
2183+
case HostPortal: {
2184+
if (flags & Update) {
21792185
if (supportsPersistence) {
21802186
const portal = finishedWork.stateNode;
21812187
const containerInfo = portal.containerInfo;
21822188
const pendingChildren = portal.pendingChildren;
21832189
replaceContainerChildren(containerInfo, pendingChildren);
21842190
}
2185-
return;
2186-
}
2187-
case Profiler: {
2188-
return;
21892191
}
2190-
case SuspenseComponent: {
2192+
return;
2193+
}
2194+
case SuspenseComponent: {
2195+
if (flags & Update) {
21912196
commitSuspenseCallback(finishedWork);
21922197
attachSuspenseRetryListeners(finishedWork);
2193-
return;
21942198
}
2195-
case SuspenseListComponent: {
2199+
return;
2200+
}
2201+
case SuspenseListComponent: {
2202+
if (flags & Update) {
21962203
attachSuspenseRetryListeners(finishedWork);
2197-
return;
2198-
}
2199-
case IncompleteClassComponent: {
2200-
return;
22012204
}
2202-
case ScopeComponent: {
2205+
return;
2206+
}
2207+
case ScopeComponent: {
2208+
if (flags & Update) {
22032209
if (enableScopeAPI) {
22042210
const scopeInstance = finishedWork.stateNode;
22052211
prepareScopeUpdate(scopeInstance, finishedWork);
22062212
}
2207-
return;
2208-
}
2209-
default: {
2210-
throw new Error(
2211-
'This unit of work tag should not have side-effects. This error is ' +
2212-
'likely caused by a bug in React. Please file an issue.',
2213-
);
22142213
}
2214+
return;
22152215
}
22162216
}
22172217
}

0 commit comments

Comments
 (0)