Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: optimize spawn cleanup removing redundant iteration #2913

Merged
merged 13 commits into from
Oct 14, 2024
Merged
19 changes: 10 additions & 9 deletions src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,17 @@ void SpawnMonster::scheduleSpawn(uint32_t spawnMonsterId, spawnBlock_t &sb, cons
}

void SpawnMonster::cleanup() {
std::vector<uint32_t> removeList;
for (const auto &[spawnMonsterId, monster] : spawnedMonsterMap) {
if (monster == nullptr || monster->isRemoved()) {
removeList.push_back(spawnMonsterId);
std::erase_if(spawnedMonsterMap, [this](const auto &entry) {
const auto &monster = entry.second;
if (!monster || monster->isRemoved()) {
auto spawnIt = spawnMonsterMap.find(entry.first);
if (spawnIt != spawnMonsterMap.end()) {
spawnIt->second.lastSpawn = OTSYS_TIME();
}
return true;
}
}
for (const auto &spawnMonsterId : removeList) {
spawnMonsterMap[spawnMonsterId].lastSpawn = OTSYS_TIME();
spawnedMonsterMap.erase(spawnMonsterId);
}
return false;
});
}

bool SpawnMonster::addMonster(const std::string &name, const Position &pos, Direction dir, uint32_t scheduleInterval, uint32_t weight /*= 1*/) {
Expand Down
Loading