enhance: optimize spawn cleanup removing redundant iteration.#2907
enhance: optimize spawn cleanup removing redundant iteration.#2907kaleohanopahala wants to merge 4 commits intoopentibiabr:mainfrom kaleohanopahala:main
Conversation
kaleohanopahala
commented
Sep 24, 2024
- Improved performance on large maps by avoiding double iteration.
- C++20 support.
- Removed redundant spawn time update.
|
Me pareceu uma boa, só não sei se retirar o OTSYS_TIME é padrão no OTBR. |
There was a problem hiding this comment.
Thanks for the contribution!
I'm curious about the reasoning behind removing the logic that updates the lastSpawn time in spawnMonsterMap. This update is crucial for tracking spawn intervals correctly. Was there a specific reason for omitting it in favor of using std::erase_if? It’s important to ensure that all necessary data remains accurate after cleanup.
Also, please consider using the pull request template provided. It really helps in giving more context and ensuring that all changes are properly reviewed and tested by other developers and contributors.
|
Hello! Sorry, I forgot to follow the pull request template. |
luan
left a comment
There was a problem hiding this comment.
Thanks for the optimization. I didn't quite follow your explanation about lastSpawn (in fact it seems maybe you misunderstood Eduardo's question), but that's ok.
We can remove setting .lastSpawn here because this isn't actually spawning those monsters, it's cleaning up removed ones, setting lastSpawn on removed monsters is not necessary, we set this on SpawnMonster::spawnMonster and that is sufficient.
This code here in fact wasn't accomplishing much other than potentially slowing down spawns of monsters which got removed.
|
@kaleohanopahala can you open the pull request again? But this time, in another branch? You opened it in your main (fork) and clang-format broke because of that, so I can't merge it |
|
I`ve tested this change, it makes the spawn time faster than the spawntime defined at spawn.xml. |
luan
left a comment
There was a problem hiding this comment.
I`ve tested this change, it makes the spawn time faster than the spawntime defined at spawn.xml. I recommend not to merge this change yet until we understand the actual spawn time.
@lBaah is right. This changes the behavior incorrectly, it makes the next monster spawn based on when it previously spawned, not when it died. I was wrong when I said this didn't do much, it's important and we cant skip the lastSpawn reset.
You can still do this in a single loop though.
|
I agree @luan, we can do it single loop, therefore the proposed change breaks spawn time. |
|
This way it should work correctly void SpawnMonster::cleanup() {
std::erase_if(spawnedMonsterMap, [&](const auto &pair) {
if (pair.second == nullptr || pair.second->isRemoved()) {
spawnMonsterMap[pair.first].lastSpawn = OTSYS_TIME();
return true;
}
return false;
});
}and can also be used in SpawnNpc::cleanup() |
|
edited |
This version 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.
