BREAKING [react_native] Don't create CSSNodes for virtual shadow nodes

Summary:
Virtual shadow nodes (e.g. text) don't use CSSNodes so we don't need to create them. This shows large savings in CSSNodes allocated, depending on the app.

This could be breaking if:
- You have virtual nodes that still set and get CSS properties. The setters now no-op for virtual nodes (I unfortunately couldn't remove them completely -- see the comment on LayoutShadowNode), but the getters will NPE. If you see these NPE's, you should almost definitely be using your own datastructure instead of a CSSNode as virtual nodes will not participate in the layout process (and the CSSNode is then behaving just as a POJO for you).

I do not anticipate this to be breaking for anyone, but am including breaking in the commit message since this is a change in API contract.

Reviewed By: emilsjolander

Differential Revision: D4220204

fbshipit-source-id: b8dc083fff420eb94180f669dd49389136111ecb
This commit is contained in:
Andy Street
2016-11-23 05:13:28 -08:00
committed by Facebook Github Bot
parent eaccd7e82e
commit 68c6d71cea
8 changed files with 139 additions and 36 deletions
@@ -77,11 +77,15 @@ public class ReactShadowNode {
private final CSSNode mCSSNode;
public ReactShadowNode() {
CSSNode node = CSSNodePool.get().acquire();
if (node == null) {
node = new CSSNode();
if (!isVirtual()) {
CSSNode node = CSSNodePool.get().acquire();
if (node == null) {
node = new CSSNode();
}
mCSSNode = node;
} else {
mCSSNode = null;
}
mCSSNode = node;
}
/**
@@ -108,7 +112,7 @@ public class ReactShadowNode {
}
public final boolean hasUpdates() {
return mNodeUpdated || hasNewLayout() || mCSSNode.isDirty();
return mNodeUpdated || hasNewLayout() || isDirty();
}
public final void markUpdateSeen() {
@@ -139,6 +143,10 @@ public class ReactShadowNode {
}
}
public final boolean isDirty() {
return mCSSNode != null && mCSSNode.isDirty();
}
public void addChildAt(ReactShadowNode child, int i) {
if (child.mParent != null) {
throw new IllegalViewOperationException(
@@ -152,8 +160,13 @@ public class ReactShadowNode {
// If a CSS node has measure defined, the layout algorithm will not visit its children. Even
// more, it asserts that you don't add children to nodes with measure functions.
if (!mCSSNode.isMeasureDefined()) {
mCSSNode.addChildAt(child.mCSSNode, i);
if (mCSSNode != null && !mCSSNode.isMeasureDefined()) {
CSSNode childCSSNode = child.mCSSNode;
if (childCSSNode == null) {
throw new RuntimeException(
"Cannot add a child that doesn't have a CSS node to a node without a measure function!");
}
mCSSNode.addChildAt(childCSSNode, i);
}
markUpdated();
@@ -171,7 +184,7 @@ public class ReactShadowNode {
ReactShadowNode removed = mChildren.remove(i);
removed.mParent = null;
if (!mCSSNode.isMeasureDefined()) {
if (mCSSNode != null && !mCSSNode.isMeasureDefined()) {
mCSSNode.removeChildAt(i);
}
markUpdated();
@@ -205,7 +218,7 @@ public class ReactShadowNode {
int decrease = 0;
for (int i = getChildCount() - 1; i >= 0; i--) {
if (!mCSSNode.isMeasureDefined()) {
if (mCSSNode != null && !mCSSNode.isMeasureDefined()) {
mCSSNode.removeChildAt(i);
}
ReactShadowNode toRemove = getChildAt(i);
@@ -344,11 +357,13 @@ public class ReactShadowNode {
}
public final boolean hasNewLayout() {
return mCSSNode.hasNewLayout();
return mCSSNode == null ? false : mCSSNode.hasNewLayout();
}
public final void markLayoutSeen() {
mCSSNode.markLayoutSeen();
if (mCSSNode != null) {
mCSSNode.markLayoutSeen();
}
}
/**
@@ -666,7 +681,9 @@ public class ReactShadowNode {
}
public void dispose() {
mCSSNode.reset();
CSSNodePool.get().release(mCSSNode);
if (mCSSNode != null) {
mCSSNode.reset();
CSSNodePool.get().release(mCSSNode);
}
}
}