8000 Add eager alternate.stateNode cleanup by sammy-SC · Pull Request #33161 · facebook/react · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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

Comparing: 21fdf30...a721b37

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 +0.05% 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.11% 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 a721b37

@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
9E3F 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.

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.

4 participants
0