Don't use a static vector for sorted PATX settings

This commit is contained in:
Jonathan G Rennison
2023-11-17 22:51:52 +00:00
parent 9197cf6be1
commit 498c4161b8

View File

@@ -3210,30 +3210,24 @@ static void SaveSettings(const SettingTable &settings, void *object)
* N bytes setting field * N bytes setting field
*/ */
/** Sorted list of PATX settings, generated by MakeSettingsPatxList */
static std::vector<const SettingDesc *> _sorted_patx_settings;
/** /**
* Prepare a sorted list of settings to be potentially be loaded out of the PATX chunk * Prepare a sorted list of settings to be potentially be loaded out of the PATX chunk
* This is to enable efficient lookup of settings by name * This is to enable efficient lookup of settings by name
* This is stored in _sorted_patx_settings
*/ */
static void MakeSettingsPatxList(const SettingTable &settings) static std::vector<const SettingDesc *> MakeSettingsPatxList(const SettingTable &settings)
{ {
static const SettingTable *previous = nullptr; std::vector<const SettingDesc *> sorted_patx_settings;
if (&settings == previous) return;
previous = &settings;
_sorted_patx_settings.clear();
for (auto &sd : settings) { for (auto &sd : settings) {
if (sd->patx_name == nullptr) continue; if (sd->patx_name == nullptr) continue;
_sorted_patx_settings.push_back(sd.get()); sorted_patx_settings.push_back(sd.get());
} }
std::sort(_sorted_patx_settings.begin(), _sorted_patx_settings.end(), [](const SettingDesc *a, const SettingDesc *b) { std::sort(sorted_patx_settings.begin(), sorted_patx_settings.end(), [](const SettingDesc *a, const SettingDesc *b) {
return strcmp(a->patx_name, b->patx_name) < 0; return strcmp(a->patx_name, b->patx_name) < 0;
}); });
return sorted_patx_settings;
} }
/** /**
@@ -3274,7 +3268,7 @@ static const SaveLoad _settings_ext_save_desc[] = {
*/ */
static void LoadSettingsPatx(const SettingTable &settings, void *object) static void LoadSettingsPatx(const SettingTable &settings, void *object)
{ {
MakeSettingsPatxList(settings); std::vector<const SettingDesc *> sorted_patx_settings = MakeSettingsPatxList(settings);
SettingsExtLoad current_setting; SettingsExtLoad current_setting;
@@ -3291,14 +3285,14 @@ static void LoadSettingsPatx(const SettingTable &settings, void *object)
// now try to find corresponding setting // now try to find corresponding setting
bool exact_match = false; bool exact_match = false;
auto iter = std::lower_bound(_sorted_patx_settings.begin(), _sorted_patx_settings.end(), current_setting.name, [&](const SettingDesc *a, const char *b) { auto iter = std::lower_bound(sorted_patx_settings.begin(), sorted_patx_settings.end(), current_setting.name, [&](const SettingDesc *a, const char *b) {
int result = strcmp(a->patx_name, b); int result = strcmp(a->patx_name, b);
if (result == 0) exact_match = true; if (result == 0) exact_match = true;
return result < 0; return result < 0;
}); });
if (exact_match) { if (exact_match) {
assert(iter != _sorted_patx_settings.end()); assert(iter != sorted_patx_settings.end());
// found setting // found setting
const SettingDesc *setting = (*iter); const SettingDesc *setting = (*iter);
const SaveLoad &sld = setting->save; const SaveLoad &sld = setting->save;