CAiFuncMap: Store std::string_view instances instead of std::string

We only make use of static string entries, so we can make use of
std::string view to elide several memory allocations.
This commit is contained in:
Lioncash 2020-05-08 16:33:29 -04:00
parent a9be7b45b2
commit a0c4b6b3ec
5 changed files with 18 additions and 18 deletions

View File

@ -78,9 +78,9 @@ void CAi::FluidFXThink(EFluidState state, CScriptWater& water, urde::CStateManag
water.GetFluidPlane().AddRipple(GetMass(), GetUniqueId(), center, GetVelocity(), water, mgr, pos); water.GetFluidPlane().AddRipple(GetMass(), GetUniqueId(), center, GetVelocity(), water, mgr, pos);
} }
CAiStateFunc CAi::GetStateFunc(const char* func) { return m_FuncMap->GetStateFunc(func); } CAiStateFunc CAi::GetStateFunc(std::string_view func) { return m_FuncMap->GetStateFunc(func); }
CAiTriggerFunc CAi::GetTrigerFunc(const char* func) { return m_FuncMap->GetTriggerFunc(func); } CAiTriggerFunc CAi::GetTriggerFunc(std::string_view func) { return m_FuncMap->GetTriggerFunc(func); }
const CStateMachine* CAi::GetStateMachine() const { return x2c8_stateMachine.GetObj(); } const CStateMachine* CAi::GetStateMachine() const { return x2c8_stateMachine.GetObj(); }
void CAi::CreateFuncLookup(CAiFuncMap* funcMap) { m_FuncMap = funcMap; } void CAi::CreateFuncLookup(CAiFuncMap* funcMap) { m_FuncMap = funcMap; }

View File

@ -36,8 +36,8 @@ public:
const CMaterialList& list, CAssetId fsm, const CActorParameters&, float f1, float f2); const CMaterialList& list, CAssetId fsm, const CActorParameters&, float f1, float f2);
static void CreateFuncLookup(CAiFuncMap* funcMap); static void CreateFuncLookup(CAiFuncMap* funcMap);
static CAiStateFunc GetStateFunc(const char* func); static CAiStateFunc GetStateFunc(std::string_view func);
static CAiTriggerFunc GetTrigerFunc(const char* func); static CAiTriggerFunc GetTriggerFunc(std::string_view func);
const CStateMachine* GetStateMachine() const; const CStateMachine* GetStateMachine() const;

View File

@ -137,7 +137,7 @@ CAiFuncMap::CAiFuncMap() {
CAi::CreateFuncLookup(this); CAi::CreateFuncLookup(this);
} }
CAiStateFunc CAiFuncMap::GetStateFunc(const char* func) const { CAiStateFunc CAiFuncMap::GetStateFunc(std::string_view func) const {
const auto iter = x0_stateFuncs.find(func); const auto iter = x0_stateFuncs.find(func);
if (iter == x0_stateFuncs.cend()) { if (iter == x0_stateFuncs.cend()) {
@ -147,7 +147,7 @@ CAiStateFunc CAiFuncMap::GetStateFunc(const char* func) const {
return iter->second; return iter->second;
} }
CAiTriggerFunc CAiFuncMap::GetTriggerFunc(const char* func) const { CAiTriggerFunc CAiFuncMap::GetTriggerFunc(std::string_view func) const {
const auto iter = x10_triggerFuncs.find(func); const auto iter = x10_triggerFuncs.find(func);
if (iter == x10_triggerFuncs.cend()) { if (iter == x10_triggerFuncs.cend()) {

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <string> #include <string_view>
#include <unordered_map> #include <unordered_map>
namespace urde { namespace urde {
@ -13,12 +13,12 @@ using CAiStateFunc = void (CAi::*)(CStateManager&, EStateMsg, float);
using CAiTriggerFunc = bool (CAi::*)(CStateManager&, float); using CAiTriggerFunc = bool (CAi::*)(CStateManager&, float);
class CAiFuncMap { class CAiFuncMap {
std::unordered_map<std::string, CAiStateFunc> x0_stateFuncs; std::unordered_map<std::string_view, CAiStateFunc> x0_stateFuncs;
std::unordered_map<std::string, CAiTriggerFunc> x10_triggerFuncs; std::unordered_map<std::string_view, CAiTriggerFunc> x10_triggerFuncs;
public: public:
CAiFuncMap(); CAiFuncMap();
CAiStateFunc GetStateFunc(const char*) const; CAiStateFunc GetStateFunc(std::string_view func) const;
CAiTriggerFunc GetTriggerFunc(const char*) const; CAiTriggerFunc GetTriggerFunc(std::string_view func) const;
}; };
} // namespace urde } // namespace urde

View File

@ -14,7 +14,7 @@ CStateMachine::CStateMachine(CInputStream& in) {
for (u32 i = 0; i < stateCount; ++i) { for (u32 i = 0; i < stateCount; ++i) {
std::string name = in.readString(31, false); std::string name = in.readString(31, false);
CAiStateFunc func = CAi::GetStateFunc(name.c_str()); CAiStateFunc func = CAi::GetStateFunc(name);
x0_states.emplace_back(func, name.c_str()); x0_states.emplace_back(func, name.c_str());
} }
@ -28,14 +28,14 @@ CStateMachine::CStateMachine(CInputStream& in) {
x0_states[i].SetTriggers(firstTrig); x0_states[i].SetTriggers(firstTrig);
x10_triggers.resize(x10_triggers.size() + x0_states[i].GetNumTriggers()); x10_triggers.resize(x10_triggers.size() + x0_states[i].GetNumTriggers());
for (u32 j = 0; j < x0_states[i].GetNumTriggers(); ++j) { for (s32 j = 0; j < x0_states[i].GetNumTriggers(); ++j) {
u32 triggerCount = in.readUint32Big(); const u32 triggerCount = in.readUint32Big();
u32 lastTriggerIdx = triggerCount - 1; const u32 lastTriggerIdx = triggerCount - 1;
for (u32 k = 0; k < triggerCount; ++k) { for (u32 k = 0; k < triggerCount; ++k) {
std::string name = in.readString(31, false); std::string name = in.readString(31, false);
bool isNot = name.front() == '!'; const bool isNot = name.front() == '!';
CAiTriggerFunc func = CAi::GetTrigerFunc(isNot ? name.c_str() + 1 : name.c_str()); const CAiTriggerFunc func = CAi::GetTriggerFunc(isNot ? name.c_str() + 1 : name.c_str());
float arg = in.readFloatBig(); const float arg = in.readFloatBig();
CAiTrigger* newTrig; CAiTrigger* newTrig;
if (k < lastTriggerIdx) { if (k < lastTriggerIdx) {
newTrig = &x10_triggers.emplace_back(); newTrig = &x10_triggers.emplace_back();