Skip to content

Commit

Permalink
Centralize props memoization (#13900)
Browse files Browse the repository at this point in the history
* Move memoizedProps to after beginWork remove memoizeProps helper

We always call this at the end. This is now enforced to line up since
we do the equality check in the beginning of beginWork. So we can't
have special cases.

* Inline the one caller of memoizeState
  • Loading branch information
sebmarkbage committed Oct 20, 2018
1 parent 0fc0446 commit 7268d97
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 35 deletions.
37 changes: 2 additions & 35 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ function updateForwardRef(
nextChildren,
renderExpirationTime,
);
memoizeProps(workInProgress, nextProps);
return workInProgress.child;
}

Expand Down Expand Up @@ -282,7 +281,6 @@ function updatePureComponent(
nextChildren,
renderExpirationTime,
);
memoizeProps(workInProgress, nextProps);
return workInProgress.child;
}

Expand All @@ -298,7 +296,6 @@ function updateFragment(
nextChildren,
renderExpirationTime,
);
memoizeProps(workInProgress, nextChildren);
return workInProgress.child;
}

Expand All @@ -314,7 +311,6 @@ function updateMode(
nextChildren,
renderExpirationTime,
);
memoizeProps(workInProgress, nextChildren);
return workInProgress.child;
}

Expand All @@ -334,7 +330,6 @@ function updateProfiler(
nextChildren,
renderExpirationTime,
);
memoizeProps(workInProgress, nextProps);
return workInProgress.child;
}

Expand Down Expand Up @@ -378,7 +373,6 @@ function updateFunctionComponent(
nextChildren,
renderExpirationTime,
);
memoizeProps(workInProgress, nextProps);
return workInProgress.child;
}

Expand Down Expand Up @@ -539,10 +533,9 @@ function finishClassComponent(
);
}

// Memoize props and state using the values we just used to render.
// Memoize state using the values we just used to render.
// TODO: Restructure so we never read values from the instance.
memoizeState(workInProgress, instance.state);
memoizeProps(workInProgress, instance.props);
workInProgress.memoizedState = instance.state;

// The context might have changed so we need to recalculate it.
if (hasContext) {
Expand Down Expand Up @@ -676,7 +669,6 @@ function updateHostComponent(current, workInProgress, renderExpirationTime) {
) {
// Schedule this fiber to re-render at offscreen priority. Then bailout.
workInProgress.expirationTime = Never;
workInProgress.memoizedProps = nextProps;
return null;
}

Expand All @@ -686,16 +678,13 @@ function updateHostComponent(current, workInProgress, renderExpirationTime) {
nextChildren,
renderExpirationTime,
);
memoizeProps(workInProgress, nextProps);
return workInProgress.child;
}

function updateHostText(current, workInProgress) {
if (current === null) {
tryToClaimNextHydratableInstance(workInProgress);
}
const nextProps = workInProgress.pendingProps;
memoizeProps(workInProgress, nextProps);
// Nothing to do here. This is terminal. We'll do the completion step
// immediately after.
return null;
Expand Down Expand Up @@ -805,7 +794,6 @@ function mountIndeterminateComponent(
);
}
}
workInProgress.memoizedProps = props;
return child;
}

Expand Down Expand Up @@ -954,7 +942,6 @@ function mountIndeterminateComponent(
}
}
reconcileChildren(null, workInProgress, value, renderExpirationTime);
memoizeProps(workInProgress, props);
return workInProgress.child;
}
}
Expand Down Expand Up @@ -1165,7 +1152,6 @@ function updateSuspenseComponent(
}
}

workInProgress.memoizedProps = nextProps;
workInProgress.memoizedState = nextState;
workInProgress.child = child;
return next;
Expand All @@ -1190,15 +1176,13 @@ function updatePortalComponent(
nextChildren,
renderExpirationTime,
);
memoizeProps(workInProgress, nextChildren);
} else {
reconcileChildren(
current,
workInProgress,
nextChildren,
renderExpirationTime,
);
memoizeProps(workInProgress, nextChildren);
}
return workInProgress.child;
}
Expand All @@ -1215,7 +1199,6 @@ function updateContextProvider(
const oldProps = workInProgress.memoizedProps;

const newValue = newProps.value;
workInProgress.memoizedProps = newProps;

if (__DEV__) {
const providerPropTypes = workInProgress.type.propTypes;
Expand Down Expand Up @@ -1327,7 +1310,6 @@ function updateContextConsumer(
// React DevTools reads this flag.
workInProgress.effectTag |= PerformedWork;
reconcileChildren(current, workInProgress, newChildren, renderExpirationTime);
workInProgress.memoizedProps = newProps;
return workInProgress.child;
}

Expand Down Expand Up @@ -1385,17 +1367,6 @@ function bailoutOnAlreadyFinishedWork(
}
}

// TODO: Delete memoizeProps/State and move to reconcile/bailout instead
function memoizeProps(workInProgress: Fiber, nextProps: any) {
workInProgress.memoizedProps = nextProps;
}

function memoizeState(workInProgress: Fiber, nextState: any) {
workInProgress.memoizedState = nextState;
// Don't reset the updateQueue, in case there are pending updates. Resetting
// is handled by processUpdateQueue.
}

function beginWork(
current: Fiber | null,
workInProgress: Fiber,
Expand Down Expand Up @@ -1517,7 +1488,6 @@ function beginWork(
resolveDefaultProps(Component, unresolvedProps),
renderExpirationTime,
);
workInProgress.memoizedProps = unresolvedProps;
return child;
}
case ClassComponent: {
Expand All @@ -1542,7 +1512,6 @@ function beginWork(
resolveDefaultProps(Component, unresolvedProps),
renderExpirationTime,
);
workInProgress.memoizedProps = unresolvedProps;
return child;
}
case HostRoot:
Expand Down Expand Up @@ -1584,7 +1553,6 @@ function beginWork(
resolveDefaultProps(Component, unresolvedProps),
renderExpirationTime,
);
workInProgress.memoizedProps = unresolvedProps;
return child;
}
case Fragment:
Expand Down Expand Up @@ -1628,7 +1596,6 @@ function beginWork(
updateExpirationTime,
renderExpirationTime,
);
workInProgress.memoizedProps = unresolvedProps;
return child;
}
default:
Expand Down
2 changes: 2 additions & 0 deletions packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1091,13 +1091,15 @@ function performUnitOfWork(workInProgress: Fiber): Fiber | null {
}

next = beginWork(current, workInProgress, nextRenderExpirationTime);
workInProgress.memoizedProps = workInProgress.pendingProps;

if (workInProgress.mode & ProfileMode) {
// Record the render duration assuming we didn't bailout (or error).
stopProfilerTimerIfRunningAndRecordDelta(workInProgress, true);
}
} else {
next = beginWork(current, workInProgress, nextRenderExpirationTime);
workInProgress.memoizedProps = workInProgress.pendingProps;
}

if (__DEV__) {
Expand Down

0 comments on commit 7268d97

Please sign in to comment.