Skip to content

Commit 01a3881

Browse files
astreetFacebook Github Bot
authored andcommitted
Lazy create children list when first child is added
Summary: @public We don't need to allocate a list for every node since leaf nodes don't have children. Reviewed By: emilsjolander Differential Revision: D4130818 fbshipit-source-id: 80d3e98fce9d2daa0676fd1cbed0e81edcf7adb3
1 parent 9c18960 commit 01a3881

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

java/com/facebook/csslayout/CSSNode.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ public CSSNode() {
6262
if (mNativePointer == 0) {
6363
throw new IllegalStateException("Failed to allocate native memory");
6464
}
65-
66-
mChildren = new ArrayList<>(4);
6765
}
6866

6967
private native void jni_CSSNodeFree(long nativePointer);
@@ -98,7 +96,7 @@ public void reset() {
9896

9997
@Override
10098
public int getChildCount() {
101-
return mChildren.size();
99+
return mChildren == null ? 0 : mChildren.size();
102100
}
103101

104102
@Override
@@ -113,6 +111,9 @@ public void addChildAt(CSSNode child, int i) {
113111
throw new IllegalStateException("Child already has a parent, it must be removed first.");
114112
}
115113

114+
if (mChildren == null) {
115+
mChildren = new ArrayList<>(4);
116+
}
116117
mChildren.add(i, child);
117118
child.mParent = this;
118119
jni_CSSNodeInsertChild(mNativePointer, child.mNativePointer, i);
@@ -136,7 +137,7 @@ CSSNode getParent() {
136137

137138
@Override
138139
public int indexOf(CSSNode child) {
139-
return mChildren.indexOf(child);
140+
return mChildren == null ? -1 : mChildren.indexOf(child);
140141
}
141142

142143
private native void jni_CSSNodeSetIsTextNode(long nativePointer, boolean isTextNode);

0 commit comments

Comments
 (0)