From 01a388142669804501385a70a98a56c4b1bd5f91 Mon Sep 17 00:00:00 2001 From: Andy Street Date: Mon, 7 Nov 2016 05:40:11 -0800 Subject: [PATCH] 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 --- java/com/facebook/csslayout/CSSNode.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/java/com/facebook/csslayout/CSSNode.java b/java/com/facebook/csslayout/CSSNode.java index f07ad7de..b928c212 100644 --- a/java/com/facebook/csslayout/CSSNode.java +++ b/java/com/facebook/csslayout/CSSNode.java @@ -62,8 +62,6 @@ public class CSSNode implements CSSNodeAPI { if (mNativePointer == 0) { throw new IllegalStateException("Failed to allocate native memory"); } - - mChildren = new ArrayList<>(4); } private native void jni_CSSNodeFree(long nativePointer); @@ -98,7 +96,7 @@ public class CSSNode implements CSSNodeAPI { @Override public int getChildCount() { - return mChildren.size(); + return mChildren == null ? 0 : mChildren.size(); } @Override @@ -113,6 +111,9 @@ public class CSSNode implements CSSNodeAPI { throw new IllegalStateException("Child already has a parent, it must be removed first."); } + if (mChildren == null) { + mChildren = new ArrayList<>(4); + } mChildren.add(i, child); child.mParent = this; jni_CSSNodeInsertChild(mNativePointer, child.mNativePointer, i); @@ -136,7 +137,7 @@ public class CSSNode implements CSSNodeAPI { @Override public int indexOf(CSSNode child) { - return mChildren.indexOf(child); + return mChildren == null ? -1 : mChildren.indexOf(child); } private native void jni_CSSNodeSetIsTextNode(long nativePointer, boolean isTextNode);