Skip to content

Commit

Permalink
Move native accesses of array into one access instead of per-element
Browse files Browse the repository at this point in the history
Differential Revision: D6578349

fbshipit-source-id: 84ebd66ed791a845e9cc0fbc7e12a377534aa903
  • Loading branch information
Kathy Gray authored and facebook-github-bot committed Jan 17, 2018
1 parent 24e521c commit 5649aed
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import com.facebook.proguard.annotations.DoNotStrip;

import java.util.ArrayList;
import java.util.Arrays;
import com.facebook.infer.annotation.Assertions;
import javax.annotation.Nullable;

/**
* Implementation of a NativeArray that allows read-only access to its members. This will generally
Expand All @@ -28,24 +31,142 @@ protected ReadableNativeArray(HybridData hybridData) {
super(hybridData);
}

//WriteOnce but not in the constructor fields
private @Nullable Object[] mLocalArray;
private @Nullable ReadableType[] mLocalTypeArray;

private static int jniPassCounter = 0;
private static boolean mUseNativeAccessor = false;
public static void setUseNativeAccessor(boolean useNativeAccessor) {
mUseNativeAccessor = useNativeAccessor;
}
public static int getJNIPassCounter() {
return jniPassCounter;
}

private Object[] getLocalArray() {
// Fast, non blocking check for the common case
if (mLocalArray != null) {
return mLocalArray;
}
synchronized (this) {
// Make sure no concurrent call already updated
if (mLocalArray == null) {
jniPassCounter++;
mLocalArray = Assertions.assertNotNull(importArray());
}
}
return mLocalArray;
}
private native Object[] importArray();

private ReadableType[] getLocalTypeArray() {
// Fast, non-blocking check for the common case
if (mLocalTypeArray != null) {
return mLocalTypeArray;
}
synchronized (this) {
// Make sure no concurrent call already updated
if (mLocalTypeArray == null) {
jniPassCounter++;
Object[] tempArray = Assertions.assertNotNull(importTypeArray());
mLocalTypeArray = Arrays.copyOf(tempArray, tempArray.length, ReadableType[].class);
}
}
return mLocalTypeArray;
}
private native Object[] importTypeArray();

@Override
public native int size();
public int size() {
if (mUseNativeAccessor) {
jniPassCounter++;
return sizeNative();
}
return getLocalArray().length;
}
private native int sizeNative();

@Override
public native boolean isNull(int index);
public boolean isNull(int index) {
if (mUseNativeAccessor) {
jniPassCounter++;
return isNullNative(index);
}
return getLocalArray()[index] == null;
}
private native boolean isNullNative(int index);

@Override
public native boolean getBoolean(int index);
public boolean getBoolean(int index) {
if (mUseNativeAccessor) {
jniPassCounter++;
return getBooleanNative(index);
}
return ((Boolean) getLocalArray()[index]).booleanValue();
}
private native boolean getBooleanNative(int index);

@Override
public native double getDouble(int index);
public double getDouble(int index) {
if (mUseNativeAccessor) {
jniPassCounter++;
return getDoubleNative(index);
}
return ((Double) getLocalArray()[index]).doubleValue();
}
private native double getDoubleNative(int index);

@Override
public native int getInt(int index);
public int getInt(int index) {
if (mUseNativeAccessor) {
jniPassCounter++;
return getIntNative(index);
}
return ((Double) getLocalArray()[index]).intValue();
}
private native int getIntNative(int index);

@Override
public native String getString(int index);
public String getString(int index) {
if (mUseNativeAccessor) {
jniPassCounter++;
return getStringNative(index);
}
return (String) getLocalArray()[index];
}
private native String getStringNative(int index);

@Override
public native ReadableNativeArray getArray(int index);
public ReadableNativeArray getArray(int index) {
if (mUseNativeAccessor) {
jniPassCounter++;
return getArrayNative(index);
}
return (ReadableNativeArray) getLocalArray()[index];
}
private native ReadableNativeArray getArrayNative(int index);

@Override
public native ReadableNativeMap getMap(int index);
public ReadableNativeMap getMap(int index) {
if (mUseNativeAccessor) {
jniPassCounter++;
return getMapNative(index);
}
return (ReadableNativeMap) getLocalArray()[index];
}
private native ReadableNativeMap getMapNative(int index);

@Override
public native ReadableType getType(int index);
public ReadableType getType(int index) {
if (mUseNativeAccessor) {
jniPassCounter++;
return getTypeNative(index);
}
return getLocalTypeArray()[index];
}

private native ReadableType getTypeNative(int index);

@Override
public Dynamic getDynamic(int index) {
Expand Down
71 changes: 62 additions & 9 deletions ReactAndroid/src/main/jni/react/jni/ReadableNativeArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,48 @@ const char* ReadableNativeArray::getString(jint index) {
return dyn.getString().c_str();
}

local_ref<JArrayClass<jobject>> ReadableNativeArray::importArray() {
jint size = array_.size();
auto jarray = JArrayClass<jobject>::newArray(size);
for (jint i = 0; i < size; i++) {
switch(array_.at(i).type()) {
case folly::dynamic::Type::NULLT: {
jarray->setElement(i, nullptr);
break;
}
case folly::dynamic::Type::BOOL: {
jarray->
setElement(i,
JBoolean::valueOf(ReadableNativeArray::getBoolean(i)).release());
break;
}
case folly::dynamic::Type::INT64:
case folly::dynamic::Type::DOUBLE: {
jarray->setElement(i,
JDouble::valueOf(ReadableNativeArray::getDouble(i)).release());
break;
}
case folly::dynamic::Type::STRING: {
jarray->
setElement(i,
make_jstring(ReadableNativeArray::getString(i)).release());
break;
}
case folly::dynamic::Type::OBJECT: {
jarray->setElement(i,ReadableNativeArray::getMap(i).release());
break;
}
case folly::dynamic::Type::ARRAY: {
jarray->setElement(i,ReadableNativeArray::getArray(i).release());
break;
}
default:
break;
}
}
return jarray;
}

local_ref<ReadableNativeArray::jhybridobject> ReadableNativeArray::getArray(jint index) {
auto& elem = array_.at(index);
if (elem.isNull()) {
Expand All @@ -69,6 +111,15 @@ local_ref<ReadableType> ReadableNativeArray::getType(jint index) {
return ReadableType::getType(array_.at(index).type());
}

local_ref<JArrayClass<jobject>> ReadableNativeArray::importTypeArray() {
jint size = array_.size();
auto jarray = JArrayClass<jobject>::newArray(size);
for (jint i = 0; i < size; i++) {
jarray->setElement(i, ReadableNativeArray::getType(i).release());
}
return jarray;
}

local_ref<NativeMap::jhybridobject> ReadableNativeArray::getMap(jint index) {
auto& elem = array_.at(index);
return ReadableNativeMap::createWithContents(folly::dynamic(elem));
Expand All @@ -83,15 +134,17 @@ local_ref<ReadableNativeMap::jhybridobject> getMapFixed(alias_ref<ReadableNative

void ReadableNativeArray::registerNatives() {
registerHybrid({
makeNativeMethod("size", ReadableNativeArray::getSize),
makeNativeMethod("isNull", ReadableNativeArray::isNull),
makeNativeMethod("getBoolean", ReadableNativeArray::getBoolean),
makeNativeMethod("getDouble", ReadableNativeArray::getDouble),
makeNativeMethod("getInt", ReadableNativeArray::getInt),
makeNativeMethod("getString", ReadableNativeArray::getString),
makeNativeMethod("getArray", ReadableNativeArray::getArray),
makeNativeMethod("getMap", getMapFixed),
makeNativeMethod("getType", ReadableNativeArray::getType),
makeNativeMethod("importArray", ReadableNativeArray::importArray),
makeNativeMethod("importTypeArray", ReadableNativeArray::importTypeArray),
makeNativeMethod("sizeNative", ReadableNativeArray::getSize),
makeNativeMethod("isNullNative", ReadableNativeArray::isNull),
makeNativeMethod("getBooleanNative", ReadableNativeArray::getBoolean),
makeNativeMethod("getDoubleNative", ReadableNativeArray::getDouble),
makeNativeMethod("getIntNative", ReadableNativeArray::getInt),
makeNativeMethod("getStringNative", ReadableNativeArray::getString),
makeNativeMethod("getArrayNative", ReadableNativeArray::getArray),
makeNativeMethod("getMapNative", getMapFixed),
makeNativeMethod("getTypeNative", ReadableNativeArray::getType),
});
}

Expand Down
2 changes: 2 additions & 0 deletions ReactAndroid/src/main/jni/react/jni/ReadableNativeArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ReadableNativeArray : public jni::HybridClass<ReadableNativeArray, NativeA
static constexpr const char* kJavaDescriptor = "Lcom/facebook/react/bridge/ReadableNativeArray;";

static void mapException(const std::exception& ex);
jni::local_ref<jni::JArrayClass<jobject>> importArray();
jni::local_ref<jni::JArrayClass<jobject>> importTypeArray();
jint getSize();
jboolean isNull(jint index);
jboolean getBoolean(jint index);
Expand Down

0 comments on commit 5649aed

Please sign in to comment.