Skip to content

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

Merged
merged 5 commits into from
May 12, 2025

Conversation

sammy-SC
Copy link
Contributor

@sammy-SC sammy-SC commented May 9, 2025

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..

image

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.

@react-sizebot
Copy link

react-sizebot commented May 9, 2025

Comparing: 21fdf30...2f38d6a

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.68 kB 6.68 kB = 1.83 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 528.09 kB 528.09 kB = 93.17 kB 93.17 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB +0.05% 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 646.40 kB 646.40 kB = 113.71 kB 113.71 kB
facebook-www/ReactDOM-prod.classic.js = 674.15 kB 674.15 kB = 118.41 kB 118.41 kB
facebook-www/ReactDOM-prod.modern.js = 664.43 kB 664.43 kB = 116.81 kB 116.81 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by 🚫 dangerJS against 2f38d6a

@sammy-SC sammy-SC self-assigned this May 9, 2025
@sammy-SC sammy-SC requested a review from kassens May 9, 2025 15:48
@@ -2171,6 +2172,11 @@ function commitMutationEffectsOnFiber(
}
}
}
if (enableEagerAlternateStateNodeCleanup) {
Copy link
Collaborator

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.

Copy link
Contributor Author

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;
Copy link
Contributor

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?

Copy link
Member

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.

@@ -141,6 +141,8 @@ export const enablePersistedModeClonedFlag = false;

export const enableShallowPropDiffing = false;

export const enableEagerAlternateStateNodeCleanup = false;
Copy link
Member

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

Copy link
Member

@rickhanlonii rickhanlonii left a 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

Comment on lines +2174 to +2176
} else {
if (enableEagerAlternateStateNodeCleanup) {
if (supportsPersistence) {
Copy link
Contributor

@yungsters yungsters May 12, 2025

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.

Suggested change
} else {
if (enableEagerAlternateStateNodeCleanup) {
if (supportsPersistence) {
} else if (supportsPersistence) {
if (enableEagerAlternateStateNodeCleanup) {

@sammy-SC sammy-SC merged commit 5d04d73 into facebook:main May 12, 2025
239 checks passed
@sammy-SC sammy-SC deleted the sammy-sc/add-eager-stateNode-cleanup branch May 12, 2025 16:39
github-actions bot pushed a commit that referenced this pull request May 12, 2025
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..

![image](https://github.com/user-attachments/assets/076b677e-d152-4763-8c9d-4f923212b424)

# 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)
github-actions bot pushed a commit to code/lib-react that referenced this pull request May 12, 2025
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..

![image](https://github.com/user-attachments/assets/076b677e-d152-4763-8c9d-4f923212b424)

# 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)
yungsters added a commit to yungsters/react-native that referenced this pull request Jun 5, 2025
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
facebook-github-bot pushed a commit to facebook/react-native that referenced this pull request Jun 5, 2025
…#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants