From e7b0470f5506dcb37753ec4465ca3bb14260163a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 26 Oct 2019 21:14:43 -0400 Subject: [PATCH 1/5] CCharLayoutInfo: Use heterogenous lookup with std::map Allows using std::string_view with std::map without the need to construct a completely new std::string instance. Making the lookups via GetSegIdFromString() completely non-allocating. Notably, this also allows it to properly function with non-null-terminated string_view instances. --- Runtime/Character/CCharLayoutInfo.cpp | 6 ++++-- Runtime/Character/CCharLayoutInfo.hpp | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Runtime/Character/CCharLayoutInfo.cpp b/Runtime/Character/CCharLayoutInfo.cpp index 2e0a8b610..3273e6917 100644 --- a/Runtime/Character/CCharLayoutInfo.cpp +++ b/Runtime/Character/CCharLayoutInfo.cpp @@ -19,9 +19,11 @@ zeus::CVector3f CCharLayoutInfo::GetFromRootUnrotated(const CSegId& id) const { } CSegId CCharLayoutInfo::GetSegIdFromString(std::string_view name) const { - auto it = x18_segIdMap.find(name.data()); - if (it == x18_segIdMap.end()) + const auto it = x18_segIdMap.find(name); + + if (it == x18_segIdMap.cend()) { return {}; + } return it->second; } diff --git a/Runtime/Character/CCharLayoutInfo.hpp b/Runtime/Character/CCharLayoutInfo.hpp index 9ec09cf3c..7e0ee2172 100644 --- a/Runtime/Character/CCharLayoutInfo.hpp +++ b/Runtime/Character/CCharLayoutInfo.hpp @@ -35,7 +35,7 @@ public: class CCharLayoutInfo { std::shared_ptr x0_node; CSegIdList x8_segIdList; - std::map x18_segIdMap; + std::map> x18_segIdMap; public: CCharLayoutInfo(CInputStream& in); From 293d19cf137d6f3c28dff446b3c6dab8bfac5c05 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 26 Oct 2019 21:19:54 -0400 Subject: [PATCH 2/5] CCharLayoutInfo: std::move std::string instance in CCharLayoutInfo constructor Avoids performing a copy (and by extension also avoids redundant allocations). --- Runtime/Character/CCharLayoutInfo.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Runtime/Character/CCharLayoutInfo.cpp b/Runtime/Character/CCharLayoutInfo.cpp index 3273e6917..29c69173f 100644 --- a/Runtime/Character/CCharLayoutInfo.cpp +++ b/Runtime/Character/CCharLayoutInfo.cpp @@ -48,10 +48,11 @@ CCharLayoutNode::CCharLayoutNode(CInputStream& in) : x0_boneMap(in.readUint32Big } CCharLayoutInfo::CCharLayoutInfo(CInputStream& in) : x0_node(std::make_shared(in)), x8_segIdList(in) { - atUint32 mapCount = in.readUint32Big(); + const atUint32 mapCount = in.readUint32Big(); + for (atUint32 i = 0; i < mapCount; ++i) { std::string key = in.readString(); - x18_segIdMap.emplace(key, in); + x18_segIdMap.emplace(std::move(key), in); } } From 9193f4c4d7e89bab3778baf888f5936d720881f9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 26 Oct 2019 21:21:55 -0400 Subject: [PATCH 3/5] CCharLayoutInfo: Make use of const where applicable Makes trivial variables const to make it explicit that they aren't modified. --- Runtime/Character/CCharLayoutInfo.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Runtime/Character/CCharLayoutInfo.cpp b/Runtime/Character/CCharLayoutInfo.cpp index 29c69173f..a13eea4f5 100644 --- a/Runtime/Character/CCharLayoutInfo.cpp +++ b/Runtime/Character/CCharLayoutInfo.cpp @@ -32,16 +32,18 @@ void CCharLayoutNode::Bone::read(CInputStream& in) { x0_parentId = CSegId(in); x4_origin.readBig(in); - u32 chCount = in.readUint32Big(); + const u32 chCount = in.readUint32Big(); x10_children.reserve(chCount); - for (u32 i = 0; i < chCount; ++i) + for (u32 i = 0; i < chCount; ++i) { x10_children.emplace_back(in); + } } CCharLayoutNode::CCharLayoutNode(CInputStream& in) : x0_boneMap(in.readUint32Big()) { - u32 cap = x0_boneMap.GetCapacity(); + const u32 cap = x0_boneMap.GetCapacity(); + for (u32 i = 0; i < cap; ++i) { - u32 thisId = in.readUint32Big(); + const u32 thisId = in.readUint32Big(); Bone& bone = x0_boneMap[thisId]; bone.read(in); } From bb9da9687067d6a639cc7b5f4510b439567b0118 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 26 Oct 2019 21:24:08 -0400 Subject: [PATCH 4/5] CCharLayoutInfo: Invert conditional within GetFromParentUnrotated() Inverts the conditional to be in terms of the true case, making it a little nicer to read. --- Runtime/Character/CCharLayoutInfo.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Runtime/Character/CCharLayoutInfo.cpp b/Runtime/Character/CCharLayoutInfo.cpp index a13eea4f5..5bf284922 100644 --- a/Runtime/Character/CCharLayoutInfo.cpp +++ b/Runtime/Character/CCharLayoutInfo.cpp @@ -5,11 +5,12 @@ namespace urde { zeus::CVector3f CCharLayoutInfo::GetFromParentUnrotated(const CSegId& id) const { const CCharLayoutNode::Bone& bone = x0_node->GetBoneMap()[id]; - if (!x0_node->GetBoneMap().HasElement(bone.x0_parentId)) + + if (x0_node->GetBoneMap().HasElement(bone.x0_parentId)) { + const CCharLayoutNode::Bone& parent = x0_node->GetBoneMap()[bone.x0_parentId]; + return bone.x4_origin - parent.x4_origin; + } else { return bone.x4_origin; - else { - const CCharLayoutNode::Bone& pBone = x0_node->GetBoneMap()[bone.x0_parentId]; - return bone.x4_origin - pBone.x4_origin; } } From 69831b4a134eeb4aaa1bc5335b96609c9312801d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 26 Oct 2019 21:27:00 -0400 Subject: [PATCH 5/5] CCharLayoutInfo: Make constructors explicit Makes constructions of the types visibly explicit, given they aren't conversion constructors. --- Runtime/Character/CCharLayoutInfo.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Runtime/Character/CCharLayoutInfo.hpp b/Runtime/Character/CCharLayoutInfo.hpp index 7e0ee2172..663a0f0d3 100644 --- a/Runtime/Character/CCharLayoutInfo.hpp +++ b/Runtime/Character/CCharLayoutInfo.hpp @@ -28,7 +28,7 @@ private: TSegIdMap x0_boneMap; public: - CCharLayoutNode(CInputStream& in); + explicit CCharLayoutNode(CInputStream& in); const TSegIdMap& GetBoneMap() const { return x0_boneMap; } }; @@ -38,7 +38,7 @@ class CCharLayoutInfo { std::map> x18_segIdMap; public: - CCharLayoutInfo(CInputStream& in); + explicit CCharLayoutInfo(CInputStream& in); const std::shared_ptr& GetRootNode() const { return x0_node; } const CSegIdList& GetSegIdList() const { return x8_segIdList; } zeus::CVector3f GetFromParentUnrotated(const CSegId& id) const;