CPlayerState: Make g_typeNameMap a compile-time array

While useful as a map, this causes an allocation that occurs during
program start and lasts until the program terminates, even if the
relevant code is never executed.

Given the total number of elements is quite small, we can convert this
into a compile-time array and perform the lookup with that instead.
While we're at it, we can eliminate an unnecessary second lookup into
said array by reusing the iterator from the first find.
This commit is contained in:
Lioncash 2020-03-21 00:57:03 -04:00
parent 36ac0a8d78
commit 83c9af9153
2 changed files with 58 additions and 56 deletions

View File

@ -389,7 +389,8 @@ void CPlayerState::InitializeScanTimes() {
u32 CPlayerState::GetPowerUpMaxValue(EItemType type) { return PowerUpMaxValues[size_t(type)]; } u32 CPlayerState::GetPowerUpMaxValue(EItemType type) { return PowerUpMaxValues[size_t(type)]; }
const std::unordered_map<std::string_view, CPlayerState::EItemType> CPlayerState::g_TypeNameMap = { CPlayerState::EItemType CPlayerState::ItemNameToType(std::string_view name) {
static constexpr std::array<std::pair<std::string_view, EItemType>, 46> typeNameMap{{
{"powerbeam"sv, EItemType::PowerBeam}, {"powerbeam"sv, EItemType::PowerBeam},
{"icebeam"sv, EItemType::IceBeam}, {"icebeam"sv, EItemType::IceBeam},
{"wavebeam"sv, EItemType::WaveBeam}, {"wavebeam"sv, EItemType::WaveBeam},
@ -436,15 +437,18 @@ const std::unordered_map<std::string_view, CPlayerState::EItemType> CPlayerState
{"world"sv, EItemType::World}, {"world"sv, EItemType::World},
{"spirit"sv, EItemType::Spirit}, {"spirit"sv, EItemType::Spirit},
{"newborn"sv, EItemType::Newborn}, {"newborn"sv, EItemType::Newborn},
}; }};
CPlayerState::EItemType CPlayerState::ItemNameToType(std::string_view name) { std::string lowName{name};
std::string lowName = name.data();
athena::utility::tolower(lowName); athena::utility::tolower(lowName);
if (g_TypeNameMap.find(lowName) == g_TypeNameMap.end())
return EItemType::Invalid;
return g_TypeNameMap.find(lowName)->second; const auto iter = std::find_if(typeNameMap.cbegin(), typeNameMap.cend(),
[&lowName](const auto& entry) { return entry.first == lowName; });
if (iter == typeNameMap.cend()) {
return EItemType::Invalid;
}
return iter->second;
} }
} // namespace urde } // namespace urde

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <string_view> #include <string_view>
#include <unordered_map>
#include <vector> #include <vector>
#include "Runtime/CStaticInterference.hpp" #include "Runtime/CStaticInterference.hpp"
@ -89,7 +88,6 @@ public:
enum class EBeamId : s32 { Invalid = -1, Power, Ice, Wave, Plasma, Phazon, Phazon2 = 27 }; enum class EBeamId : s32 { Invalid = -1, Power, Ice, Wave, Plasma, Phazon, Phazon2 = 27 };
private: private:
static const std::unordered_map<std::string_view, EItemType> g_TypeNameMap;
struct CPowerUp { struct CPowerUp {
u32 x0_amount = 0; u32 x0_amount = 0;
u32 x4_capacity = 0; u32 x4_capacity = 0;