cfg: optimize saving config (#714)

## Link to GitHub Issue or related Pull Request, if one exists
#0

## Description of change
Save config xml once instead of ~33 times

## Testing
xml is still saved properly
This commit is contained in:
drmext
2026-05-29 16:09:02 +00:00
committed by GitHub
parent 89602cfcab
commit b558df3340
3 changed files with 19 additions and 5 deletions
+10 -3
View File
@@ -98,7 +98,7 @@ bool Config::getStatus() {
return this->status; return this->status;
} }
bool Config::addGame(Game &game) { bool Config::addGame(Game &game, bool save) {
tinyxml2::XMLNode *rootNode = this->configFile.LastChild(); tinyxml2::XMLNode *rootNode = this->configFile.LastChild();
tinyxml2::XMLElement *gameNodes = rootNode->FirstChildElement("game"); tinyxml2::XMLElement *gameNodes = rootNode->FirstChildElement("game");
@@ -486,13 +486,20 @@ bool Config::addGame(Game &game) {
rootNode->InsertEndChild(gameNode); rootNode->InsertEndChild(gameNode);
} }
// save config // save config (skipped when caller batches multiple addGame calls
this->saveConfigFile(); // and flushes once at the end via Config::save())
if (save) {
this->saveConfigFile();
}
// return success // return success
return true; return true;
} }
void Config::save() {
this->saveConfigFile();
}
bool Config::updateBinding(const Game &game, const Button &button, int alternative) { bool Config::updateBinding(const Game &game, const Button &button, int alternative) {
// get root node // get root node
+6 -1
View File
@@ -22,7 +22,12 @@ public:
bool getStatus(); bool getStatus();
bool createConfigFile(); bool createConfigFile();
bool addGame(Game &game); bool addGame(Game &game, bool save = true);
// flush pending in-memory changes to disk; intended to be paired with
// addGame(game, false) in bulk-init paths so we write the XML once instead
// of once per game
void save();
bool updateBinding(const Game &game, const Button &button, int alternative); bool updateBinding(const Game &game, const Button &button, int alternative);
bool updateBinding(const Game &game, const Analog &analog); bool updateBinding(const Game &game, const Analog &analog);
+3 -1
View File
@@ -154,12 +154,14 @@ namespace overlay::windows {
// add games to the config and window // add games to the config and window
auto &config = ::Config::getInstance(); auto &config = ::Config::getInstance();
for (auto &game : games_list) { for (auto &game : games_list) {
config.addGame(game); config.addGame(game, /* save */ false);
if (!config.getStatus()) { if (!config.getStatus()) {
log_warning("config", "failure adding game: {}", game.getGameName()); log_warning("config", "failure adding game: {}", game.getGameName());
} }
} }
// single flush in place of ~33 redundant writes during the loop above
config.save();
// read card numbers // read card numbers
read_card(); read_card();