NetKDRequest: Make use of constexpr arrays containing pairs instead of maps

Currently we were using heap allocating maps that last for the entire
duration of the emulator running.

Given the size N of both of these maps are very small (< 20 elements),
we can just make use of an array of pairs and perform linear scans. This
is also fine, given this code isn't particularly "hot" either, so this
won't be run often.
This commit is contained in:
Lioncash 2021-08-24 07:35:02 -04:00
parent b49bd76d91
commit e65363f05f
1 changed files with 27 additions and 12 deletions

View File

@ -3,9 +3,11 @@
#include "Core/IOS/Network/KD/NetKDRequest.h" #include "Core/IOS/Network/KD/NetKDRequest.h"
#include <algorithm>
#include <array> #include <array>
#include <map>
#include <string> #include <string>
#include <string_view>
#include <utility>
#include "Common/CommonPaths.h" #include "Common/CommonPaths.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -33,31 +35,44 @@ enum class HardwareModel : u8
Unknown = 7 Unknown = 7
}; };
u8 GetAreaCode(const std::string& area) u8 GetAreaCode(std::string_view area)
{ {
static const std::map<std::string, u8> regions = { static constexpr std::array<std::pair<std::string_view, u8>, 13> regions{{
{"JPN", 0}, {"USA", 1}, {"EUR", 2}, {"AUS", 2}, {"BRA", 1}, {"TWN", 3}, {"ROC", 3}, {"JPN", 0},
{"KOR", 4}, {"HKG", 5}, {"ASI", 5}, {"LTN", 1}, {"SAF", 2}, {"CHN", 6}, {"USA", 1},
}; {"EUR", 2},
{"AUS", 2},
{"BRA", 1},
{"TWN", 3},
{"ROC", 3},
{"KOR", 4},
{"HKG", 5},
{"ASI", 5},
{"LTN", 1},
{"SAF", 2},
{"CHN", 6},
}};
const auto entry_pos = regions.find(area); const auto entry_pos = std::find_if(regions.cbegin(), regions.cend(),
[&area](const auto& entry) { return entry.first == area; });
if (entry_pos != regions.end()) if (entry_pos != regions.end())
return entry_pos->second; return entry_pos->second;
return 7; // Unknown return 7; // Unknown
} }
HardwareModel GetHardwareModel(const std::string& model) HardwareModel GetHardwareModel(std::string_view model)
{ {
static const std::map<std::string, HardwareModel> models = { static constexpr std::array<std::pair<std::string_view, HardwareModel>, 4> models{{
{"RVL", HardwareModel::RVL}, {"RVL", HardwareModel::RVL},
{"RVT", HardwareModel::RVT}, {"RVT", HardwareModel::RVT},
{"RVV", HardwareModel::RVV}, {"RVV", HardwareModel::RVV},
{"RVD", HardwareModel::RVD}, {"RVD", HardwareModel::RVD},
}; }};
const auto entry_pos = models.find(model); const auto entry_pos = std::find_if(models.cbegin(), models.cend(),
if (entry_pos != models.end()) [&model](const auto& entry) { return entry.first == model; });
if (entry_pos != models.cend())
return entry_pos->second; return entry_pos->second;
return HardwareModel::Unknown; return HardwareModel::Unknown;