Don't use a static vector for sorted PATX settings

pull/611/head
Jonathan G Rennison 7 months ago
parent 9197cf6be1
commit 498c4161b8

@ -3210,30 +3210,24 @@ static void SaveSettings(const SettingTable &settings, void *object)
* 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
* 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;
if (&settings == previous) return;
previous = &settings;
std::vector<const SettingDesc *> sorted_patx_settings;
_sorted_patx_settings.clear();
for (auto &sd : settings) {
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 sorted_patx_settings;
}
/**
@ -3274,7 +3268,7 @@ static const SaveLoad _settings_ext_save_desc[] = {
*/
static void LoadSettingsPatx(const SettingTable &settings, void *object)
{
MakeSettingsPatxList(settings);
std::vector<const SettingDesc *> sorted_patx_settings = MakeSettingsPatxList(settings);
SettingsExtLoad current_setting;
@ -3291,14 +3285,14 @@ static void LoadSettingsPatx(const SettingTable &settings, void *object)
// now try to find corresponding setting
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);
if (result == 0) exact_match = true;
return result < 0;
});
if (exact_match) {
assert(iter != _sorted_patx_settings.end());
assert(iter != sorted_patx_settings.end());
// found setting
const SettingDesc *setting = (*iter);
const SaveLoad &sld = setting->save;

Loading…
Cancel
Save