Add eager alternate.stateNode cleanup #33161
Open
+16
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.