Skip to content

Commit

Permalink
Don't access ReadableNativeArray on UI thread
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ayc1 authored and facebook-github-bot committed Oct 1, 2018
1 parent b030fcb commit 4ac500a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
1 change: 1 addition & 0 deletions ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand Down Expand Up @@ -77,7 +76,7 @@ private ReadableType[] getLocalTypeArray() {

@Override
public int size() {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return sizeNative();
}
Expand All @@ -87,7 +86,7 @@ public int size() {

@Override
public boolean isNull(int index) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return isNullNative(index);
}
Expand All @@ -97,7 +96,7 @@ public boolean isNull(int index) {

@Override
public boolean getBoolean(int index) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return getBooleanNative(index);
}
Expand All @@ -107,7 +106,7 @@ public boolean getBoolean(int index) {

@Override
public double getDouble(int index) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return getDoubleNative(index);
}
Expand All @@ -117,7 +116,7 @@ public double getDouble(int index) {

@Override
public int getInt(int index) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return getIntNative(index);
}
Expand All @@ -127,7 +126,7 @@ public int getInt(int index) {

@Override
public String getString(int index) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return getStringNative(index);
}
Expand All @@ -137,7 +136,7 @@ public String getString(int index) {

@Override
public ReadableNativeArray getArray(int index) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return getArrayNative(index);
}
Expand All @@ -147,7 +146,7 @@ public ReadableNativeArray getArray(int index) {

@Override
public ReadableNativeMap getMap(int index) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return getMapNative(index);
}
Expand All @@ -157,7 +156,7 @@ public ReadableNativeMap getMap(int index) {

@Override
public ReadableType getType(int index) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useArrayNativeAccessor) {
jniPassCounter++;
return getTypeNative(index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -33,10 +32,9 @@ protected ReadableNativeMap(HybridData hybridData) {
private @Nullable String[] mKeys;
private @Nullable HashMap<String,Object> mLocalMap;
private @Nullable HashMap<String,ReadableType> 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;
Expand Down Expand Up @@ -94,7 +92,7 @@ private HashMap<String,ReadableType> getLocalTypeMap() {

@Override
public boolean hasKey(String name) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useMapNativeAccessor) {
mJniCallCounter++;
return hasKeyNative(name);
}
Expand All @@ -104,7 +102,7 @@ public boolean hasKey(String name) {

@Override
public boolean isNull(String name) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useMapNativeAccessor) {
mJniCallCounter++;
return isNullNative(name);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -161,7 +159,7 @@ public boolean getBoolean(String name) {

@Override
public double getDouble(String name) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useMapNativeAccessor) {
mJniCallCounter++;
return getDoubleNative(name);
}
Expand All @@ -171,7 +169,7 @@ public double getDouble(String name) {

@Override
public int getInt(String name) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useMapNativeAccessor) {
mJniCallCounter++;
return getIntNative(name);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -213,7 +211,7 @@ public int getInt(String name) {

@Override
public ReadableType getType(String name) {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useMapNativeAccessor) {
mJniCallCounter++;
return getTypeNative(name);
}
Expand All @@ -236,7 +234,7 @@ public ReadableMapKeySetIterator keySetIterator() {

@Override
public HashMap<String, Object> toHashMap() {
if (mUseNativeAccessor) {
if (ReactFeatureFlags.useMapNativeAccessor) {
ReadableMapKeySetIterator iterator = keySetIterator();
HashMap<String, Object> hashMap = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 4ac500a

Please sign in to comment.