Dont create a spacing object for returning margin, padding, border, and position

Summary: The current implementation was made out of simplicity and to keep the same API as before. Now that the java version of csslayout is deprecated it is time to change the API to make the calls more efficient for the JNI version. This diff with reduce allocations as well as reduce the number of JNI calls done.

Differential Revision: D4050773

fbshipit-source-id: 3fd04c27f887a36875e455b5404a17154ac18f91
This commit is contained in:
Emil Sjolander
2016-10-24 10:35:43 -07:00
committed by Facebook Github Bot
parent 69c374e74e
commit e9b9973cae
4 changed files with 28 additions and 92 deletions

View File

@@ -253,18 +253,9 @@ namespace Facebook.CSSLayout
} }
} }
public Spacing GetMargin() public float GetMargin(CSSEdge edge)
{ {
return Native.CSSNodeStyleGetMargin(_cssNode, edge);
var margin = new Spacing();
margin.Set(Spacing.Left, Native.CSSNodeStyleGetMargin(_cssNode, CSSEdge.Left));
margin.Set(Spacing.Top, Native.CSSNodeStyleGetMargin(_cssNode, CSSEdge.Top));
margin.Set(Spacing.Right, Native.CSSNodeStyleGetMargin(_cssNode, CSSEdge.Right));
margin.Set(Spacing.Bottom, Native.CSSNodeStyleGetMargin(_cssNode, CSSEdge.Bottom));
margin.Set(Spacing.Start, Native.CSSNodeStyleGetMargin(_cssNode, CSSEdge.Start));
margin.Set(Spacing.End, Native.CSSNodeStyleGetMargin(_cssNode, CSSEdge.End));
return margin;
} }
public void SetMargin(CSSEdge edge, float value) public void SetMargin(CSSEdge edge, float value)
@@ -272,18 +263,9 @@ namespace Facebook.CSSLayout
Native.CSSNodeStyleSetMargin(_cssNode, edge, value); Native.CSSNodeStyleSetMargin(_cssNode, edge, value);
} }
public Spacing GetPadding() public float GetPadding(CSSEdge edge)
{ {
return Native.CSSNodeStyleGetPadding(_cssNode, edge);
var padding = new Spacing();
padding.Set(Spacing.Left, Native.CSSNodeStyleGetPadding(_cssNode, CSSEdge.Left));
padding.Set(Spacing.Top, Native.CSSNodeStyleGetPadding(_cssNode, CSSEdge.Top));
padding.Set(Spacing.Right, Native.CSSNodeStyleGetPadding(_cssNode, CSSEdge.Right));
padding.Set(Spacing.Bottom, Native.CSSNodeStyleGetPadding(_cssNode, CSSEdge.Bottom));
padding.Set(Spacing.Start, Native.CSSNodeStyleGetPadding(_cssNode, CSSEdge.Start));
padding.Set(Spacing.End, Native.CSSNodeStyleGetPadding(_cssNode, CSSEdge.End));
return padding;
} }
public void SetPadding(CSSEdge edge, float padding) public void SetPadding(CSSEdge edge, float padding)
@@ -291,18 +273,9 @@ namespace Facebook.CSSLayout
Native.CSSNodeStyleSetPadding(_cssNode, edge, padding); Native.CSSNodeStyleSetPadding(_cssNode, edge, padding);
} }
public Spacing GetBorder() public float GetBorder(CSSEdge edge)
{ {
return Native.CSSNodeStyleGetBorder(_cssNode, edge);
var border = new Spacing();
border.Set(Spacing.Left, Native.CSSNodeStyleGetBorder(_cssNode, CSSEdge.Left));
border.Set(Spacing.Top, Native.CSSNodeStyleGetBorder(_cssNode, CSSEdge.Top));
border.Set(Spacing.Right, Native.CSSNodeStyleGetBorder(_cssNode, CSSEdge.Right));
border.Set(Spacing.Bottom, Native.CSSNodeStyleGetBorder(_cssNode, CSSEdge.Bottom));
border.Set(Spacing.Start, Native.CSSNodeStyleGetBorder(_cssNode, CSSEdge.Start));
border.Set(Spacing.End, Native.CSSNodeStyleGetBorder(_cssNode, CSSEdge.End));
return border;
} }
public void SetBorder(CSSEdge edge, float border) public void SetBorder(CSSEdge edge, float border)
@@ -310,18 +283,9 @@ namespace Facebook.CSSLayout
Native.CSSNodeStyleSetBorder(_cssNode, edge, border); Native.CSSNodeStyleSetBorder(_cssNode, edge, border);
} }
public Spacing GetPosition() public float GetPosition(CSSEdge edge)
{ {
return Native.CSSNodeStyleGetPosition(_cssNode, edge);
var position = new Spacing();
position.Set(Spacing.Left, Native.CSSNodeStyleGetPosition(_cssNode, CSSEdge.Left));
position.Set(Spacing.Top, Native.CSSNodeStyleGetPosition(_cssNode, CSSEdge.Top));
position.Set(Spacing.Right, Native.CSSNodeStyleGetPosition(_cssNode, CSSEdge.Right));
position.Set(Spacing.Bottom, Native.CSSNodeStyleGetPosition(_cssNode, CSSEdge.Bottom));
position.Set(Spacing.Start, Native.CSSNodeStyleGetPosition(_cssNode, CSSEdge.Start));
position.Set(Spacing.End, Native.CSSNodeStyleGetPosition(_cssNode, CSSEdge.End));
return position;
} }
public void SetPosition(CSSEdge edge, float position) public void SetPosition(CSSEdge edge, float position)

View File

@@ -303,15 +303,8 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native float jni_CSSNodeStyleGetMargin(long nativePointer, int edge); private native float jni_CSSNodeStyleGetMargin(long nativePointer, int edge);
@Override @Override
public Spacing getMargin() { public float getMargin(int spacingType) {
Spacing margin = new Spacing(); return jni_CSSNodeStyleGetMargin(mNativePointer, spacingType);
margin.set(Spacing.LEFT, jni_CSSNodeStyleGetMargin(mNativePointer, Spacing.LEFT));
margin.set(Spacing.TOP, jni_CSSNodeStyleGetMargin(mNativePointer, Spacing.TOP));
margin.set(Spacing.RIGHT, jni_CSSNodeStyleGetMargin(mNativePointer, Spacing.RIGHT));
margin.set(Spacing.BOTTOM, jni_CSSNodeStyleGetMargin(mNativePointer, Spacing.BOTTOM));
margin.set(Spacing.START, jni_CSSNodeStyleGetMargin(mNativePointer, Spacing.START));
margin.set(Spacing.END, jni_CSSNodeStyleGetMargin(mNativePointer, Spacing.END));
return margin;
} }
private native void jni_CSSNodeStyleSetMargin(long nativePointer, int edge, float margin); private native void jni_CSSNodeStyleSetMargin(long nativePointer, int edge, float margin);
@@ -322,15 +315,8 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native float jni_CSSNodeStyleGetPadding(long nativePointer, int edge); private native float jni_CSSNodeStyleGetPadding(long nativePointer, int edge);
@Override @Override
public Spacing getPadding() { public float getPadding(int spacingType) {
Spacing padding = new Spacing(); return jni_CSSNodeStyleGetPadding(mNativePointer, spacingType);
padding.set(Spacing.LEFT, jni_CSSNodeStyleGetPadding(mNativePointer, Spacing.LEFT));
padding.set(Spacing.TOP, jni_CSSNodeStyleGetPadding(mNativePointer, Spacing.TOP));
padding.set(Spacing.RIGHT, jni_CSSNodeStyleGetPadding(mNativePointer, Spacing.RIGHT));
padding.set(Spacing.BOTTOM, jni_CSSNodeStyleGetPadding(mNativePointer, Spacing.BOTTOM));
padding.set(Spacing.START, jni_CSSNodeStyleGetPadding(mNativePointer, Spacing.START));
padding.set(Spacing.END, jni_CSSNodeStyleGetPadding(mNativePointer, Spacing.END));
return padding;
} }
private native void jni_CSSNodeStyleSetPadding(long nativePointer, int edge, float padding); private native void jni_CSSNodeStyleSetPadding(long nativePointer, int edge, float padding);
@@ -341,15 +327,8 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native float jni_CSSNodeStyleGetBorder(long nativePointer, int edge); private native float jni_CSSNodeStyleGetBorder(long nativePointer, int edge);
@Override @Override
public Spacing getBorder() { public float getBorder(int spacingType) {
Spacing border = new Spacing(); return jni_CSSNodeStyleGetBorder(mNativePointer, spacingType);
border.set(Spacing.LEFT, jni_CSSNodeStyleGetBorder(mNativePointer, Spacing.LEFT));
border.set(Spacing.TOP, jni_CSSNodeStyleGetBorder(mNativePointer, Spacing.TOP));
border.set(Spacing.RIGHT, jni_CSSNodeStyleGetBorder(mNativePointer, Spacing.RIGHT));
border.set(Spacing.BOTTOM, jni_CSSNodeStyleGetBorder(mNativePointer, Spacing.BOTTOM));
border.set(Spacing.START, jni_CSSNodeStyleGetBorder(mNativePointer, Spacing.START));
border.set(Spacing.END, jni_CSSNodeStyleGetBorder(mNativePointer, Spacing.END));
return border;
} }
private native void jni_CSSNodeStyleSetBorder(long nativePointer, int edge, float border); private native void jni_CSSNodeStyleSetBorder(long nativePointer, int edge, float border);
@@ -360,15 +339,8 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
private native float jni_CSSNodeStyleGetPosition(long nativePointer, int edge); private native float jni_CSSNodeStyleGetPosition(long nativePointer, int edge);
@Override @Override
public Spacing getPosition() { public float getPosition(int spacingType) {
Spacing position = new Spacing(); return jni_CSSNodeStyleGetPosition(mNativePointer, spacingType);
position.set(Spacing.LEFT, jni_CSSNodeStyleGetPosition(mNativePointer, Spacing.LEFT));
position.set(Spacing.TOP, jni_CSSNodeStyleGetPosition(mNativePointer, Spacing.TOP));
position.set(Spacing.RIGHT, jni_CSSNodeStyleGetPosition(mNativePointer, Spacing.RIGHT));
position.set(Spacing.BOTTOM, jni_CSSNodeStyleGetPosition(mNativePointer, Spacing.BOTTOM));
position.set(Spacing.START, jni_CSSNodeStyleGetPosition(mNativePointer, Spacing.START));
position.set(Spacing.END, jni_CSSNodeStyleGetPosition(mNativePointer, Spacing.END));
return position;
} }
private native void jni_CSSNodeStyleSetPosition(long nativePointer, int edge, float position); private native void jni_CSSNodeStyleSetPosition(long nativePointer, int edge, float position);

View File

@@ -59,13 +59,13 @@ public interface CSSNodeAPI<CSSNodeType extends CSSNodeAPI> {
void setFlexShrink(float flexShrink); void setFlexShrink(float flexShrink);
float getFlexBasis(); float getFlexBasis();
void setFlexBasis(float flexBasis); void setFlexBasis(float flexBasis);
Spacing getMargin(); float getMargin(int spacingType);
void setMargin(int spacingType, float margin); void setMargin(int spacingType, float margin);
Spacing getPadding(); float getPadding(int spacingType);
void setPadding(int spacingType, float padding); void setPadding(int spacingType, float padding);
Spacing getBorder(); float getBorder(int spacingType);
void setBorder(int spacingType, float border); void setBorder(int spacingType, float border);
Spacing getPosition(); float getPosition(int spacingType);
void setPosition(int spacingType, float position); void setPosition(int spacingType, float position);
float getStyleWidth(); float getStyleWidth();
void setStyleWidth(float width); void setStyleWidth(float width);

View File

@@ -413,8 +413,8 @@ public class CSSNodeDEPRECATED implements CSSNodeAPI<CSSNodeDEPRECATED> {
* Get this node's margin, as defined by style + default margin. * Get this node's margin, as defined by style + default margin.
*/ */
@Override @Override
public Spacing getMargin() { public float getMargin(int spacingType) {
return style.margin; return style.margin.get(spacingType);
} }
@Override @Override
@@ -428,8 +428,8 @@ public class CSSNodeDEPRECATED implements CSSNodeAPI<CSSNodeDEPRECATED> {
* Get this node's padding, as defined by style + default padding. * Get this node's padding, as defined by style + default padding.
*/ */
@Override @Override
public Spacing getPadding() { public float getPadding(int spacingType) {
return style.padding; return style.padding.get(spacingType);
} }
@Override @Override
@@ -443,8 +443,8 @@ public class CSSNodeDEPRECATED implements CSSNodeAPI<CSSNodeDEPRECATED> {
* Get this node's border, as defined by style. * Get this node's border, as defined by style.
*/ */
@Override @Override
public Spacing getBorder() { public float getBorder(int spacingType) {
return style.border; return style.border.get(spacingType);
} }
@Override @Override
@@ -458,8 +458,8 @@ public class CSSNodeDEPRECATED implements CSSNodeAPI<CSSNodeDEPRECATED> {
* Get this node's position, as defined by style. * Get this node's position, as defined by style.
*/ */
@Override @Override
public Spacing getPosition() { public float getPosition(int spacingType) {
return style.position; return style.position.get(spacingType);
} }
@Override @Override