Skip to content

Avoid copying the children list twice when cloning a path to the root - #58690

Open
shubh73 wants to merge 3 commits into
react:mainfrom
shubh73:perf/shadownode-clonetree-move
Open

shubh73 wants to merge 3 commits into
react:mainfrom
shubh73:perf/shadownode-clonetree-move

Conversation

@shubh73

@shubh73 shubh73 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

ShadowNode::cloneTree copies the parent's children into a local vector, swaps in the new child, then copies that local again into the std::make_shared passed to clone(). The second copy bumps and drops the refcount of every sibling at each level on the path to the root. The local is dead after the call, so this moves it instead. UIManager::updateShadowTree has the same double copy, and addAncestorsToUpdateList copied the whole children vector just to read one element. Output is unchanged.

This is a small cleanup rather than a meaningful perf win, since the parent clone itself does O(N) Yoga work per child. Host benchmark (clang -O3, Apple M4 Pro), cloneTree on one leaf of a View with N View siblings, median of 7 runs:

Children Before After Delta
10 1.68 µs 1.65 µs -1.7%
100 16.02 µs 15.76 µs -1.6%
1000 164.23 µs 160.84 µs -2.1%

At 10–100 children the difference is within noise.

Benchmark
#include <benchmark/benchmark.h>
#include <react/renderer/components/view/ViewComponentDescriptor.h>
#include <react/renderer/core/EventDispatcher.h>
#include <react/renderer/core/ShadowNode.h>
#include <react/utils/ContextContainer.h>
#include <memory>
#include <vector>

namespace facebook::react {

auto contextContainer = std::make_shared<const ContextContainer>();
auto eventDispatcher = std::shared_ptr<EventDispatcher>{nullptr};
auto viewComponentDescriptor =
    ViewComponentDescriptor{ComponentDescriptorParameters{
        .eventDispatcher = eventDispatcher,
        .contextContainer = contextContainer}};

std::shared_ptr<ShadowNode> createViewShadowNode(
    Tag tag,
    std::shared_ptr<const std::vector<std::shared_ptr<const ShadowNode>>>
        children) {
  auto family = viewComponentDescriptor.createFamily(
      {.tag = tag, .surfaceId = 1, .instanceHandle = nullptr});
  return viewComponentDescriptor.createShadowNode(
      {.props = ViewShadowNode::defaultSharedProps(),
       .children = std::move(children)},
      family);
}

// Clones the path from the root to a single leaf of a parent with
// `state.range(0)` children, as a state update on that leaf would.
static void cloneTreeWithSiblings(benchmark::State& state) {
  auto childCount = static_cast<Tag>(state.range(0));
  auto children =
      std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>();
  for (Tag tag = 0; tag < childCount; tag++) {
    children->push_back(createViewShadowNode(
        tag + 2, ShadowNode::emptySharedShadowNodeSharedList()));
  }
  auto leaf = children->at(childCount / 2);
  auto root = createViewShadowNode(1, children);

  for (auto _ : state) {
    auto newRoot = root->cloneTree(
        leaf->getFamily(),
        [](const ShadowNode& oldShadowNode) { return oldShadowNode.clone({}); });
    benchmark::DoNotOptimize(newRoot);
  }
}
BENCHMARK(cloneTreeWithSiblings)->Arg(10)->Arg(100)->Arg(1000);

} // namespace facebook::react

BENCHMARK_MAIN();

Changelog:

[GENERAL] [CHANGED] - Avoid a redundant copy of the children list in ShadowNode::cloneTree and UIManager::updateShadowTree

Test Plan:

Added cloneTree coverage to ShadowNodeTest. Built the renderer gtests on the host with CMake (Debug):

  • core/tests: 65/65 pass (the 4 Hermes-dependent files were not built)
  • mounting/tests StateReconciliationTest, StackingContextTest, OrderIndexTest: 21/21 pass

Not built for Android/iOS and not measured on a device.

🤖 Generated with Claude Code

`ShadowNode::cloneTree` and `UIManager::updateShadowTree` copy the parent's
children into a local vector, replace the updated child, and then copy that
local again into the `std::make_shared` passed to `clone`. The second copy
bumps and drops the refcount of every sibling. The local is dead after the
call, so move it instead. `addAncestorsToUpdateList` also copied the whole
children vector just to read a single element; take it by reference.

Added `cloneTree` coverage to `ShadowNodeTest`.

Changelog: [General][Changed] - Avoid a redundant copy of the children list in `ShadowNode::cloneTree` and `UIManager::updateShadowTree`

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 26, 2026
shubh73 and others added 2 commits September 26, 2026 13:30
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Sep 26, 2026
@meta-codesync

meta-codesync Bot commented Sep 27, 2026

Copy link
Copy Markdown

@fabriziocucci has imported this pull request. If you are a Meta employee, you can view this in D122059304.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant