Skip to content

Commit

Permalink
perf: optimize spawn cleanup removing redundant iteration (opentibiab…
Browse files Browse the repository at this point in the history
…r#2913)

This improves efficiency by avoiding an intermediate list for removal,
saving time and memory. The map is traversed only once, with iterators
updated after each removal, making the process more efficient.
Additionally, it eliminates redundant lookups by directly modifying the
map during iteration.
  • Loading branch information
kaleohanopahala authored and vllworldbuilding committed Oct 24, 2024
1 parent f1475c0 commit 180f236
Showing 1 changed file with 10 additions and 9 deletions.
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

0 comments on commit 180f236

Please sign in to comment.