Skip to content
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

Cloned flag to avoid extra clones in persistent renderer #27647

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

Conversation

kassens
Copy link
Member

@kassens kassens commented Nov 3, 2023

Persistent renderers used the Update effect flag to check if a subtree needs to be cloned. In some cases, that causes extra renders, such as when a layout effect is triggered which only has an effect on the JS side, but doesn't update the host components.

It's been a bit tricky to find the right places where this needs to be set and I'm not 100% sure I got all the cases even though the tests passed.

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Nov 3, 2023
// then we only have to check the `completedWork.subtreeFlags`.
let child = completedWork.child;
while (child !== null) {
if (
(child.flags & MutationMask) !== NoFlags ||
(child.subtreeFlags & MutationMask) !== NoFlags
(child.flags & (Cloned | Visibility | Placement)) !== NoFlags ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the function where I would put the roll out flag

/* Skipped value: 0b0000000000000000000000001000; */
export const Update = /* */ 0b00000000000000000000000000100;
export const Cloned = /* */ 0b10000000000000000000000000000;
/* Skipped value: 0b00000000000000000000000001000; */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just use the skipped value? Can't think of any reason why not. I'm guessing this is the old Deletion flag that was replaced by ChildDeletion and then removed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was introduced in d69b2cf reverting some changes to the Flags. Some versioning issues with DevTools that I don't fully understand. Maybe it's okay when the devtools have since auto-updated to a newer version?

cc @hoxyq in case you know if this would be safe?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

react-devtools-shared/src/backend/ReactFiberFlags.js was removed in https://github.com/facebook/react/pull/26542/files, so we could mirror the whole function implementation, instead of just mirroring flags.

We should update these flags for DevTools accordingly, like here, for example: https://github.com/facebook/react/blame/main/packages/react-devtools-shared/src/backend/renderer.js#L1515.

I think it should be safe to use skipped value here, because DevTools were not depending on this flag at any time, AFAIK. I can help with testing these changes if you end up with using skipped value.

@react-sizebot
Copy link

react-sizebot commented Mar 6, 2024

Comparing: 8416ebe...c73400f

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 = 497.93 kB 497.93 kB = 89.26 kB 89.26 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 = 502.75 kB 502.75 kB = 89.96 kB 89.96 kB
facebook-www/ReactDOM-prod.classic.js = 597.10 kB 597.10 kB = 105.31 kB 105.31 kB
facebook-www/ReactDOM-prod.modern.js = 571.44 kB 571.44 kB = 101.24 kB 101.24 kB
test_utils/ReactAllWarnings.js Deleted 62.88 kB 0.00 kB Deleted 15.69 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
react-native/implementations/ReactFabric-prod.fb.js +0.20% 365.56 kB 366.30 kB +0.15% 64.23 kB 64.32 kB
test_utils/ReactAllWarnings.js Deleted 62.88 kB 0.00 kB Deleted 15.69 kB 0.00 kB

Generated by 🚫 dangerJS against c73400f

@kassens kassens force-pushed the persistent-update3 branch 2 times, most recently from 830f58f to 6c2c437 Compare March 20, 2024 20:56
Copy link

vercel bot commented Jun 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 26, 2024 8:16pm
@kassens kassens requested a review from acdlite June 27, 2024 14:05
@kassens kassens changed the title Add Cloned flag to avoid extra clones in persistent renderer Jul 1, 2024
Copy link
Collaborator

@gnoff gnoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely out of my comfort zone so I can't approve but left some comments. I think there is a bug that might be causing cloned tags to be assigned more than they should (but maybe I misunderstand the mechanic)

@@ -473,6 +489,8 @@ function updateHostComponent(
// Note that this might release a previous clone.
workInProgress.stateNode = currentInstance;
return;
} else {
markCloned(workInProgress);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this right? seems like we should only mark clone if requiresClone is true

Comment on lines +648 to +652
if (!enablePersistedModeClonedFlag) {
// We'll have to mark it as having an effect, even though we won't use the effect for anything.
// This lets the parents know that at least one of their children has changed.
markUpdate(workInProgress);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be right but it's a little confusing that markCloned and markUpdate are not apparently mutually exclusive (when they actually are given the implementation of markCloned)

if (parentFiber.subtreeFlags & MutationMask) {
if (
parentFiber.subtreeFlags &
(enablePersistedModeClonedFlag ? MutationMask | Cloned : MutationMask)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should Cloned just be part of the MutationMask?

@@ -199,9 +211,12 @@ function doesRequireClone(current: null | Fiber, completedWork: Fiber) {
// then we only have to check the `completedWork.subtreeFlags`.
let child = completedWork.child;
while (child !== null) {
const checkedFlags = enablePersistedModeClonedFlag
? Cloned | Visibility | Placement
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this answers my question about why Cloned isn't just part of MutationMask. you specifically want to track it separately from general mutations like Update. You could still make it part of the Mask and just not use MutationMask here (as you are doing now). I guess the question is does Cloned generally count as a Mutation you need to visit during commit (yes) and if so then it semantically probably does belong in the mask.

Copy link
Contributor

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have similar questions to what @gnoff pointed out, but overall this makes sense. Have you tried syncing internally and running RN tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
7 participants