Fabric: Using small_vector for SharedShadowNodeList

Summary:
SharedShadowNodeList is one of the core collections in Fabric. Every ShadowNode has one, it's used intensivly during diffing and so on.
Usually, nodes have a very few children, so using `small_vector` instead of regular `vector` should save us a lot of memory allocations.

Reviewed By: JoshuaGross

Differential Revision: D14249201

fbshipit-source-id: 53297caa027a74099d0a1ed4a3cce78f8feb651b
This commit is contained in:
Valentin Shergin
2019-03-07 13:38:09 -08:00
committed by Facebook Github Bot
parent efd28bdc84
commit c3ecae0db4
3 changed files with 48 additions and 5 deletions

View File

@@ -0,0 +1,39 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <better/better.h>
// `folly::small_vector` is broken on some versions of Android.
#if defined(BETTER_USE_FOLLY_CONTAINERS) && !defined(ANDROID)
#include <folly/small_vector.h>
#else
#include <vector>
#endif
namespace facebook {
namespace better {
#if defined(BETTER_USE_FOLLY_CONTAINERS) && !defined(ANDROID)
template <typename T, std::size_t Size, typename... Ts>
using small_vector = folly::small_vector<T, Size, Ts...>;
#else
template <typename T, std::size_t Size, typename... Ts>
using small_vector = std::vector<T, Ts...>;
#endif
} // namespace better
} // namespace facebook

View File

@@ -11,6 +11,7 @@
#include <string>
#include <vector>
#include <better/small_vector.h>
#include <react/core/EventEmitter.h>
#include <react/core/LocalData.h>
#include <react/core/Props.h>
@@ -22,6 +23,8 @@
namespace facebook {
namespace react {
static constexpr const int kShadowNodeChildrenSmallVectorSize = 8;
class ComponentDescriptor;
struct ShadowNodeFragment;
@@ -30,7 +33,8 @@ class ShadowNode;
using SharedShadowNode = std::shared_ptr<const ShadowNode>;
using WeakShadowNode = std::weak_ptr<const ShadowNode>;
using UnsharedShadowNode = std::shared_ptr<ShadowNode>;
using SharedShadowNodeList = std::vector<SharedShadowNode>;
using SharedShadowNodeList =
better::small_vector<SharedShadowNode, kShadowNodeChildrenSmallVectorSize>;
using SharedShadowNodeSharedList = std::shared_ptr<const SharedShadowNodeList>;
using SharedShadowNodeUnsharedList = std::shared_ptr<SharedShadowNodeList>;

View File

@@ -267,8 +267,8 @@ TEST(ShadowNodeTest, handleBacktracking) {
},
componentDescriptor);
auto nodeABChildren = std::make_shared<std::vector<SharedShadowNode>>(
std::vector<SharedShadowNode>{nodeABA, nodeABB, nodeABC});
auto nodeABChildren = std::make_shared<SharedShadowNodeList>(
SharedShadowNodeList{nodeABA, nodeABB, nodeABC});
auto nodeAB = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
/* .tag = */ ShadowNodeFragment::tagPlaceholder(),
@@ -289,8 +289,8 @@ TEST(ShadowNodeTest, handleBacktracking) {
},
componentDescriptor);
auto nodeAChildren = std::make_shared<std::vector<SharedShadowNode>>(
std::vector<SharedShadowNode>{nodeAA, nodeAB, nodeAC});
auto nodeAChildren = std::make_shared<SharedShadowNodeList>(
SharedShadowNodeList{nodeAA, nodeAB, nodeAC});
auto nodeA = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
/* .tag = */ ShadowNodeFragment::tagPlaceholder(),