Fabric: Using small_vector instead of regular vector in some hot code paths

Summary: The hope is that it will remove many unnececery allocations improving overal perfromance.

Reviewed By: mdvacca

Differential Revision: D14249198

fbshipit-source-id: f0442b3919ccead0582a3190dea0e33d517d85f6
This commit is contained in:
Valentin Shergin
2019-03-07 13:38:10 -08:00
committed by Facebook Github Bot
parent c3ecae0db4
commit 2862ef3a47
7 changed files with 27 additions and 11 deletions

View File

@@ -70,7 +70,7 @@ void AttributedString::prependAttributedString(
attributedString.fragments_.end());
}
const std::vector<Fragment> &AttributedString::getFragments() const {
const Fragments &AttributedString::getFragments() const {
return fragments_;
}

View File

@@ -44,7 +44,7 @@ class AttributedString : public Sealable, public DebugStringConvertible {
bool operator!=(const Fragment &rhs) const;
};
using Fragments = std::vector<Fragment>;
using Fragments = better::small_vector<Fragment, 1>;
/*
* Appends and prepends a `fragment` to the string.

View File

@@ -90,7 +90,7 @@ void YogaLayoutableShadowNode::appendChild(YogaLayoutableShadowNode *child) {
}
void YogaLayoutableShadowNode::setChildren(
std::vector<YogaLayoutableShadowNode *> children) {
YogaLayoutableShadowNode::UnsharedList children) {
yogaNode_.setChildren({});
for (const auto &child : children) {
appendChild(child);
@@ -139,9 +139,9 @@ void YogaLayoutableShadowNode::layoutChildren(LayoutContext layoutContext) {
}
}
std::vector<LayoutableShadowNode *>
LayoutableShadowNode::UnsharedList
YogaLayoutableShadowNode::getLayoutableChildNodes() const {
std::vector<LayoutableShadowNode *> yogaLayoutableChildNodes;
LayoutableShadowNode::UnsharedList yogaLayoutableChildNodes;
yogaLayoutableChildNodes.reserve(yogaNode_.getChildren().size());
for (const auto &childYogaNode : yogaNode_.getChildren()) {

View File

@@ -15,6 +15,7 @@
#include <react/components/view/YogaStylableProps.h>
#include <react/core/LayoutableShadowNode.h>
#include <react/core/Sealable.h>
#include <react/core/ShadowNode.h>
#include <react/debug/DebugStringConvertible.h>
namespace facebook {
@@ -24,6 +25,10 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode,
public virtual DebugStringConvertible,
public virtual Sealable {
public:
using UnsharedList = better::small_vector<
YogaLayoutableShadowNode *,
kShadowNodeChildrenSmallVectorSize>;
#pragma mark - Constructors
YogaLayoutableShadowNode();
@@ -51,7 +56,7 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode,
* instances. Complements `ShadowNode::setChildren(...)` functionality from
* Yoga perspective.
*/
void setChildren(std::vector<YogaLayoutableShadowNode *> children);
void setChildren(YogaLayoutableShadowNode::UnsharedList children);
/*
* Sets Yoga styles based on given `YogaStylableProps`.
@@ -75,7 +80,7 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode,
void layoutChildren(LayoutContext layoutContext) override;
std::vector<LayoutableShadowNode *> getLayoutableChildNodes() const override;
LayoutableShadowNode::UnsharedList getLayoutableChildNodes() const override;
protected:
/*

View File

@@ -12,8 +12,10 @@
#include <memory>
#include <vector>
#include <better/small_vector.h>
#include <react/core/LayoutMetrics.h>
#include <react/core/Sealable.h>
#include <react/core/ShadowNode.h>
#include <react/debug/DebugStringConvertible.h>
namespace facebook {
@@ -28,6 +30,9 @@ struct LayoutContext;
*/
class LayoutableShadowNode : public virtual Sealable {
public:
using UnsharedList = better::
small_vector<LayoutableShadowNode *, kShadowNodeChildrenSmallVectorSize>;
virtual ~LayoutableShadowNode() noexcept = default;
/*
@@ -97,7 +102,7 @@ class LayoutableShadowNode : public virtual Sealable {
/*
* Returns layoutable children to interate on.
*/
virtual std::vector<LayoutableShadowNode *> getLayoutableChildNodes()
virtual LayoutableShadowNode::UnsharedList getLayoutableChildNodes()
const = 0;
/*

View File

@@ -90,8 +90,12 @@ class ConcreteShadowNode : public ShadowNode {
* Returns subset of children that are inherited from `SpecificShadowNodeT`.
*/
template <typename SpecificShadowNodeT>
std::vector<SpecificShadowNodeT *> getChildrenSlice() const {
std::vector<SpecificShadowNodeT *> children;
better::
small_vector<SpecificShadowNodeT *, kShadowNodeChildrenSmallVectorSize>
getChildrenSlice() const {
better::
small_vector<SpecificShadowNodeT *, kShadowNodeChildrenSmallVectorSize>
children;
for (const auto &childShadowNode : getChildren()) {
auto specificChildShadowNode =
dynamic_cast<const SpecificShadowNodeT *>(childShadowNode.get());

View File

@@ -5,6 +5,7 @@
#pragma once
#include <better/small_vector.h>
#include <folly/Hash.h>
#include <react/core/EventEmitter.h>
#include <react/core/LayoutMetrics.h>
@@ -59,7 +60,8 @@ struct ShadowViewNodePair final {
bool operator!=(const ShadowViewNodePair &rhs) const;
};
using ShadowViewNodePairList = std::vector<ShadowViewNodePair>;
using ShadowViewNodePairList = better::
small_vector<ShadowViewNodePair, kShadowNodeChildrenSmallVectorSize>;
} // namespace react
} // namespace facebook