From d2c86f98d10237d64c335fe47346d921fb62026b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 28 Feb 2020 04:49:04 -0500 Subject: [PATCH 1/4] CCharacterInfo: std::move vectors in CParticleResData's constructor Same behavior, but allows the caller to move into the constructor, potentially avoiding copies altogether. --- Runtime/Character/CCharacterInfo.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Runtime/Character/CCharacterInfo.hpp b/Runtime/Character/CCharacterInfo.hpp index b66431e52..b94acd251 100644 --- a/Runtime/Character/CCharacterInfo.hpp +++ b/Runtime/Character/CCharacterInfo.hpp @@ -23,9 +23,9 @@ public: std::vector x20_elsc; std::vector x30_elsc; CParticleResData(CInputStream& in, u16 tableCount); - CParticleResData(const std::vector& part, const std::vector& swhc, - const std::vector& elsc1, const std::vector& elsc2) - : x0_part(part), x10_swhc(swhc), x20_elsc(elsc1), x30_elsc(elsc2) {} + CParticleResData(std::vector part, std::vector swhc, std::vector elsc1, + std::vector elsc2) + : x0_part(std::move(part)), x10_swhc(std::move(swhc)), x20_elsc(std::move(elsc1)), x30_elsc(std::move(elsc2)) {} }; private: From 435a6ea1d482df810c5677fd5176966cb96c1b7f Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 28 Feb 2020 04:51:16 -0500 Subject: [PATCH 2/4] CCharacterInfo: Make use of emplace_back() where applicable We can construct these elements in place. While we're at it, we can mark the count variables at const to be explicit. --- Runtime/Character/CCharacterInfo.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Runtime/Character/CCharacterInfo.cpp b/Runtime/Character/CCharacterInfo.cpp index 87b5c14aa..757ac0a24 100644 --- a/Runtime/Character/CCharacterInfo.cpp +++ b/Runtime/Character/CCharacterInfo.cpp @@ -3,26 +3,30 @@ namespace urde { CCharacterInfo::CParticleResData::CParticleResData(CInputStream& in, u16 tableCount) { - u32 partCount = in.readUint32Big(); + const u32 partCount = in.readUint32Big(); x0_part.reserve(partCount); - for (u32 i = 0; i < partCount; ++i) - x0_part.push_back(in.readUint32Big()); + for (u32 i = 0; i < partCount; ++i) { + x0_part.emplace_back(in.readUint32Big()); + } - u32 swhcCount = in.readUint32Big(); + const u32 swhcCount = in.readUint32Big(); x10_swhc.reserve(swhcCount); - for (u32 i = 0; i < swhcCount; ++i) - x10_swhc.push_back(in.readUint32Big()); + for (u32 i = 0; i < swhcCount; ++i) { + x10_swhc.emplace_back(in.readUint32Big()); + } - u32 unkCount = in.readUint32Big(); + const u32 unkCount = in.readUint32Big(); x20_elsc.reserve(unkCount); - for (u32 i = 0; i < unkCount; ++i) - x20_elsc.push_back(in.readUint32Big()); + for (u32 i = 0; i < unkCount; ++i) { + x20_elsc.emplace_back(in.readUint32Big()); + } if (tableCount > 5) { - u32 elscCount = in.readUint32Big(); + const u32 elscCount = in.readUint32Big(); x30_elsc.reserve(elscCount); - for (u32 i = 0; i < elscCount; ++i) - x30_elsc.push_back(in.readUint32Big()); + for (u32 i = 0; i < elscCount; ++i) { + x30_elsc.emplace_back(in.readUint32Big()); + } } } From 63cf485f27eb04b7c5c5ec3b0d50905affe5e3dd Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 28 Feb 2020 04:55:05 -0500 Subject: [PATCH 3/4] CCharacterInfo: std::move std::string instance where applicable Avoids potentially reallocating strings where it's not necessary to do so. --- Runtime/Character/CCharacterInfo.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Runtime/Character/CCharacterInfo.cpp b/Runtime/Character/CCharacterInfo.cpp index 757ac0a24..afafb5760 100644 --- a/Runtime/Character/CCharacterInfo.cpp +++ b/Runtime/Character/CCharacterInfo.cpp @@ -32,13 +32,13 @@ CCharacterInfo::CParticleResData::CParticleResData(CInputStream& in, u16 tableCo static std::vector>> MakeAnimInfoVector(CInputStream& in) { std::vector>> ret; - u32 animInfoCount = in.readUint32Big(); + const u32 animInfoCount = in.readUint32Big(); ret.reserve(animInfoCount); for (u32 i = 0; i < animInfoCount; ++i) { - s32 idx = in.readInt32Big(); + const s32 idx = in.readInt32Big(); std::string a = in.readString(); std::string b = in.readString(); - ret.emplace_back(idx, std::make_pair(a, b)); + ret.emplace_back(idx, std::make_pair(std::move(a), std::move(b))); } return ret; } @@ -54,26 +54,27 @@ CCharacterInfo::CCharacterInfo(CInputStream& in) , x44_partRes(in, x0_tableCount) , x84_unk(in.readUint32Big()) { if (x0_tableCount > 1) { - u32 aabbCount = in.readUint32Big(); + const u32 aabbCount = in.readUint32Big(); x88_aabbs.reserve(aabbCount); for (u32 i = 0; i < aabbCount; ++i) { std::string name = in.readString(); - x88_aabbs.emplace_back(name, zeus::CAABox()); + x88_aabbs.emplace_back(std::move(name), zeus::CAABox()); x88_aabbs.back().second.readBoundingBoxBig(in); } } if (x0_tableCount > 2) { - u32 effectCount = in.readUint32Big(); + const u32 effectCount = in.readUint32Big(); x98_effects.reserve(effectCount); for (u32 i = 0; i < effectCount; ++i) { std::string name = in.readString(); - x98_effects.emplace_back(name, std::vector()); + x98_effects.emplace_back(std::move(name), std::vector()); std::vector& comps = x98_effects.back().second; - u32 compCount = in.readUint32Big(); + const u32 compCount = in.readUint32Big(); comps.reserve(compCount); - for (u32 j = 0; j < compCount; ++j) + for (u32 j = 0; j < compCount; ++j) { comps.emplace_back(in); + } } } @@ -83,10 +84,11 @@ CCharacterInfo::CCharacterInfo(CInputStream& in) } if (x0_tableCount > 4) { - u32 aidxCount = in.readUint32Big(); + const u32 aidxCount = in.readUint32Big(); xb0_animIdxs.reserve(aidxCount); - for (u32 i = 0; i < aidxCount; ++i) + for (u32 i = 0; i < aidxCount; ++i) { xb0_animIdxs.push_back(in.readInt32Big()); + } } } From 9524208cd271320bfe946b35a3a3782af186356a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 28 Feb 2020 04:56:41 -0500 Subject: [PATCH 4/4] CCharacterInfo: Remove unnecessary top-level const in return value of GetAnimationIndex() Same behavior, less code. --- Runtime/Character/CCharacterInfo.cpp | 2 +- Runtime/Character/CCharacterInfo.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Runtime/Character/CCharacterInfo.cpp b/Runtime/Character/CCharacterInfo.cpp index afafb5760..f5610f613 100644 --- a/Runtime/Character/CCharacterInfo.cpp +++ b/Runtime/Character/CCharacterInfo.cpp @@ -92,7 +92,7 @@ CCharacterInfo::CCharacterInfo(CInputStream& in) } } -const s32 CCharacterInfo::GetAnimationIndex(std::string_view name) const { +s32 CCharacterInfo::GetAnimationIndex(std::string_view name) const { for (const auto& pair : x20_animInfo) { if (pair.second.second == name) return pair.first; diff --git a/Runtime/Character/CCharacterInfo.hpp b/Runtime/Character/CCharacterInfo.hpp index b94acd251..b5e5a8d22 100644 --- a/Runtime/Character/CCharacterInfo.hpp +++ b/Runtime/Character/CCharacterInfo.hpp @@ -66,6 +66,6 @@ public: s32 GetAnimationIndex(s32 idx) const { return xb0_animIdxs.at(idx); } const CPASDatabase& GetPASDatabase() const { return x30_pasDatabase; } - const s32 GetAnimationIndex(std::string_view) const; + s32 GetAnimationIndex(std::string_view) const; }; } // namespace urde