Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <array>
#include <cfenv>
#include <cmath>
#include <deque>
#include <string_view>
#include <vector>

Expand Down Expand Up @@ -633,6 +634,7 @@ void FabricMountingManager::executeMount(
const EventEmitter* eventEmitter;
};
std::unordered_map<Tag, CreatedView> createdViews;
std::deque<ShadowView> preallocatedShadowViews;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a deque instead of a vector?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The items hold references to the copy, and a vector would move existing elements when it grows and leave those references dangling. dequeue never moves them.


{
std::lock_guard allocatedViewsLock(allocatedViewsMutex_);
Expand Down Expand Up @@ -802,12 +804,16 @@ void FabricMountingManager::executeMount(
: cppUpdatePropsMountItems)
.push_back(
CppMountItem::UpdatePropsMountItem(
{}, newChildShadowView));
CppMountItem::emptyShadowView(),
newChildShadowView));
} else if (auto& preallocatedProps =
allocatedView->second.preallocatedProps;
preallocatedProps != nullptr) {
if (preallocatedProps != newChildShadowView.props) {
auto preallocatedShadowView = newChildShadowView;
// Mount items hold references, so the copy must outlive
// them.
auto& preallocatedShadowView =
preallocatedShadowViews.emplace_back(newChildShadowView);
preallocatedShadowView.props = preallocatedProps;
(maintainMutationOrder ? cppCommonMountItems
: cppUpdatePropsMountItems)
Expand All @@ -825,7 +831,8 @@ void FabricMountingManager::executeMount(
: cppUpdatePropsMountItems)
.push_back(
CppMountItem::UpdatePropsMountItem(
{}, newChildShadowView));
CppMountItem::emptyShadowView(),
newChildShadowView));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class FabricMountingManager final {
*
* In the pull model (`synchronous` = true) the batch is
* applied immediately on the calling (UI) thread.
*
* The mount items built here reference the ShadowViews in `transaction`'s
* mutations instead of copying them. The caller must keep `transaction`
* alive and unmodified until the IntBufferBatchMountItem is created, which
* today happens before this call returns.
*/
void executeMount(const MountingTransaction &transaction, bool synchronous = false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@

namespace facebook::react {

const ShadowView& CppMountItem::emptyShadowView() {
static const ShadowView emptyShadowView{};
return emptyShadowView;
}

CppMountItem CppMountItem::CreateMountItem(const ShadowView& shadowView) {
return {
.type = CppMountItem::Type::Create,
.parentTag = {},
.oldChildShadowView = {},
.oldChildShadowView = emptyShadowView(),
.newChildShadowView = shadowView,
.index = -1};
}
Expand All @@ -22,7 +27,7 @@ CppMountItem CppMountItem::DeleteMountItem(const ShadowView& shadowView) {
.type = CppMountItem::Type::Delete,
.parentTag = {},
.oldChildShadowView = shadowView,
.newChildShadowView = {},
.newChildShadowView = emptyShadowView(),
.index = -1};
}
CppMountItem CppMountItem::InsertMountItem(
Expand All @@ -32,7 +37,7 @@ CppMountItem CppMountItem::InsertMountItem(
return {
.type = CppMountItem::Type::Insert,
.parentTag = parentTag,
.oldChildShadowView = {},
.oldChildShadowView = emptyShadowView(),
.newChildShadowView = shadowView,
.index = index};
}
Expand All @@ -44,7 +49,7 @@ CppMountItem CppMountItem::RemoveMountItem(
.type = CppMountItem::Type::Remove,
.parentTag = parentTag,
.oldChildShadowView = shadowView,
.newChildShadowView = {},
.newChildShadowView = emptyShadowView(),
.index = index};
}
CppMountItem CppMountItem::UpdatePropsMountItem(
Expand All @@ -61,7 +66,7 @@ CppMountItem CppMountItem::UpdateStateMountItem(const ShadowView& shadowView) {
return {
.type = CppMountItem::Type::UpdateState,
.parentTag = {},
.oldChildShadowView = {},
.oldChildShadowView = emptyShadowView(),
.newChildShadowView = shadowView,
.index = -1};
}
Expand All @@ -71,7 +76,7 @@ CppMountItem CppMountItem::UpdateLayoutMountItem(
return {
.type = CppMountItem::Type::UpdateLayout,
.parentTag = parentTag,
.oldChildShadowView = {},
.oldChildShadowView = emptyShadowView(),
.newChildShadowView = shadowView,
.index = -1};
}
Expand All @@ -80,7 +85,7 @@ CppMountItem CppMountItem::UpdateEventEmitterMountItem(
return {
.type = CppMountItem::Type::UpdateEventEmitter,
.parentTag = kNoTag,
.oldChildShadowView = {},
.oldChildShadowView = emptyShadowView(),
.newChildShadowView = shadowView,
.index = -1};
}
Expand All @@ -89,7 +94,7 @@ CppMountItem CppMountItem::UpdatePaddingMountItem(
return {
.type = CppMountItem::Type::UpdatePadding,
.parentTag = kNoTag,
.oldChildShadowView = {},
.oldChildShadowView = emptyShadowView(),
.newChildShadowView = shadowView,
.index = -1};
}
Expand All @@ -98,7 +103,7 @@ CppMountItem CppMountItem::UpdateOverflowInsetMountItem(
return {
.type = CppMountItem::Type::UpdateOverflowInset,
.parentTag = kNoTag,
.oldChildShadowView = {},
.oldChildShadowView = emptyShadowView(),
.newChildShadowView = shadowView,
.index = -1};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ struct CppMountItem final {

static CppMountItem UpdateOverflowInsetMountItem(const ShadowView &shadowView);

// Empty view for the side a mount item does not use.
static const ShadowView &emptyShadowView();

#pragma mark - Type

enum Type {
Expand All @@ -60,8 +63,12 @@ struct CppMountItem final {

Type type = {Create};
Tag parentTag = kNoTag;
ShadowView oldChildShadowView = {};
ShadowView newChildShadowView = {};
// Not owned: these point into a MountingTransaction's mutations, or at
// emptyShadowView(). The transaction must stay alive until the item is
// written into the JNI batch. Copying the views would cost three atomic
// reference count updates per view on creation and again on destruction.
const ShadowView &oldChildShadowView;
const ShadowView &newChildShadowView;
int index = {};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ TEST_F(MountItemTest, testCreateAndDeleteUseOppositeShadowViewSlots) {
EXPECT_EQ(-1, deleteItem.index);
}

TEST_F(MountItemTest, testItemsReferenceTheirShadowViews) {
const auto oldShadowView =
makeShadowView("View", /*surfaceId=*/11, /*tag=*/101);
const auto newShadowView =
makeShadowView("View", /*surfaceId=*/11, /*tag=*/101);

const auto updateItem =
CppMountItem::UpdatePropsMountItem(oldShadowView, newShadowView);
const auto createItem = CppMountItem::CreateMountItem(newShadowView);

EXPECT_EQ(&oldShadowView, &updateItem.oldChildShadowView);
EXPECT_EQ(&newShadowView, &updateItem.newChildShadowView);
EXPECT_EQ(&newShadowView, &createItem.newChildShadowView);
EXPECT_EQ(&CppMountItem::emptyShadowView(), &createItem.oldChildShadowView);
}

TEST_F(MountItemTest, testInsertAndRemovePreserveParentIndexAndChildSlot) {
constexpr Tag kParentTag = 501;
constexpr int kIndex = 3;
Expand Down
5 changes: 3 additions & 2 deletions scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -3216,11 +3216,12 @@ struct facebook::react::CornerRadii {
}

struct facebook::react::CppMountItem {
public const facebook::react::ShadowView& newChildShadowView;
public const facebook::react::ShadowView& oldChildShadowView;
public facebook::react::CppMountItem::Type type;
public facebook::react::ShadowView newChildShadowView;
public facebook::react::ShadowView oldChildShadowView;
public facebook::react::Tag parentTag;
public int index;
public static const facebook::react::ShadowView& emptyShadowView();
public static facebook::react::CppMountItem CreateMountItem(const facebook::react::ShadowView& shadowView);
public static facebook::react::CppMountItem DeleteMountItem(const facebook::react::ShadowView& shadowView);
public static facebook::react::CppMountItem InsertMountItem(facebook::react::Tag parentTag, const facebook::react::ShadowView& shadowView, int index);
Expand Down
5 changes: 3 additions & 2 deletions scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -6058,11 +6058,12 @@ struct facebook::react::CornerRadii {
}

struct facebook::react::CppMountItem {
public const facebook::react::ShadowView& newChildShadowView;
public const facebook::react::ShadowView& oldChildShadowView;
public facebook::react::CppMountItem::Type type;
public facebook::react::ShadowView newChildShadowView;
public facebook::react::ShadowView oldChildShadowView;
public facebook::react::Tag parentTag;
public int index;
public static const facebook::react::ShadowView& emptyShadowView();
public static facebook::react::CppMountItem CreateMountItem(const facebook::react::ShadowView& shadowView);
public static facebook::react::CppMountItem DeleteMountItem(const facebook::react::ShadowView& shadowView);
public static facebook::react::CppMountItem InsertMountItem(facebook::react::Tag parentTag, const facebook::react::ShadowView& shadowView, int index);
Expand Down
5 changes: 3 additions & 2 deletions scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -3209,11 +3209,12 @@ struct facebook::react::CornerRadii {
}

struct facebook::react::CppMountItem {
public const facebook::react::ShadowView& newChildShadowView;
public const facebook::react::ShadowView& oldChildShadowView;
public facebook::react::CppMountItem::Type type;
public facebook::react::ShadowView newChildShadowView;
public facebook::react::ShadowView oldChildShadowView;
public facebook::react::Tag parentTag;
public int index;
public static const facebook::react::ShadowView& emptyShadowView();
public static facebook::react::CppMountItem CreateMountItem(const facebook::react::ShadowView& shadowView);
public static facebook::react::CppMountItem DeleteMountItem(const facebook::react::ShadowView& shadowView);
public static facebook::react::CppMountItem InsertMountItem(facebook::react::Tag parentTag, const facebook::react::ShadowView& shadowView, int index);
Expand Down
5 changes: 3 additions & 2 deletions scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -3209,11 +3209,12 @@ struct facebook::react::CornerRadii {
}

struct facebook::react::CppMountItem {
public const facebook::react::ShadowView& newChildShadowView;
public const facebook::react::ShadowView& oldChildShadowView;
public facebook::react::CppMountItem::Type type;
public facebook::react::ShadowView newChildShadowView;
public facebook::react::ShadowView oldChildShadowView;
public facebook::react::Tag parentTag;
public int index;
public static const facebook::react::ShadowView& emptyShadowView();
public static facebook::react::CppMountItem CreateMountItem(const facebook::react::ShadowView& shadowView);
public static facebook::react::CppMountItem DeleteMountItem(const facebook::react::ShadowView& shadowView);
public static facebook::react::CppMountItem InsertMountItem(facebook::react::Tag parentTag, const facebook::react::ShadowView& shadowView, int index);
Expand Down
Loading