From 7cc791b978e0f7836f8cc90f5a1166ef28f474cc Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 20 Apr 2020 00:17:58 -0700 Subject: [PATCH] Fabric: `ComponentDescriptor::cloneProps()` now never returns the base props objects Summary: The diff changes how the `empty raw props` optimization works in `ComponentDescriptor::cloneProps()`. Now it only fires only when the base `props` object is null, which is practically all production cases we have (and care about). (I tried, in a normal run there were no cases where the empty raw props were passed with non-null props.) From the other side, the old behavior that may return the same props objects previously several times created bugs and practically unexpected results and practically disallowed to clone props objects easily. Changelog: [Internal] Fabric-specific internal change. Reviewed By: JoshuaGross Differential Revision: D21110608 fbshipit-source-id: 884807cd8e9c5c3e6cc1c9e4c1f0227259cc21fb --- .../core/componentdescriptor/ComponentDescriptor.h | 1 + .../componentdescriptor/ConcreteComponentDescriptor.h | 9 +++++++-- .../fabric/core/tests/ComponentDescriptorTest.cpp | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h b/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h index ea2952ce291..c9d2feb75af 100644 --- a/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h +++ b/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h @@ -98,6 +98,7 @@ class ComponentDescriptor { * `props` and `rawProps` applied on top of this. * If `props` is `nullptr`, a default `Props` object (with default values) * will be used. + * Must return an object which is NOT pointer equal to `props`. */ virtual SharedProps cloneProps( const SharedProps &props, diff --git a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h index 142e0dd2aad..7b7550447e6 100644 --- a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h +++ b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h @@ -108,8 +108,13 @@ class ConcreteComponentDescriptor : public ComponentDescriptor { dynamic_cast(props.get()) && "Provided `props` has an incompatible type."); - if (rawProps.isEmpty()) { - return props ? props : ShadowNodeT::defaultSharedProps(); + // Optimization: + // Quite often nodes are constructed with default/empty props: the base + // `props` object is `null` (there no base because it's not cloning) and the + // `rawProps` is empty. In this case, we can return the default props object + // of a concrete type entirely bypassing parsing. + if (!props && rawProps.isEmpty()) { + return ShadowNodeT::defaultSharedProps(); } rawProps.parse(rawPropsParser_); diff --git a/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp b/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp index 1dfc1878eff..58baadffb32 100644 --- a/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp +++ b/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp @@ -72,6 +72,9 @@ TEST(ComponentDescriptorTest, cloneShadowNode) { EXPECT_EQ(cloned->getTag(), 9); EXPECT_EQ(cloned->getSurfaceId(), 1); EXPECT_STREQ(cloned->getProps()->nativeId.c_str(), "abc"); + + auto clonedButSameProps = descriptor->cloneProps(props, RawProps()); + EXPECT_NE(clonedButSameProps, props); } TEST(ComponentDescriptorTest, appendChild) {