Change Java to use array indexes instead of methods
Method invocations are not entirely free on Android. Change the generated Java code to use the same array-based approach used in JS and C to compute dimensions, positions, etc instead of relying too heavily on method invovations. As a bonus, the Java transpiler becomes a lot simpler because the code is more analogous to the C counterpart. In my local benchmarks this change gives us a major performance boost on Android (between 15% and 30%) depending on the device and the runtime (Dalvik|Art).
This commit is contained in:
@@ -8,49 +8,50 @@
|
||||
*/
|
||||
package com.facebook.csslayout;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Where the output of {@link LayoutEngine#layoutNode(CSSNode, float)} will go in the CSSNode.
|
||||
*/
|
||||
public class CSSLayout {
|
||||
static final int POSITION_LEFT = 0;
|
||||
static final int POSITION_TOP = 1;
|
||||
static final int POSITION_RIGHT = 2;
|
||||
static final int POSITION_BOTTOM = 3;
|
||||
|
||||
public float top;
|
||||
public float left;
|
||||
public float right;
|
||||
public float bottom;
|
||||
public float width = CSSConstants.UNDEFINED;
|
||||
public float height = CSSConstants.UNDEFINED;
|
||||
public CSSDirection direction = CSSDirection.LTR;
|
||||
static final int DIMENSION_WIDTH = 0;
|
||||
static final int DIMENSION_HEIGHT = 1;
|
||||
|
||||
float[] position = new float[4];
|
||||
float[] dimensions = new float[2];
|
||||
CSSDirection direction = CSSDirection.LTR;
|
||||
|
||||
/**
|
||||
* This should always get called before calling {@link LayoutEngine#layoutNode(CSSNode, float)}
|
||||
*/
|
||||
public void resetResult() {
|
||||
left = 0;
|
||||
top = 0;
|
||||
right = 0;
|
||||
bottom = 0;
|
||||
width = CSSConstants.UNDEFINED;
|
||||
height = CSSConstants.UNDEFINED;
|
||||
Arrays.fill(position, 0);
|
||||
Arrays.fill(dimensions, CSSConstants.UNDEFINED);
|
||||
direction = CSSDirection.LTR;
|
||||
}
|
||||
|
||||
public void copy(CSSLayout layout) {
|
||||
left = layout.left;
|
||||
top = layout.top;
|
||||
right = layout.right;
|
||||
bottom = layout.bottom;
|
||||
width = layout.width;
|
||||
height = layout.height;
|
||||
position[POSITION_LEFT] = layout.position[POSITION_LEFT];
|
||||
position[POSITION_TOP] = layout.position[POSITION_TOP];
|
||||
position[POSITION_RIGHT] = layout.position[POSITION_RIGHT];
|
||||
position[POSITION_BOTTOM] = layout.position[POSITION_BOTTOM];
|
||||
dimensions[DIMENSION_WIDTH] = layout.dimensions[DIMENSION_WIDTH];
|
||||
dimensions[DIMENSION_HEIGHT] = layout.dimensions[DIMENSION_HEIGHT];
|
||||
direction = layout.direction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "layout: {" +
|
||||
"left: " + left + ", " +
|
||||
"top: " + top + ", " +
|
||||
"width: " + width + ", " +
|
||||
"height: " + height + ", " +
|
||||
"left: " + position[POSITION_LEFT] + ", " +
|
||||
"top: " + position[POSITION_TOP] + ", " +
|
||||
"width: " + dimensions[DIMENSION_WIDTH] + ", " +
|
||||
"height: " + dimensions[DIMENSION_HEIGHT] + ", " +
|
||||
"direction: " + direction +
|
||||
"}";
|
||||
}
|
||||
|
@@ -14,6 +14,13 @@ import java.util.ArrayList;
|
||||
|
||||
import com.facebook.infer.annotation.Assertions;
|
||||
|
||||
import static com.facebook.csslayout.CSSLayout.DIMENSION_HEIGHT;
|
||||
import static com.facebook.csslayout.CSSLayout.DIMENSION_WIDTH;
|
||||
import static com.facebook.csslayout.CSSLayout.POSITION_BOTTOM;
|
||||
import static com.facebook.csslayout.CSSLayout.POSITION_LEFT;
|
||||
import static com.facebook.csslayout.CSSLayout.POSITION_RIGHT;
|
||||
import static com.facebook.csslayout.CSSLayout.POSITION_TOP;
|
||||
|
||||
/**
|
||||
* A CSS Node. It has a style object you can manipulate at {@link #style}. After calling
|
||||
* {@link #calculateLayout()}, {@link #layout} will be filled with the results of the layout.
|
||||
@@ -293,61 +300,61 @@ public class CSSNode {
|
||||
}
|
||||
|
||||
public void setPositionTop(float positionTop) {
|
||||
if (!valuesEqual(style.positionTop, positionTop)) {
|
||||
style.positionTop = positionTop;
|
||||
if (!valuesEqual(style.position[POSITION_TOP], positionTop)) {
|
||||
style.position[POSITION_TOP] = positionTop;
|
||||
dirty();
|
||||
}
|
||||
}
|
||||
|
||||
public void setPositionBottom(float positionBottom) {
|
||||
if (!valuesEqual(style.positionBottom, positionBottom)) {
|
||||
style.positionBottom = positionBottom;
|
||||
if (!valuesEqual(style.position[POSITION_BOTTOM], positionBottom)) {
|
||||
style.position[POSITION_BOTTOM] = positionBottom;
|
||||
dirty();
|
||||
}
|
||||
}
|
||||
|
||||
public void setPositionLeft(float positionLeft) {
|
||||
if (!valuesEqual(style.positionLeft, positionLeft)) {
|
||||
style.positionLeft = positionLeft;
|
||||
if (!valuesEqual(style.position[POSITION_LEFT], positionLeft)) {
|
||||
style.position[POSITION_LEFT] = positionLeft;
|
||||
dirty();
|
||||
}
|
||||
}
|
||||
|
||||
public void setPositionRight(float positionRight) {
|
||||
if (!valuesEqual(style.positionRight, positionRight)) {
|
||||
style.positionRight = positionRight;
|
||||
if (!valuesEqual(style.position[POSITION_RIGHT], positionRight)) {
|
||||
style.position[POSITION_RIGHT] = positionRight;
|
||||
dirty();
|
||||
}
|
||||
}
|
||||
|
||||
public void setStyleWidth(float width) {
|
||||
if (!valuesEqual(style.width, width)) {
|
||||
style.width = width;
|
||||
if (!valuesEqual(style.dimensions[DIMENSION_WIDTH], width)) {
|
||||
style.dimensions[DIMENSION_WIDTH] = width;
|
||||
dirty();
|
||||
}
|
||||
}
|
||||
|
||||
public void setStyleHeight(float height) {
|
||||
if (!valuesEqual(style.height, height)) {
|
||||
style.height = height;
|
||||
if (!valuesEqual(style.dimensions[DIMENSION_HEIGHT], height)) {
|
||||
style.dimensions[DIMENSION_HEIGHT] = height;
|
||||
dirty();
|
||||
}
|
||||
}
|
||||
|
||||
public float getLayoutX() {
|
||||
return layout.left;
|
||||
return layout.position[POSITION_LEFT];
|
||||
}
|
||||
|
||||
public float getLayoutY() {
|
||||
return layout.top;
|
||||
return layout.position[POSITION_TOP];
|
||||
}
|
||||
|
||||
public float getLayoutWidth() {
|
||||
return layout.width;
|
||||
return layout.dimensions[DIMENSION_WIDTH];
|
||||
}
|
||||
|
||||
public float getLayoutHeight() {
|
||||
return layout.height;
|
||||
return layout.dimensions[DIMENSION_HEIGHT];
|
||||
}
|
||||
|
||||
public CSSDirection getLayoutDirection() {
|
||||
@@ -365,14 +372,14 @@ public class CSSNode {
|
||||
* Get this node's width, as defined in the style.
|
||||
*/
|
||||
public float getStyleWidth() {
|
||||
return style.width;
|
||||
return style.dimensions[DIMENSION_WIDTH];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this node's height, as defined in the style.
|
||||
*/
|
||||
public float getStyleHeight() {
|
||||
return style.height;
|
||||
return style.dimensions[DIMENSION_HEIGHT];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -27,13 +27,17 @@ public class CSSStyle {
|
||||
public Spacing padding = new Spacing();
|
||||
public Spacing border = new Spacing();
|
||||
|
||||
public float positionTop = CSSConstants.UNDEFINED;
|
||||
public float positionBottom = CSSConstants.UNDEFINED;
|
||||
public float positionLeft = CSSConstants.UNDEFINED;
|
||||
public float positionRight = CSSConstants.UNDEFINED;
|
||||
public float[] position = {
|
||||
CSSConstants.UNDEFINED,
|
||||
CSSConstants.UNDEFINED,
|
||||
CSSConstants.UNDEFINED,
|
||||
CSSConstants.UNDEFINED,
|
||||
};
|
||||
|
||||
public float width = CSSConstants.UNDEFINED;
|
||||
public float height = CSSConstants.UNDEFINED;
|
||||
public float[] dimensions = {
|
||||
CSSConstants.UNDEFINED,
|
||||
CSSConstants.UNDEFINED,
|
||||
};
|
||||
|
||||
public float minWidth = CSSConstants.UNDEFINED;
|
||||
public float minHeight = CSSConstants.UNDEFINED;
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user