Skip to content

Commit

Permalink
Mark Paragraph as clean if it hasn't changed (#38749)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #38749

changelog: [internal]

# Request for comment

I'll gate this and extend this to TextInput if we think this is appropriate solution.

## Problem
Paragraph and TextInput have custom measure functions. They outsource measurements to the host platform. On Android, this means going through JNI (expensive).

When we architected YogaLayoutableShadowNode, we made intentional decision to dirty those nodes on every clone. This was to lower the complexity.

## Solution

One possible to solution is to just check if children, props or textAttributes have changed. If not, nothing has affected the layout and it is safe to mark the node as clean.

Reviewed By: NickGerleman

Differential Revision: D47914641

fbshipit-source-id: ab448fa18599eb2266984eba9f5d935caad1caed
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Aug 2, 2023
1 parent 95e7a74 commit 1f24750
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,7 @@ public class ReactFeatureFlags {

/** Only swap left and right on Android in RTL scripts. */
public static boolean doNotSwapLeftAndRightOnAndroidInLTR = false;

/** Clean yoga node when <Text /> does not change. */
public static boolean enableCleanParagraphYogaNode = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ void Binding::installFabricUIManager(
CoreFeatures::enableMapBuffer = getFeatureFlagValue("useMapBufferProps");
CoreFeatures::doNotSwapLeftAndRightOnAndroidInLTR =
getFeatureFlagValue("doNotSwapLeftAndRightOnAndroidInLTR");
CoreFeatures::enableCleanParagraphYogaNode =
getFeatureFlagValue("enableCleanParagraphYogaNode");

// RemoveDelete mega-op
ShadowViewMutation::PlatformSupportsRemoveDeleteTreeInstruction =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <react/renderer/core/TraitCast.h>
#include <react/renderer/graphics/rounding.h>
#include <react/renderer/telemetry/TransactionTelemetry.h>
#include <react/utils/CoreFeatures.h>

#include "ParagraphState.h"

Expand All @@ -25,6 +26,20 @@ using Content = ParagraphShadowNode::Content;

char const ParagraphComponentName[] = "Paragraph";

ParagraphShadowNode::ParagraphShadowNode(
ShadowNode const &sourceShadowNode,
ShadowNodeFragment const &fragment)
: ConcreteViewShadowNode(sourceShadowNode, fragment) {
if (CoreFeatures::enableCleanParagraphYogaNode) {
if (!fragment.children && !fragment.props) {
// This ParagraphShadowNode was cloned but did not change
// in a way that affects its layout. Let's mark it clean
// to stop Yoga from traversing it.
cleanLayout();
}
}
}

Content const &ParagraphShadowNode::getContent(
LayoutContext const &layoutContext) const {
if (content_.has_value()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class ParagraphShadowNode final : public ConcreteViewShadowNode<
public:
using ConcreteViewShadowNode::ConcreteViewShadowNode;

ParagraphShadowNode(
ShadowNode const &sourceShadowNode,
ShadowNodeFragment const &fragment);

static ShadowNodeTraits BaseTraits() {
auto traits = ConcreteViewShadowNode::BaseTraits();
traits.set(ShadowNodeTraits::Trait::LeafYogaNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ bool CoreFeatures::cancelImageDownloadsOnRecycle = false;
bool CoreFeatures::enableGranularScrollViewStateUpdatesIOS = false;
bool CoreFeatures::enableMountHooks = false;
bool CoreFeatures::doNotSwapLeftAndRightOnAndroidInLTR = false;
bool CoreFeatures::enableCleanParagraphYogaNode = false;

} // namespace facebook::react
3 changes: 3 additions & 0 deletions packages/react-native/ReactCommon/react/utils/CoreFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class CoreFeatures {

// Only swap left and right on Android in RTL scripts.
static bool doNotSwapLeftAndRightOnAndroidInLTR;

// Clean yoga node when <Text /> does not change.
static bool enableCleanParagraphYogaNode;
};

} // namespace facebook::react

0 comments on commit 1f24750

Please sign in to comment.