Skip to content

fix(android): preallocate views between frames, not only on a frame - #60421

Open
duguyihou wants to merge 1 commit into
react:mainfrom
duguyihou:fix/android-preallocate-between-frames
Open

duguyihou wants to merge 1 commit into
react:mainfrom
duguyihou:fix/android-preallocate-between-frames

Conversation

@duguyihou

Copy link
Copy Markdown

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::maybePreallocateShadowNode calls FabricUIManager.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).
  • The requested drain runs drainPreallocateViewsQueue and then MountItemDispatcher.dispatchPreMountItemsBetweenFrames(), which executes pre-mount items within the budget of the last frame, the same deadline dispatchPreMountItems uses. Past it, items wait for the next frame callback.
  • The frame callback path is unchanged: it still drains and dispatches as before.

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:

MountItemDispatcherTest gains 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.

./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests 'com.facebook.react.fabric.mounting.MountItemDispatcherTest'
tests="7" skipped="0" failures="0" errors="0"

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-verification branch. I could not run the C++ or instrumentation tests of FabricMountingManager (the OSS Gradle build excludes them), and I did not test with enableMountingCoordinatorPullModelAndroid.

1. Plain Views, 0.89.0-rc.3. The reproducer from the issue (https://github.com/duguyihou/rn-android-preallocation-repro): a screen of 21 Views 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.

without with
Fast phases: mounts preceded by premountViews 0–1 of 13 13 of 13
Fast phases: CREATE instructions in the mount batch 21 0
Fast phases: IntBufferBatchMountItem::mountViews 2.3–3.5 ms 0.56–0.80 ms
Slow phase: mountViews 1.10–1.17 ms 0.45–0.49 ms
JS thread time before the mount, fast phases 7.4–8.2 ms 7.7–8.4 ms

2. 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.

A without A with p B without B with p
Mount batch, median 7.2 ms (6.7–8.1) 2.8 ms (2.7–3.0) < 0.0001 6.0 ms (5.7–6.3) 4.9 ms (2.9–5.5) < 0.0001
Frame callback that mounts the screen, median 8.6 ms (7.8–9.3) 6.9 ms (6.2–7.5) < 0.0001 7.2 ms (6.8–8.4) 7.0 ms (5.1–10.5) 0.22
Frames marked "App Deadline Missed" 5.5 of 26 (4–9) 3 of 26 (1–6) 0.0025 0 of ~111 (0–1) 0 of ~115 (0–1) 1.0

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.

  • The work is moved, not removed. In the plain-View run the UI thread spent about 4.5 ms in premountViews ahead 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.
  • How late the mount's frame callback starts after its expected vsync did not change in the image app (about 1 ms for A, 0.6 ms for B, with and without), so the between-frame work did not delay those frames.
  • The gain depends on creating the views taking less time than the render that queues them. With the image app left uncompiled (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).

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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 1, 2026
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 1, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android][Fabric] View preallocation only happens when a frame tick lands inside the JS render, so faster renders get none

1 participant