From ed91cf9397cdf8d14c5a5823ee5155eca6cb5cd9 Mon Sep 17 00:00:00 2001 From: elsid Date: Thu, 27 May 2021 16:38:04 +0200 Subject: [PATCH] Replace unordered_map by map for storing objects For small amount of items it gives better performance for find by key for update. --- components/detournavigator/recastmeshmanager.cpp | 9 ++++----- components/detournavigator/recastmeshmanager.hpp | 3 +-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/components/detournavigator/recastmeshmanager.cpp b/components/detournavigator/recastmeshmanager.cpp index e168a33040..bfc0409a51 100644 --- a/components/detournavigator/recastmeshmanager.cpp +++ b/components/detournavigator/recastmeshmanager.cpp @@ -12,13 +12,12 @@ namespace DetourNavigator bool RecastMeshManager::addObject(const ObjectId id, const btCollisionShape& shape, const btTransform& transform, const AreaType areaType) { + const auto object = mObjects.lower_bound(id); + if (object != mObjects.end() && object->first == id) + return false; const auto iterator = mObjectsOrder.emplace(mObjectsOrder.end(), OscillatingRecastMeshObject(RecastMeshObject(shape, transform, areaType), mRevision + 1)); - if (!mObjects.emplace(id, iterator).second) - { - mObjectsOrder.erase(iterator); - return false; - } + mObjects.emplace_hint(object, id, iterator); ++mRevision; return true; } diff --git a/components/detournavigator/recastmeshmanager.hpp b/components/detournavigator/recastmeshmanager.hpp index e1606f0a95..5922821f26 100644 --- a/components/detournavigator/recastmeshmanager.hpp +++ b/components/detournavigator/recastmeshmanager.hpp @@ -13,7 +13,6 @@ #include #include #include -#include class btCollisionShape; @@ -67,7 +66,7 @@ namespace DetourNavigator RecastMeshBuilder mMeshBuilder; TileBounds mTileBounds; std::list mObjectsOrder; - std::unordered_map::iterator> mObjects; + std::map::iterator> mObjects; std::list mWaterOrder; std::map::iterator> mWater; std::optional mLastNavMeshReportedChange;