Skip to content

Commit f33a1cd

Browse files
Android: Schedule image prefetching on tree commit (#53555)
Summary: Pull Request resolved: #53555 Changelog: [Internal] ## TLDR; We run the `ReactVitoImageManager.kt` on the Java Message Queue Thread (`mqt`) > Maybe running it on the `UiThread` (as Android view creation) solves the QE reegressions ## Issue > Your experiment [qe:enable_image_prefetching_android_v4] is significantly moving important metric(s) T235749297 > e.g negatively impact `sp_core` (Scroll Performance Core) https://fburl.com/deltoid3/ef2fd92e {F1981479985} ## Observation After adding Perfetto traces in D80717558 and building a `automation_fbandroid_art_arm64_for_perftest_profileable` build > I see 'larger amounts' of `experimental_prefetchResource` on the JavaScript Message Queue Thread {F1981479808} We do run this entire logic on the JavaScript Message Queue Thread https://www.internalfb.com/code/fbsource/[368503303835439955d87d79439a3d19d979cd40]/fbandroid/java/com/facebook/fresco/vito/rn/ReactVitoImageManager.kt?lines=253-260 However when normally `mounting` Shadow Nodes in RN Android we jump from the JavaScript Message Queue Thread to the Android UI Thread https://www.internalfb.com/code/fbsource/[68603b276cb9de1ae2ecb83ec4a789ae3db3b051]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricUIManagerBinding.cpp?lines=626%2C638 -> https://www.internalfb.com/code/fbsource/[68603b276cb9de1ae2ecb83ec4a789ae3db3b051]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.cpp?lines=690%2C701%2C872-883 -> https://www.internalfb.com/code/fbsource/[68603b276cb9de1ae2ecb83ec4a789ae3db3b051]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java?lines=897%2C943 -> https://www.internalfb.com/code/fbsource/[68603b276cb9de1ae2ecb83ec4a789ae3db3b051]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java?lines=938-946 ## Idea Run `imagePrefetcher?.prefetchResource` also on the UI thread ## Resources ### :: GDoc - Image Prefetching for Android https://docs.google.com/document/d/1Yc5G5vuollx0I4tdXpE8Hgwn2DuIhJKwOTHak3g4Gu8/edit?fbclid=IwY2xjawLjgcBleHRuA2FlbQIxMQBicmlkETFra3N5WHg3OGV6UndYUmVTAR5KiXIDgrH2FW4HEBdezFBr2NqX4KPT6FzYQXD1sBRjEfq8d_x0JwQfeL_TXg_aem_ZqWb9dAJ59pHFfoHsrzwbw&pli=1&tab=t.0#heading=h.udv4z3lhwhf7 - React Field of View https://docs.google.com/document/d/1gHLF3oAv9JhKKcztM56iZZUPPWp0mBbjqDlosBkL1kE/edit?tab=t.0#heading=h.36p5puf8ufz7 ### :: Fb4A (Facebook for Android) The debug package name for fb4a `com.facebook.katana` is typically `com.facebook.wakizashi` ### :: Links - How to Perfetto profile fb4a https://www.internalfb.com/wiki/Luna_Wei/Building_a_fb4a_Profile_Build/ - Building Catalyst Profile Build https://www.internalfb.com/wiki/Luna_Wei/Building_Catalyst_Profile_Build/ - Marketplace QE Regression Guide https://www.internalfb.com/intern/staticdocs/marketplace/performance/my-experiment-is-regressing-perf/ - Install for Profileable build https://www.internalfb.com/wiki/Metatrace/Metatrace-install_for_Profileable_build/ - Metatrace https://www.internalfb.com/wiki/Metatrace/ ### Android Java Debug https://www.internalfb.com/wiki/Platfrom_Health_Learnings/Onboarding_Material_or_New-hired_Engineers/How_to_Debug_FB4A_0/ ``` arc focus clean --invalidate-caches-only arc focus --targets <YOUR_TARGET> --open ``` in this case ``` arc focus --targets fb4a --open ``` It creates a `monoproject` now {F1981501090} Reviewed By: javache Differential Revision: D80950423 fbshipit-source-id: 5f1c4c096adab218a2d765d262901521bab2e6b3
1 parent d6ed32f commit f33a1cd

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.facebook.react.fabric.mounting.mountitems.DispatchCommandMountItem;
6666
import com.facebook.react.fabric.mounting.mountitems.MountItem;
6767
import com.facebook.react.fabric.mounting.mountitems.MountItemFactory;
68+
import com.facebook.react.fabric.mounting.mountitems.PrefetchResourcesMountItem;
6869
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags;
6970
import com.facebook.react.internal.featureflags.ReactNativeNewArchitectureFeatureFlags;
7071
import com.facebook.react.internal.interop.InteropEventEmitter;
@@ -985,8 +986,13 @@ public void runGuarded() {
985986
*/
986987
@UnstableReactNativeAPI
987988
public void experimental_prefetchResources(String componentName, ReadableMapBuffer params) {
988-
mMountingManager.experimental_prefetchResources(
989-
mReactApplicationContext, componentName, params);
989+
if (ReactNativeFeatureFlags.enableImagePrefetchingOnUiThreadAndroid()) {
990+
mMountItemDispatcher.addMountItem(
991+
new PrefetchResourcesMountItem(mReactApplicationContext, componentName, params));
992+
} else {
993+
mMountingManager.experimental_prefetchResources(
994+
mReactApplicationContext, componentName, params);
995+
}
990996
}
991997

992998
void setBinding(FabricUIManagerBinding binding) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.fabric.mounting.mountitems
9+
10+
import com.facebook.react.bridge.ReactApplicationContext
11+
import com.facebook.react.common.annotations.FrameworkAPI
12+
import com.facebook.react.common.annotations.UnstableReactNativeAPI
13+
import com.facebook.react.common.mapbuffer.ReadableMapBuffer
14+
import com.facebook.react.fabric.mounting.MountingManager
15+
16+
internal class PrefetchResourcesMountItem(
17+
private val reactApplicationContext: ReactApplicationContext,
18+
private val componentName: String,
19+
private val params: ReadableMapBuffer,
20+
) : MountItem {
21+
22+
@OptIn(UnstableReactNativeAPI::class, FrameworkAPI::class)
23+
override fun execute(mountingManager: MountingManager) {
24+
mountingManager.experimental_prefetchResources(
25+
reactApplicationContext,
26+
componentName,
27+
params,
28+
)
29+
}
30+
31+
override fun getSurfaceId(): Int = -1 /* unused */
32+
33+
override fun toString(): String = "PrefetchResourcesMountItem"
34+
}

0 commit comments

Comments
 (0)