Repository navigation
Conversation
The preallocation queue was drained only from the UI thread's frame callback. A render that commits before the next frame callback never met a drain, so none of its views were preallocated and all of them were created inside the mount batch. A slower render of the same tree spanned a frame and got most of its views created ahead of time. The first view queued now asks the UI thread to drain the queue and run the pre-mount items straight away. One request is outstanding at a time, so a render makes one JNI call per drain, not one per view. Pre-mount items run between frames only within the budget of the last frame, as they do in a frame callback. Past it they wait for the next frame callback, which still drains and dispatches as before. Not behind a feature flag: the frame callback path is unchanged and this only adds an earlier drain.
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:
Fixes #60415.
The preallocation queue is drained in one place, the UI thread's frame callback (
DispatchUIFrameCallback.doFrameGuarded→drainPreallocateViewsQueue). A render that commits before the next frame callback never meets a drain, so none of its views are preallocated and all of them are created inside the mount batch. A slower render of the same tree spans a frame and gets most of its views created ahead of time. So a faster render gets a slower mount.With this change, the first view queued asks the UI thread to drain the queue and run the pre-mount items straight away:
FabricMountingManager::maybePreallocateShadowNodecallsFabricUIManager.requestPreallocateViewsDrain()when no drain is already requested. An atomic flag keeps it to one JNI call per drain, not one per view; the queue was introduced to keep those calls off the JS thread (add optimised path for view preallocation on android behind feature flag #45587).drainPreallocateViewsQueueand thenMountItemDispatcher.dispatchPreMountItemsBetweenFrames(), which executes pre-mount items within the budget of the last frame, the same deadlinedispatchPreMountItemsuses. Past it, items wait for the next frame callback.It is not behind a feature flag, since it only adds an earlier drain. I'm happy to add one if you prefer to roll it out that way.
Changelog:
[ANDROID] [FIXED] - Preallocate views for renders that commit within one frame, instead of creating them all in the mount batch
Test Plan:
MountItemDispatcherTestgains two cases: a pre-mount item is executed between frames within the last frame's budget, and is left for the next frame callback once the budget has passed.Everything below was measured on a Galaxy S25 (Android 17, 120 Hz) with release builds and Perfetto, with React Native built from source with and without this change. The patch as applied to 0.89.0-rc.3, the from-source build setup and the trace scripts are on the reproducer's
fix-verificationbranch. I could not run the C++ or instrumentation tests ofFabricMountingManager(the OSS Gradle build excludes them), and I did not test withenableMountingCoordinatorPullModelAndroid.1. Plain
Views, 0.89.0-rc.3. The reproducer from the issue (https://github.com/duguyihou/rn-android-preallocation-repro): a screen of 21Views mounted 39 times, 13 with a fast render, 13 with 0.3 ms of busy work per cell, 13 fast again. Three launches of each build, alternating; medians over the 13 mounts of a phase.premountViewsCREATEinstructions in the mount batchIntBufferBatchMountItem::mountViewsmountViews2. Image components, 0.88.1. An app that mounts and unmounts a screen of 21 images 13 times, with two image libraries (A renders in about 7.5 ms, B in about 12.5 ms). The app's baseline profile was compiled (
speed-profile), as on a Play Store install. Ten launches of each build per library, alternating; each value is one launch's median, and p is an exact two-sided rank-sum test.For library A, whose render fits in one frame, the mount's frame callback goes from over the 8.3 ms frame to under it. Library B's render already spanned a frame, so it was already being preallocated; its mount batch gets shorter and its frames are unchanged.
What it costs, and where it does not help.
Viewrun the UI thread spent about 4.5 ms inpremountViewsahead of a mount, against about 3 ms creating the same views inside the batch. In one trace of each build with CPU frequency recorded, pre-mount work between frames ran at a median 0.56 GHz and pre-mount work inside a frame callback at 1.36 GHz; that app is idle between mounts.adb install, everything interpreted), creating library A's 21 views took about 9.5 ms against a 7.5 ms render: its mount batch still halved (10.5–10.8 ms to 4.8–5.5 ms), but its frame callback stayed around 10 ms and its missed frames did not change (6–10 of 26 without, 6–8 with, three launches each).