From 321868011b51597657b602902c07f2b02dec19ce Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Thu, 16 Feb 2023 13:42:26 -0800 Subject: [PATCH] Avoid redundant copy of shared_ptr 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 --- .../react/renderer/mounting/MountingCoordinator.cpp | 4 ++-- .../react/renderer/mounting/MountingCoordinator.h | 2 +- ReactCommon/react/renderer/mounting/ShadowTree.cpp | 13 ++++++------- ReactCommon/react/renderer/mounting/ShadowTree.h | 2 +- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/ReactCommon/react/renderer/mounting/MountingCoordinator.cpp b/ReactCommon/react/renderer/mounting/MountingCoordinator.cpp index bbd02b4cdc9..88242a74cb5 100644 --- a/ReactCommon/react/renderer/mounting/MountingCoordinator.cpp +++ b/ReactCommon/react/renderer/mounting/MountingCoordinator.cpp @@ -33,7 +33,7 @@ SurfaceId MountingCoordinator::getSurfaceId() const { return surfaceId_; } -void MountingCoordinator::push(ShadowTreeRevision const &revision) const { +void MountingCoordinator::push(ShadowTreeRevision revision) const { { std::lock_guard lock(mutex_); @@ -41,7 +41,7 @@ void MountingCoordinator::push(ShadowTreeRevision const &revision) const { !lastRevision_.has_value() || revision.number != lastRevision_->number); if (!lastRevision_.has_value() || lastRevision_->number < revision.number) { - lastRevision_ = revision; + lastRevision_ = std::move(revision); } } diff --git a/ReactCommon/react/renderer/mounting/MountingCoordinator.h b/ReactCommon/react/renderer/mounting/MountingCoordinator.h index d2685185660..188c6fb02b7 100644 --- a/ReactCommon/react/renderer/mounting/MountingCoordinator.h +++ b/ReactCommon/react/renderer/mounting/MountingCoordinator.h @@ -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`. diff --git a/ReactCommon/react/renderer/mounting/ShadowTree.cpp b/ReactCommon/react/renderer/mounting/ShadowTree.cpp index c1b66df770e..109cada60db 100644 --- a/ReactCommon/react/renderer/mounting/ShadowTree.cpp +++ b/ReactCommon/react/renderer/mounting/ShadowTree.cpp @@ -393,8 +393,8 @@ CommitStatus ShadowTree::tryCommit( telemetry.didCommit(); telemetry.setRevisionNumber(static_cast(newRevisionNumber)); - newRevision = - ShadowTreeRevision{newRootShadowNode, newRevisionNumber, telemetry}; + newRevision = ShadowTreeRevision{ + std::move(newRootShadowNode), newRevisionNumber, telemetry}; currentRevision_ = newRevision; } @@ -402,7 +402,7 @@ CommitStatus ShadowTree::tryCommit( emitLayoutEvents(affectedLayoutableNodes); if (commitMode == CommitMode::Normal) { - mount(newRevision, commitOptions.mountSynchronously); + mount(std::move(newRevision), commitOptions.mountSynchronously); } return CommitStatus::Succeeded; @@ -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); } diff --git a/ReactCommon/react/renderer/mounting/ShadowTree.h b/ReactCommon/react/renderer/mounting/ShadowTree.h index b750a1a2fcf..0e8d5f32486 100644 --- a/ReactCommon/react/renderer/mounting/ShadowTree.h +++ b/ReactCommon/react/renderer/mounting/ShadowTree.h @@ -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 &affectedLayoutableNodes) const;