|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <benchmark/benchmark.h> |
| 9 | +#include <react/renderer/components/view/ViewComponentDescriptor.h> |
| 10 | +#include <react/renderer/core/EventDispatcher.h> |
| 11 | +#include <react/renderer/core/ShadowNode.h> |
| 12 | +#include <react/renderer/core/ShadowNodeFragment.h> |
| 13 | +#include <react/utils/ContextContainer.h> |
| 14 | + |
| 15 | +namespace facebook::react { |
| 16 | + |
| 17 | +using ShadowNodeList = std::vector<std::shared_ptr<const ShadowNode>>; |
| 18 | +using SharedShadowNodeList = std::shared_ptr<const ShadowNodeList>; |
| 19 | + |
| 20 | +static auto makeViewComponentDescriptor() { |
| 21 | + auto contextContainer = std::make_shared<const ContextContainer>(); |
| 22 | + auto eventDispatcher = std::shared_ptr<EventDispatcher>{nullptr}; |
| 23 | + return ViewComponentDescriptor{ComponentDescriptorParameters{ |
| 24 | + .eventDispatcher = eventDispatcher, |
| 25 | + .contextContainer = contextContainer, |
| 26 | + .flavor = nullptr}}; |
| 27 | +} |
| 28 | + |
| 29 | +static auto createViewShadowNode( |
| 30 | + ViewComponentDescriptor& descriptor, |
| 31 | + Tag tag, |
| 32 | + SurfaceId surfaceId, |
| 33 | + const ShadowNodeList& children = {}) { |
| 34 | + auto family = descriptor.createFamily( |
| 35 | + ShadowNodeFamilyFragment{ |
| 36 | + .tag = tag, |
| 37 | + .surfaceId = surfaceId, |
| 38 | + .instanceHandle = nullptr, |
| 39 | + }); |
| 40 | + |
| 41 | + SharedShadowNodeList childrenPtr = children.empty() |
| 42 | + ? ShadowNode::emptySharedShadowNodeSharedList() |
| 43 | + : std::make_shared<const ShadowNodeList>(children); |
| 44 | + |
| 45 | + return descriptor.createShadowNode( |
| 46 | + ShadowNodeFragment{ |
| 47 | + .props = ViewShadowNode::defaultSharedProps(), |
| 48 | + .children = childrenPtr, |
| 49 | + }, |
| 50 | + family); |
| 51 | +} // NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks) |
| 52 | + |
| 53 | +static void cloneShadowNodeNoChanges(benchmark::State& state) { |
| 54 | + auto descriptor = makeViewComponentDescriptor(); |
| 55 | + auto node = createViewShadowNode(descriptor, 1, 1); |
| 56 | + |
| 57 | + for ([[maybe_unused]] auto _ : state) { |
| 58 | + auto cloned = descriptor.cloneShadowNode(*node, ShadowNodeFragment{}); |
| 59 | + benchmark::DoNotOptimize(cloned); |
| 60 | + } |
| 61 | +} |
| 62 | +BENCHMARK(cloneShadowNodeNoChanges); |
| 63 | + |
| 64 | +static void cloneShadowNodeWithNewProps(benchmark::State& state) { |
| 65 | + auto descriptor = makeViewComponentDescriptor(); |
| 66 | + auto node = createViewShadowNode(descriptor, 1, 1); |
| 67 | + auto newProps = std::make_shared<const ViewShadowNodeProps>(); |
| 68 | + |
| 69 | + for ([[maybe_unused]] auto _ : state) { |
| 70 | + auto cloned = descriptor.cloneShadowNode( |
| 71 | + *node, |
| 72 | + ShadowNodeFragment{ |
| 73 | + .props = newProps, |
| 74 | + }); |
| 75 | + benchmark::DoNotOptimize(cloned); |
| 76 | + } |
| 77 | +} |
| 78 | +BENCHMARK(cloneShadowNodeWithNewProps); |
| 79 | + |
| 80 | +static void cloneShadowNodeWithChildren(benchmark::State& state) { |
| 81 | + auto descriptor = makeViewComponentDescriptor(); |
| 82 | + |
| 83 | + ShadowNodeList children; |
| 84 | + for (int i = 0; i < 10; ++i) { |
| 85 | + children.push_back(createViewShadowNode(descriptor, 100 + i, 1)); |
| 86 | + } |
| 87 | + auto parent = createViewShadowNode(descriptor, 1, 1, children); |
| 88 | + |
| 89 | + for ([[maybe_unused]] auto _ : state) { |
| 90 | + auto cloned = descriptor.cloneShadowNode(*parent, ShadowNodeFragment{}); |
| 91 | + benchmark::DoNotOptimize(cloned); |
| 92 | + } |
| 93 | +} |
| 94 | +BENCHMARK(cloneShadowNodeWithChildren); |
| 95 | + |
| 96 | +static void cloneShadowNodeWithNewChildren(benchmark::State& state) { |
| 97 | + auto descriptor = makeViewComponentDescriptor(); |
| 98 | + |
| 99 | + ShadowNodeList children; |
| 100 | + for (int i = 0; i < 10; ++i) { |
| 101 | + children.push_back(createViewShadowNode(descriptor, 100 + i, 1)); |
| 102 | + } |
| 103 | + auto parent = createViewShadowNode(descriptor, 1, 1, children); |
| 104 | + |
| 105 | + ShadowNodeList newChildren; |
| 106 | + for (int i = 0; i < 10; ++i) { |
| 107 | + newChildren.push_back(createViewShadowNode(descriptor, 200 + i, 1)); |
| 108 | + } |
| 109 | + auto newChildrenPtr = std::make_shared<const ShadowNodeList>(newChildren); |
| 110 | + |
| 111 | + for ([[maybe_unused]] auto _ : state) { |
| 112 | + auto cloned = descriptor.cloneShadowNode( |
| 113 | + *parent, |
| 114 | + ShadowNodeFragment{ |
| 115 | + .children = newChildrenPtr, |
| 116 | + }); |
| 117 | + benchmark::DoNotOptimize(cloned); |
| 118 | + } |
| 119 | +} |
| 120 | +BENCHMARK(cloneShadowNodeWithNewChildren); |
| 121 | + |
| 122 | +static void cloneShadowNodeDeepHierarchy(benchmark::State& state) { |
| 123 | + auto descriptor = makeViewComponentDescriptor(); |
| 124 | + |
| 125 | + std::shared_ptr<ShadowNode> deepest = createViewShadowNode(descriptor, 50, 1); |
| 126 | + for (int i = 49; i >= 1; --i) { |
| 127 | + ShadowNodeList singleChild = {deepest}; |
| 128 | + deepest = createViewShadowNode(descriptor, i, 1, singleChild); |
| 129 | + } |
| 130 | + |
| 131 | + for ([[maybe_unused]] auto _ : state) { |
| 132 | + auto cloned = descriptor.cloneShadowNode(*deepest, ShadowNodeFragment{}); |
| 133 | + benchmark::DoNotOptimize(cloned); |
| 134 | + } |
| 135 | +} |
| 136 | +BENCHMARK(cloneShadowNodeDeepHierarchy); |
| 137 | + |
| 138 | +} // namespace facebook::react |
| 139 | + |
| 140 | +BENCHMARK_MAIN(); |
0 commit comments