From 2014650d5814022bc3c1277ccab849b6ea4b9ecc Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 30 Sep 2019 02:24:11 -0400 Subject: [PATCH 1/5] CToken: Mark CToken's move constructor noexcept The move constructor doesn't perform any behavior that would result in an exception being thrown. Marking it as noexcept allows the type to play nicely with facilities that make use of std::is_move_constructible to determine whether copies can be avoided or not in certain circumstances (e.g. the standard library; notably, std::vector). We can't mark the move assignment operator as noexcept currently, however, as it calls into interfaces outside of CToken. --- Runtime/CToken.cpp | 2 +- Runtime/CToken.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Runtime/CToken.cpp b/Runtime/CToken.cpp index d3cc11850..a9a47b681 100644 --- a/Runtime/CToken.cpp +++ b/Runtime/CToken.cpp @@ -128,7 +128,7 @@ CToken::CToken(const CToken& other) : x0_objRef(other.x0_objRef) { Lock(); } } -CToken::CToken(CToken&& other) : x0_objRef(other.x0_objRef), x4_lockHeld(other.x4_lockHeld) { +CToken::CToken(CToken&& other) noexcept : x0_objRef(other.x0_objRef), x4_lockHeld(other.x4_lockHeld) { other.x0_objRef = nullptr; other.x4_lockHeld = false; } diff --git a/Runtime/CToken.hpp b/Runtime/CToken.hpp index fc5ee6332..906786aa1 100644 --- a/Runtime/CToken.hpp +++ b/Runtime/CToken.hpp @@ -85,7 +85,7 @@ public: CToken& operator=(CToken&& other); CToken() = default; CToken(const CToken& other); - CToken(CToken&& other); + CToken(CToken&& other) noexcept; CToken(IObj* obj); CToken(std::unique_ptr&& obj); const SObjectTag* GetObjectTag() const; From 541adb02d2213e22247bf5de44664c2489d46324 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 30 Sep 2019 02:34:08 -0400 Subject: [PATCH 2/5] CToken: std::move buildParams within CObjectReference's constructor CVParamTransfer contains a std::shared_ptr, so a copy here performs an unnecessary reference count increment and decrement. We can std::move here to avoid this. --- Runtime/CToken.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/CToken.cpp b/Runtime/CToken.cpp index a9a47b681..a82937eaf 100644 --- a/Runtime/CToken.cpp +++ b/Runtime/CToken.cpp @@ -16,7 +16,7 @@ u16 CObjectReference::RemoveReference() { CObjectReference::CObjectReference(IObjectStore& objStore, std::unique_ptr&& obj, const SObjectTag& objTag, CVParamTransfer buildParams) -: x4_objTag(objTag), xC_objectStore(&objStore), x10_object(std::move(obj)), x14_params(buildParams) {} +: x4_objTag(objTag), xC_objectStore(&objStore), x10_object(std::move(obj)), x14_params(std::move(buildParams)) {} CObjectReference::CObjectReference(std::unique_ptr&& obj) : x10_object(std::move(obj)) {} void CObjectReference::Unlock() { From 026de4461194ffbefb0490bf111559aa5e6e3ca1 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 30 Sep 2019 02:46:31 -0400 Subject: [PATCH 3/5] IVParamObj: Explicitly supply copy and move constructor/assingnment operators Since the copy constructor is provided, we should explicitly specify the others to make behavior explicit (and prevent the copy constructor from being considered over the move constructor in certain scenarios). --- Runtime/IVParamObj.hpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Runtime/IVParamObj.hpp b/Runtime/IVParamObj.hpp index 70e0e817f..1be474932 100644 --- a/Runtime/IVParamObj.hpp +++ b/Runtime/IVParamObj.hpp @@ -24,9 +24,15 @@ class CVParamTransfer { std::shared_ptr m_ref; public: - CVParamTransfer() = default; + CVParamTransfer() noexcept = default; CVParamTransfer(IVParamObj* obj) : m_ref(obj) {} - CVParamTransfer(const CVParamTransfer& other) : m_ref(other.m_ref) {} + + CVParamTransfer(const CVParamTransfer& other) noexcept = default; + CVParamTransfer& operator=(const CVParamTransfer&) noexcept = default; + + CVParamTransfer(CVParamTransfer&&) noexcept = default; + CVParamTransfer& operator=(CVParamTransfer&&) noexcept = default; + IVParamObj* GetObj() const { return m_ref.get(); } CVParamTransfer ShareTransferRef() const { return CVParamTransfer(*this); } From cdf6361d6c8504fb0e5dbb62945c72de9a4b1799 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 30 Sep 2019 02:52:52 -0400 Subject: [PATCH 4/5] IVParamObj: Make rest of interface noexcept where applicable Allows a few of these member functions to be used within noexcept contexts. --- Runtime/IVParamObj.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Runtime/IVParamObj.hpp b/Runtime/IVParamObj.hpp index 1be474932..982dd2ae3 100644 --- a/Runtime/IVParamObj.hpp +++ b/Runtime/IVParamObj.hpp @@ -16,8 +16,8 @@ class TObjOwnerParam : public IVParamObj { public: TObjOwnerParam(T&& obj) : m_param(std::move(obj)) {} - T& GetParam() { return m_param; } - const T& GetParam() const { return m_param; } + T& GetParam() noexcept { return m_param; } + const T& GetParam() const noexcept { return m_param; } }; class CVParamTransfer { @@ -33,15 +33,15 @@ public: CVParamTransfer(CVParamTransfer&&) noexcept = default; CVParamTransfer& operator=(CVParamTransfer&&) noexcept = default; - IVParamObj* GetObj() const { return m_ref.get(); } - CVParamTransfer ShareTransferRef() const { return CVParamTransfer(*this); } + IVParamObj* GetObj() const noexcept { return m_ref.get(); } + CVParamTransfer ShareTransferRef() const noexcept { return CVParamTransfer(*this); } template - T& GetOwnedObj() const { + T& GetOwnedObj() const noexcept { return static_cast*>(GetObj())->GetParam(); } - static CVParamTransfer Null() { return CVParamTransfer(); } + static CVParamTransfer Null() noexcept { return CVParamTransfer(); } }; } // namespace urde From c17b268c579c511b1c1026188b691374c879116e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 30 Sep 2019 02:54:23 -0400 Subject: [PATCH 5/5] IVParamObj: Make default constructor of CVParamTransfer constexpr std::shared_ptr's default constructor is constexpr, so we can apply it to CVParamTransfer to allow using it within constexpr contexts, granting more flexibility to the type. --- Runtime/IVParamObj.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/IVParamObj.hpp b/Runtime/IVParamObj.hpp index 982dd2ae3..83d307883 100644 --- a/Runtime/IVParamObj.hpp +++ b/Runtime/IVParamObj.hpp @@ -24,7 +24,7 @@ class CVParamTransfer { std::shared_ptr m_ref; public: - CVParamTransfer() noexcept = default; + constexpr CVParamTransfer() noexcept = default; CVParamTransfer(IVParamObj* obj) : m_ref(obj) {} CVParamTransfer(const CVParamTransfer& other) noexcept = default;