Skip to content

Commit

Permalink
feat: batch save for players and guilds
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Nov 7, 2024
1 parent d0ef23f commit baab58c
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/game/scheduling/save_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ void SaveManager::saveAll() {
logger.info("Saving server...");

Benchmark bm_players;
const auto &players = game.getPlayers();
const auto async = g_configManager().getBoolean(TOGGLE_SAVE_ASYNC);

const auto &players = std::vector<std::pair<uint32_t, std::shared_ptr<Player>>>(game.getPlayers().begin(), game.getPlayers().end());
logger.info("Saving {} players... (Async: {})", players.size(), async ? "Enabled" : "Disabled");
for (const auto &[_, player] : players) {

threadPool.submit_loop(static_cast<int>(0), static_cast<int>(players.size()), [this, &players](const int i) {
const auto &player = players[i].second;
player->loginPosition = player->getPosition();
doSavePlayer(player);
}
}).wait();


double duration_players = bm_players.duration();
if (duration_players > 1000.0) {
Expand All @@ -51,10 +55,11 @@ void SaveManager::saveAll() {
}

Benchmark bm_guilds;
const auto &guilds = game.getGuilds();
for (const auto &[_, guild] : guilds) {
saveGuild(guild);
}
const auto &guilds = std::vector<std::pair<uint32_t, std::shared_ptr<Guild>>>(game.getGuilds().begin(), game.getGuilds().end());
threadPool.submit_loop(static_cast<int>(0), static_cast<int>(guilds.size()), [this, &guilds](const int i) {
saveGuild(guilds[i].second);
}).wait();

double duration_guilds = bm_guilds.duration();
if (duration_guilds > 1000.0) {
logger.info("Guilds saved in {:.2f} seconds.", duration_guilds / 1000.0);
Expand Down

0 comments on commit baab58c

Please sign in to comment.