From 4ac500a337572996c4a822c3433da3e4297cf138 Mon Sep 17 00:00:00 2001 From: "Andrew Chen (Eng)" Date: Mon, 1 Oct 2018 16:07:29 -0700 Subject: [PATCH] Don't access ReadableNativeArray on UI thread Summary: ReadableNativeArray initializes the ReactBridge which loads a bunch of c++ classes. Let's avoid doing this on the UI thread. Reviewed By: mdvacca Differential Revision: D10108380 fbshipit-source-id: ab4520535288ce450a865952e996b716d571df7f --- .../main/java/com/facebook/react/bridge/BUCK | 1 + .../react/bridge/ReadableNativeArray.java | 25 ++++++++--------- .../react/bridge/ReadableNativeMap.java | 28 +++++++++---------- .../react/config/ReactFeatureFlags.java | 9 ++++++ 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK b/ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK index 3f14185bf8ffcb..f76357ec486ca7 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK @@ -38,6 +38,7 @@ rn_android_library( react_native_target("java/com/facebook/debug/tags:tags"), react_native_target("java/com/facebook/debug/holder:holder"), react_native_target("java/com/facebook/react/common:common"), + react_native_target("java/com/facebook/react/config:config"), react_native_target("java/com/facebook/react/module/model:model"), react_native_target("java/com/facebook/react/uimanager/common:common"), react_native_target("java/com/facebook/react/module/annotations:annotations"), diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeArray.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeArray.java index 5439f4c54546d3..80f360adc1d63f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeArray.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeArray.java @@ -7,12 +7,12 @@ package com.facebook.react.bridge; +import com.facebook.infer.annotation.Assertions; import com.facebook.jni.HybridData; import com.facebook.proguard.annotations.DoNotStrip; - +import com.facebook.react.config.ReactFeatureFlags; import java.util.ArrayList; import java.util.Arrays; -import com.facebook.infer.annotation.Assertions; import javax.annotation.Nullable; /** @@ -34,9 +34,8 @@ protected ReadableNativeArray(HybridData hybridData) { private @Nullable ReadableType[] mLocalTypeArray; private static int jniPassCounter = 0; - private static boolean mUseNativeAccessor = false; public static void setUseNativeAccessor(boolean useNativeAccessor) { - mUseNativeAccessor = useNativeAccessor; + ReactFeatureFlags.useArrayNativeAccessor = useNativeAccessor; } public static int getJNIPassCounter() { return jniPassCounter; @@ -77,7 +76,7 @@ private ReadableType[] getLocalTypeArray() { @Override public int size() { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useArrayNativeAccessor) { jniPassCounter++; return sizeNative(); } @@ -87,7 +86,7 @@ public int size() { @Override public boolean isNull(int index) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useArrayNativeAccessor) { jniPassCounter++; return isNullNative(index); } @@ -97,7 +96,7 @@ public boolean isNull(int index) { @Override public boolean getBoolean(int index) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useArrayNativeAccessor) { jniPassCounter++; return getBooleanNative(index); } @@ -107,7 +106,7 @@ public boolean getBoolean(int index) { @Override public double getDouble(int index) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useArrayNativeAccessor) { jniPassCounter++; return getDoubleNative(index); } @@ -117,7 +116,7 @@ public double getDouble(int index) { @Override public int getInt(int index) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useArrayNativeAccessor) { jniPassCounter++; return getIntNative(index); } @@ -127,7 +126,7 @@ public int getInt(int index) { @Override public String getString(int index) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useArrayNativeAccessor) { jniPassCounter++; return getStringNative(index); } @@ -137,7 +136,7 @@ public String getString(int index) { @Override public ReadableNativeArray getArray(int index) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useArrayNativeAccessor) { jniPassCounter++; return getArrayNative(index); } @@ -147,7 +146,7 @@ public ReadableNativeArray getArray(int index) { @Override public ReadableNativeMap getMap(int index) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useArrayNativeAccessor) { jniPassCounter++; return getMapNative(index); } @@ -157,7 +156,7 @@ public ReadableNativeMap getMap(int index) { @Override public ReadableType getType(int index) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useArrayNativeAccessor) { jniPassCounter++; return getTypeNative(index); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java index 309f405260ef49..1ece8d666b0a44 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java @@ -7,13 +7,12 @@ package com.facebook.react.bridge; +import com.facebook.infer.annotation.Assertions; import com.facebook.jni.HybridData; import com.facebook.proguard.annotations.DoNotStrip; - +import com.facebook.react.config.ReactFeatureFlags; import java.util.HashMap; import java.util.Iterator; - -import com.facebook.infer.annotation.Assertions; import javax.annotation.Nullable; /** @@ -33,10 +32,9 @@ protected ReadableNativeMap(HybridData hybridData) { private @Nullable String[] mKeys; private @Nullable HashMap mLocalMap; private @Nullable HashMap mLocalTypeMap; - private static boolean mUseNativeAccessor; private static int mJniCallCounter; public static void setUseNativeAccessor(boolean useNativeAccessor) { - mUseNativeAccessor = useNativeAccessor; + ReactFeatureFlags.useMapNativeAccessor = useNativeAccessor; } public static int getJNIPassCounter() { return mJniCallCounter; @@ -94,7 +92,7 @@ private HashMap getLocalTypeMap() { @Override public boolean hasKey(String name) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useMapNativeAccessor) { mJniCallCounter++; return hasKeyNative(name); } @@ -104,7 +102,7 @@ public boolean hasKey(String name) { @Override public boolean isNull(String name) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useMapNativeAccessor) { mJniCallCounter++; return isNullNative(name); } @@ -151,7 +149,7 @@ private void checkInstance(String name, Object value, Class type) { @Override public boolean getBoolean(String name) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useMapNativeAccessor) { mJniCallCounter++; return getBooleanNative(name); } @@ -161,7 +159,7 @@ public boolean getBoolean(String name) { @Override public double getDouble(String name) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useMapNativeAccessor) { mJniCallCounter++; return getDoubleNative(name); } @@ -171,7 +169,7 @@ public double getDouble(String name) { @Override public int getInt(String name) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useMapNativeAccessor) { mJniCallCounter++; return getIntNative(name); } @@ -183,7 +181,7 @@ public int getInt(String name) { @Override public @Nullable String getString(String name) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useMapNativeAccessor) { mJniCallCounter++; return getStringNative(name); } @@ -193,7 +191,7 @@ public int getInt(String name) { @Override public @Nullable ReadableArray getArray(String name) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useMapNativeAccessor) { mJniCallCounter++; return getArrayNative(name); } @@ -203,7 +201,7 @@ public int getInt(String name) { @Override public @Nullable ReadableNativeMap getMap(String name) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useMapNativeAccessor) { mJniCallCounter++; return getMapNative(name); } @@ -213,7 +211,7 @@ public int getInt(String name) { @Override public ReadableType getType(String name) { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useMapNativeAccessor) { mJniCallCounter++; return getTypeNative(name); } @@ -236,7 +234,7 @@ public ReadableMapKeySetIterator keySetIterator() { @Override public HashMap toHashMap() { - if (mUseNativeAccessor) { + if (ReactFeatureFlags.useMapNativeAccessor) { ReadableMapKeySetIterator iterator = keySetIterator(); HashMap hashMap = new HashMap<>(); diff --git a/ReactAndroid/src/main/java/com/facebook/react/config/ReactFeatureFlags.java b/ReactAndroid/src/main/java/com/facebook/react/config/ReactFeatureFlags.java index c7de00bacc6f3e..f9f1b70833301b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/config/ReactFeatureFlags.java +++ b/ReactAndroid/src/main/java/com/facebook/react/config/ReactFeatureFlags.java @@ -14,4 +14,13 @@ public class ReactFeatureFlags { */ public static boolean lazilyLoadViewManagers = false; + /** + * Reduce the number of Java-JS interops while accessing native arrays + */ + public static boolean useArrayNativeAccessor = false; + + /** + * Reduce the number of Java-JS interops while accessing native maps + */ + public static boolean useMapNativeAccessor = false; }