Skip to content

Commit

Permalink
Avoid redundant copy of shared_ptr
Browse files Browse the repository at this point in the history
Summary:
changelog: [internal]

Making a copy of shared_ptr is order of magnitude more expensive than moving it. This diff avoids two redundant copies in commit phase.

Reviewed By: rubennorte, cipolleschi

Differential Revision: D43306245

fbshipit-source-id: cfa942cc67b1e5c91be47803b80f7c8cda2e32d8
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Feb 16, 2023
1 parent 1f151e0 commit 3218680
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
4 changes: 2 additions & 2 deletions ReactCommon/react/renderer/mounting/MountingCoordinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ SurfaceId MountingCoordinator::getSurfaceId() const {
return surfaceId_;
}

void MountingCoordinator::push(ShadowTreeRevision const &revision) const {
void MountingCoordinator::push(ShadowTreeRevision revision) const {
{
std::lock_guard<std::mutex> lock(mutex_);

react_native_assert(
!lastRevision_.has_value() || revision.number != lastRevision_->number);

if (!lastRevision_.has_value() || lastRevision_->number < revision.number) {
lastRevision_ = revision;
lastRevision_ = std::move(revision);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/react/renderer/mounting/MountingCoordinator.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class MountingCoordinator final {
private:
friend class ShadowTree;

void push(ShadowTreeRevision const &revision) const;
void push(ShadowTreeRevision revision) const;

/*
* Revokes the last pushed `ShadowTreeRevision`.
Expand Down
13 changes: 6 additions & 7 deletions ReactCommon/react/renderer/mounting/ShadowTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,16 +393,16 @@ CommitStatus ShadowTree::tryCommit(
telemetry.didCommit();
telemetry.setRevisionNumber(static_cast<int>(newRevisionNumber));

newRevision =
ShadowTreeRevision{newRootShadowNode, newRevisionNumber, telemetry};
newRevision = ShadowTreeRevision{
std::move(newRootShadowNode), newRevisionNumber, telemetry};

currentRevision_ = newRevision;
}

emitLayoutEvents(affectedLayoutableNodes);

if (commitMode == CommitMode::Normal) {
mount(newRevision, commitOptions.mountSynchronously);
mount(std::move(newRevision), commitOptions.mountSynchronously);
}

return CommitStatus::Succeeded;
Expand All @@ -413,10 +413,9 @@ ShadowTreeRevision ShadowTree::getCurrentRevision() const {
return currentRevision_;
}

void ShadowTree::mount(
ShadowTreeRevision const &revision,
bool mountSynchronously) const {
mountingCoordinator_->push(revision);
void ShadowTree::mount(ShadowTreeRevision revision, bool mountSynchronously)
const {
mountingCoordinator_->push(std::move(revision));
delegate_.shadowTreeDidFinishTransaction(
mountingCoordinator_, mountSynchronously);
}
Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/react/renderer/mounting/ShadowTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class ShadowTree final {
private:
constexpr static ShadowTreeRevision::Number INITIAL_REVISION{0};

void mount(ShadowTreeRevision const &revision, bool mountSynchronously) const;
void mount(ShadowTreeRevision revision, bool mountSynchronously) const;

void emitLayoutEvents(
std::vector<LayoutableShadowNode const *> &affectedLayoutableNodes) const;
Expand Down

0 comments on commit 3218680

Please sign in to comment.