Repository navigation
Avoid per-child dynamic_casts during Yoga layout - #59643
Open
hryhoriiK97 wants to merge 1 commit into
Open
hryhoriiK97 wants to merge 1 commit into
hryhoriiK97 wants to merge 1 commit into
Conversation
`resolveErrata` and `getContentBounds` check whether a child is a `LayoutConformanceShadowNode` or a `ViewShadowNode` with a `dynamic_cast`, which fails for almost every node. Both classes are final, so compare the component handle instead. `shadowNodeFromContext` casts the Yoga node's context, which only this class's constructors set (to `this`), so a `static_cast` recovers it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
javache
approved these changes
Oct 29, 2026
|
@javache has imported this pull request. If you are a Meta employee, you can view this in D128421875. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary:
YogaLayoutableShadowNoderuns threedynamic_casts for children on every layout pass:resolveErratachecksdynamic_cast<const LayoutConformanceShadowNode *>(this).configureYogaTreecalls it for every child to decide whether the child has to be configured again, so it runs on every layout, and it fails for almost every node, which is the slowest case of adynamic_cast.getContentBoundschecksdynamic_cast<const ViewShadowNode *>for every child to read itshitSlop. It fails for every child that isn't aView(Text, ScrollView, Image, …).shadowNodeFromContextgets the shadow node back from a Yoga node's context with adynamic_cast. It runs for every child inlayout()andgetContentBounds(), and in Yoga's measure, baseline and clone callbacks.Current behavior. In Time Profiler traces of JS-driven updates on an iPhone,
__dynamic_casttook 11–18% of the JS thread, most of it insidelayoutTree, and the type comparisons in those traces go throughstrcmp.With this change.
LayoutConformanceShadowNodeandViewShadowNodeare bothfinal, and their nodes are only created by descriptors that report the class's ownHandle(), so comparinggetComponentHandle()with that handle gives the same answer as thedynamic_cast; astatic_castfollows. The Yoga node's context is only set by this class's two constructors, tothis, soshadowNodeFromContextrecovers it with astatic_castof the stored pointer. The behavior doesn't change.Changelog:
[GENERAL] [CHANGED] - Yoga layout no longer uses
dynamic_castfor every child to check its typeTest Plan:
clang-formatpasses on the changed file, and it compiles for iOS (arm64).yarn fantom(full suite, tester rebuilt with cmake from this change, then frommain): no test fails with this change that doesn't also fail onmainin the same local environment. The local failures (LogBox andexpectsnapshot formatting,Text-itest,EventTimingAPI-itest) happen with and without this change.xcodebuildin Release and run on a physical iPhone 15 Pro (iOS 26.6.1). Both arms are the same binary, with this change switched by an environment variable read once. 8 launches in the order before, after, after, before, after, before, before, after; in each, 12 s of Time Profiler per scenario, counting JS-thread samples. Interactions were driven by Argent on the physical iPhone. The phone warmed up during the runs, so the totals of the same arm vary by up to 30%; the time indynamic_castis the most direct number, and the medians are given for context.RNTester Playground, 30
Texts animated withAnimated(JS driver), medians of 4 launches per arm:dynamic_castinside layout, per 12 sdynamic_cast, share of the JS threadlayoutTree), per 12 sdynamic_castinside layout, per 12 sdynamic_cast, share of the JS threadlayoutTree), per 12 sThe
dynamic_casttime left with this change (about 3–5% of the JS thread) comes from other places, such asadoptYogaChildand the shadow node constructors, which this PR doesn't change.On Android these casts cost much less, since there the type comparison compares pointers: in the same color-animation scenario on an Android emulator,
dynamic_castinside layout went from 1.5% to 0.2% of the JS thread.🤖 Generated with Claude Code