mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-28 20:25:33 +08:00
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:
committed by
Facebook Github Bot
parent
efd28bdc84
commit
c3ecae0db4
39
ReactCommon/better/small_vector.h
Normal file
39
ReactCommon/better/small_vector.h
Normal 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
|
||||
@@ -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>;
|
||||
|
||||
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user