From 8a2e5f2e2cbd79cb3606e1553ea233b388782ac8 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Fri, 14 Jun 2024 21:55:29 -0300 Subject: [PATCH] perf: remove browseField on container destruction (#2677) Instead of checking all the time if there is a browsefield to be destroyed, why not remove it at the time of its destruction?! Besides, the linear search of phmap::flat_hash_map is very slow. --- src/game/game.cpp | 14 -------------- src/game/game.hpp | 1 - src/items/containers/container.cpp | 4 ++++ 3 files changed, 4 insertions(+), 15 deletions(-) diff --git a/src/game/game.cpp b/src/game/game.cpp index 57daddded2e..984e675407c 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -6255,7 +6255,6 @@ void Game::checkCreatureWalk(uint32_t creatureId) { const auto &creature = getCreatureByID(creatureId); if (creature && creature->getHealth() > 0) { creature->onCreatureWalk(); - cleanup(); } } @@ -6318,7 +6317,6 @@ void Game::checkCreatures() { --end; } } - cleanup(); index = (index + 1) % EVENT_CREATURECOUNT; } @@ -7967,8 +7965,6 @@ void Game::shutdown() { map.spawnsNpc.clear(); raids.clear(); - cleanup(); - if (serviceManager) { serviceManager->stop(); } @@ -7980,16 +7976,6 @@ void Game::shutdown() { g_logger().info("Done!"); } -void Game::cleanup() { - for (auto it = browseFields.begin(); it != browseFields.end();) { - if (it->second.expired()) { - it = browseFields.erase(it); - } else { - ++it; - } - } -} - void Game::addBestiaryList(uint16_t raceid, std::string name) { auto it = BestiaryList.find(raceid); if (it != BestiaryList.end()) { diff --git a/src/game/game.hpp b/src/game/game.hpp index b4be8facd09..2de00410e5e 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -426,7 +426,6 @@ class Game { void updatePlayerHelpers(std::shared_ptr player); - void cleanup(); void shutdown(); void dieSafely(const std::string &errorMsg); void addBestiaryList(uint16_t raceid, std::string name); diff --git a/src/items/containers/container.cpp b/src/items/containers/container.cpp index 63ce657741a..6c526a8500a 100644 --- a/src/items/containers/container.cpp +++ b/src/items/containers/container.cpp @@ -63,6 +63,10 @@ std::shared_ptr Container::create(std::shared_ptr tile) { Container::~Container() { if (getID() == ITEM_BROWSEFIELD) { + if (getParent() && getParent()->getTile()) { + g_game().browseFields.erase(getParent()->getTile()); + } + for (std::shared_ptr item : itemlist) { item->setParent(getParent()); }