-
Notifications
You must be signed in to change notification settings - Fork 48.8k
Add eager alternate.stateNode cleanup #33161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add eager alternate.stateNode cleanup #33161
Conversation
Comparing: 21fdf30...2f38d6a Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: (No significant changes) |
@@ -2171,6 +2172,11 @@ function commitMutationEffectsOnFiber( | |||
} | |||
} | |||
} | |||
if (enableEagerAlternateStateNodeCleanup) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in the else branch of if (supportsMutation)
and it should check if (supportsPersistence)
since this should only apply to the persistent mode which is the only mode that clones the state nodes.
The flag is only on in RN but it's still on for Paper unnecessarily. You're better off turning the flag on everywhere else because that flushes out any issues where it would have issues if it turned on in the other renders. Like not gating it properly on supportsPersistence
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me do that. My thinking was that it is a no-op in supportsMutation
mode as stateNode will be the same between finishedWork and alternate.
thanks for pointing out the supportsPersistence
. I will also add a comment explaining why this is needed.
// prevents shadow node from staying in memory longer than it | ||
// needs to. The correct behaviour of this is checked by test in | ||
// React Native: ShadowNodeReferenceCounter-itest.js#L150 | ||
finishedWork.alternate.stateNode = finishedWork.stateNode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe @sebmarkbage or @rickhanlonii…
What are the correctness trade offs between setting this to finishedWork.stateNode
versus null
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setting it to null seems wrong because this is a shared value between the current and alternate fibers, and I don't think we do that for DOM, but maybe @sebmarkbage will say I'm wrong and it's fine. What's the benefit? It doesn't seem like it would allow releasing any additional memory.
packages/shared/ReactFeatureFlags.js
Outdated
@@ -141,6 +141,8 @@ export const enablePersistedModeClonedFlag = false; | |||
|
|||
export const enableShallowPropDiffing = false; | |||
|
|||
export const enableEagerAlternateStateNodeCleanup = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be true here, as well as the other non-RN files. Since this only impacts RN, we don't need to set it to false. Makes it easier to roll out - and if it causes a test to fail somehow (it shouldn't) we'll catch it now instead of when trying to remove the flag
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a couple nits
} else { | ||
if (enableEagerAlternateStateNodeCleanup) { | ||
if (supportsPersistence) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what you have here is fine, but I think what @rickhanlonii meant was this.
} else { | |
if (enableEagerAlternateStateNodeCleanup) { | |
if (supportsPersistence) { | |
} else if (supportsPersistence) { | |
if (enableEagerAlternateStateNodeCleanup) { |
This is a fix for a problem where React retains shadow nodes longer than it needs to. The behaviour is shown in React Native test: https://github.com/facebook/react-native/blob/main/packages/react-native/src/private/__tests__/utilities/__tests__/ShadowNodeReferenceCounter-itest.js#L169 # Problem When React commits a new shadow tree, old shadow nodes are stored inside `fiber.alternate.stateNode`. This is not cleared up until React clones the node again. This may be problematic if mutation deletes a subtree, in that case `fiber.alternate.stateNode` will retain entire subtree until next update. In case of image nodes, this means retaining entire images. So when React goes from revision A: `<View><View /></View>` to revision B: `<View />`, `fiber.alternate.stateNode` will be pointing to Shadow Node that represents revision A..  # Fix To fix this, this PR adds a new feature flag `enableEagerAlternateStateNodeCleanup`. When enabled, `alternate.stateNode` is proactively pointed towards finishedWork's stateNode, releasing resources sooner. I have verified this fixes the issue [demonstrated by React Native tests](https://github.com/facebook/react-native/blob/main/packages/react-native/src/private/__tests__/utilities/__tests__/ShadowNodeReferenceCounter-itest.js#L169). All existing React tests pass when the flag is enabled. DiffTrain build for [5d04d73](5d04d73)
This is a fix for a problem where React retains shadow nodes longer than it needs to. The behaviour is shown in React Native test: https://github.com/facebook/react-native/blob/main/packages/react-native/src/private/__tests__/utilities/__tests__/ShadowNodeReferenceCounter-itest.js#L169 # Problem When React commits a new shadow tree, old shadow nodes are stored inside `fiber.alternate.stateNode`. This is not cleared up until React clones the node again. This may be problematic if mutation deletes a subtree, in that case `fiber.alternate.stateNode` will retain entire subtree until next update. In case of image nodes, this means retaining entire images. So when React goes from revision A: `<View><View /></View>` to revision B: `<View />`, `fiber.alternate.stateNode` will be pointing to Shadow Node that represents revision A..  # Fix To fix this, this PR adds a new feature flag `enableEagerAlternateStateNodeCleanup`. When enabled, `alternate.stateNode` is proactively pointed towards finishedWork's stateNode, releasing resources sooner. I have verified this fixes the issue [demonstrated by React Native tests](https://github.com/facebook/react-native/blob/main/packages/react-native/src/private/__tests__/utilities/__tests__/ShadowNodeReferenceCounter-itest.js#L169). All existing React tests pass when the flag is enabled. DiffTrain build for [5d04d73](facebook@5d04d73)
Summary: Enables the `enableEagerAlternateStateNodeCleanup` feature flag in the open source React Native renderers that are currently targeting React 19.1, by manually patching them in the React Native repository. This feature flag has been found to significantly improve memory management of parent alternate fibers in persistent modes (i.e. Fabric), and we want this to be available to open source users of React Native before the next scheduled public version release of React. For more details about the fix, see: facebook/react#33161 Changelog: [General][Changed] - Reduces memory usage by improving memory management of parent alternate fibers. Differential Revision: D76073900
…#51856) Summary: Pull Request resolved: #51856 Enables the `enableEagerAlternateStateNodeCleanup` feature flag in the open source React Native renderers that are currently targeting React 19.1, by manually patching them in the React Native repository. This feature flag has been found to significantly improve memory management of parent alternate fibers in persistent modes (i.e. Fabric), and we want this to be available to open source users of React Native before the next scheduled public version release of React. For more details about the fix, see: facebook/react#33161 Changelog: [General][Changed] - Reduces memory usage, by improving memory management of parent alternate fibers. (Previously, a parent fiber might retain memory associated with shadow nodes from a previous commit.) Reviewed By: rickhanlonii Differential Revision: D76073900 fbshipit-source-id: 6779ea0862d4a1e25354b12ef3d1363dc12d26cc
This is a fix for a problem where React retains shadow nodes longer than it needs to. The behaviour is shown in React Native test: https://github.com/facebook/react-native/blob/main/packages/react-native/src/private/__tests__/utilities/__tests__/ShadowNodeReferenceCounter-itest.js#L169
Problem
When React commits a new shadow tree, old shadow nodes are stored inside
fiber.alternate.stateNode
. This is not cleared up until React clones the node again. This may be problematic if mutation deletes a subtree, in that casefiber.alternate.stateNode
will retain entire subtree until next update. In case of image nodes, this means retaining entire images.So when React goes from revision A:
<View><View /></View>
to revision B:<View />
,fiber.alternate.stateNode
will be pointing to Shadow Node that represents revision A..Fix
To fix this, this PR adds a new feature flag
enableEagerAlternateStateNodeCleanup
. When enabled,alternate.stateNode
is proactively pointed towards finishedWork's stateNode, releasing resources sooner.I have verified this fixes the issue demonstrated by React Native tests.
All existing React tests pass when the flag is enabled.