Skip to content

Commit

Permalink
added flag for useBatchingForLayoutOutputs experiment
Browse files Browse the repository at this point in the history
Summary:
Using a config flag to switch between different implementations of transferring layout outputs
- YogaNodeJNI uses multiple access of java fields to pass all properties like width, height, margin etc...
- YogaNodeJNIBatching uses a float array to pass all the data in one java field access

Reviewed By: davidaurelio

Differential Revision: D14378301

fbshipit-source-id: 0da5b28e6a67ad8fd60eb7efe622d9b2deaf177f
  • Loading branch information
SidharthGuglani-zz authored and facebook-github-bot committed Apr 1, 2019
1 parent 28dc0b0 commit 5ab519e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/yoga/src/main/java/com/facebook/yoga/YogaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class YogaConfig {

public static int SPACING_TYPE = 1;
public static boolean useBatchingForLayoutOutputs = false;

long mNativePointer;
private YogaLogger mLogger;
Expand Down
4 changes: 2 additions & 2 deletions lib/yoga/src/main/java/com/facebook/yoga/YogaNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class YogaNative {

// YGNode related
static native int jni_YGNodeGetInstanceCount();
static native long jni_YGNodeNew();
static native long jni_YGNodeNewWithConfig(long configPointer);
static native long jni_YGNodeNew(boolean useBatchingForLayoutOutputs);
static native long jni_YGNodeNewWithConfig(long configPointer, boolean useBatchingForLayoutOutputs);
static native void jni_YGNodeFree(long nativePointer);
static native void jni_YGNodeReset(long nativePointer);
static native void jni_YGNodeInsertChild(long nativePointer, long childPointer, int index);
Expand Down
4 changes: 2 additions & 2 deletions lib/yoga/src/main/java/com/facebook/yoga/YogaNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

public abstract class YogaNode {
public static YogaNode create() {
return new YogaNodeJNI();
return YogaConfig.useBatchingForLayoutOutputs ? new YogaNodeJNIBatching() : new YogaNodeJNI();
}

public static YogaNode create(YogaConfig config) {
return new YogaNodeJNI(config);
return YogaConfig.useBatchingForLayoutOutputs ? new YogaNodeJNIBatching(config) : new YogaNodeJNI(config);
}

public abstract void reset();
Expand Down
4 changes: 2 additions & 2 deletions lib/yoga/src/main/java/com/facebook/yoga/YogaNodeJNIBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public abstract class YogaNodeJNIBase extends YogaNode {
@Nullable private Object mData;

public YogaNodeJNIBase() {
mNativePointer = YogaNative.jni_YGNodeNew();
mNativePointer = YogaNative.jni_YGNodeNew(YogaConfig.useBatchingForLayoutOutputs);
if (mNativePointer == 0) {
throw new IllegalStateException("Failed to allocate native memory");
}
}

public YogaNodeJNIBase(YogaConfig config) {
mNativePointer = YogaNative.jni_YGNodeNewWithConfig(config.mNativePointer);
mNativePointer = YogaNative.jni_YGNodeNewWithConfig(config.mNativePointer, YogaConfig.useBatchingForLayoutOutputs);
if (mNativePointer == 0) {
throw new IllegalStateException("Failed to allocate native memory");
}
Expand Down
13 changes: 10 additions & 3 deletions lib/yogajni/src/main/cpp/jni/YGJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const short int LAYOUT_MARGIN_START_INDEX = 6;
const short int LAYOUT_PADDING_START_INDEX = 10;
const short int LAYOUT_BORDER_START_INDEX = 14;

bool useBatchingForLayoutOutputs;

class PtrJNodeMap {
using JNodeArray = JArrayClass<JYogaNode::javaobject>;
std::map<YGNodeRef, size_t> ptrsToIdxs_;
Expand Down Expand Up @@ -194,7 +196,7 @@ static void YGTransferLayoutOutputsRecursive(

auto edgesSet = YGNodeEdges{root};

if (false) {
if (useBatchingForLayoutOutputs) {
bool marginFieldSet = edgesSet.has(YGNodeEdges::MARGIN);
bool paddingFieldSet = edgesSet.has(YGNodeEdges::PADDING);
bool borderFieldSet = edgesSet.has(YGNodeEdges::BORDER);
Expand Down Expand Up @@ -431,16 +433,21 @@ static int YGJNILogFunc(
return result;
}

jlong jni_YGNodeNew(alias_ref<jclass>) {
jlong jni_YGNodeNew(alias_ref<jobject> thiz, jboolean useBatching) {
const YGNodeRef node = YGNodeNew();
node->setContext(YGNodeContext{}.asVoidPtr);
node->setPrintFunc(YGPrint);
useBatchingForLayoutOutputs = useBatching;
return reinterpret_cast<jlong>(node);
}

jlong jni_YGNodeNewWithConfig(alias_ref<jclass>, jlong configPointer) {
jlong jni_YGNodeNewWithConfig(
alias_ref<jclass>,
jlong configPointer,
jboolean useBatching) {
const YGNodeRef node = YGNodeNewWithConfig(_jlong2YGConfigRef(configPointer));
node->setContext(YGNodeContext{}.asVoidPtr);
useBatchingForLayoutOutputs = useBatching;
return reinterpret_cast<jlong>(node);
}

Expand Down

0 comments on commit 5ab519e

Please sign in to comment.