1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-07 12:54:00 +00:00

Store key by reference in tiles map

This commit is contained in:
elsid 2019-03-10 20:28:33 +03:00
parent 8dab6e0f6c
commit 9d61c49478
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40
2 changed files with 20 additions and 6 deletions

View File

@ -85,7 +85,7 @@ namespace DetourNavigator
if (navMeshSize > mFreeNavMeshDataSize + (mMaxNavMeshDataSize - mUsedNavMeshDataSize)) if (navMeshSize > mFreeNavMeshDataSize + (mMaxNavMeshDataSize - mUsedNavMeshDataSize))
return Value(); return Value();
const auto navMeshKey = makeNavMeshKey(recastMesh, offMeshConnections); auto navMeshKey = makeNavMeshKey(recastMesh, offMeshConnections);
const auto itemSize = navMeshSize + 2 * navMeshKey.size(); const auto itemSize = navMeshSize + 2 * navMeshKey.size();
if (itemSize > mFreeNavMeshDataSize + (mMaxNavMeshDataSize - mUsedNavMeshDataSize)) if (itemSize > mFreeNavMeshDataSize + (mMaxNavMeshDataSize - mUsedNavMeshDataSize))
@ -94,9 +94,8 @@ namespace DetourNavigator
while (!mFreeItems.empty() && mUsedNavMeshDataSize + itemSize > mMaxNavMeshDataSize) while (!mFreeItems.empty() && mUsedNavMeshDataSize + itemSize > mMaxNavMeshDataSize)
removeLeastRecentlyUsed(); removeLeastRecentlyUsed();
const auto iterator = mFreeItems.emplace(mFreeItems.end(), agentHalfExtents, changedTile, navMeshKey); const auto iterator = mFreeItems.emplace(mFreeItems.end(), agentHalfExtents, changedTile, std::move(navMeshKey));
// TODO: use std::string_view or some alternative to avoid navMeshKey copy into both mFreeItems and mValues const auto emplaced = mValues[agentHalfExtents][changedTile].mMap.emplace(iterator->mNavMeshKey, iterator);
const auto emplaced = mValues[agentHalfExtents][changedTile].mMap.emplace(navMeshKey, iterator);
if (!emplaced.second) if (!emplaced.second)
{ {
@ -131,9 +130,10 @@ namespace DetourNavigator
mUsedNavMeshDataSize -= getSize(item); mUsedNavMeshDataSize -= getSize(item);
mFreeNavMeshDataSize -= getSize(item); mFreeNavMeshDataSize -= getSize(item);
mFreeItems.pop_back();
tileValues->second.mMap.erase(value); tileValues->second.mMap.erase(value);
mFreeItems.pop_back();
if (!tileValues->second.mMap.empty()) if (!tileValues->second.mMap.empty())
return; return;

View File

@ -105,10 +105,24 @@ namespace DetourNavigator
NavMeshData&& value); NavMeshData&& value);
private: private:
class KeyView
{
public:
KeyView(const std::string& value)
: mValue(value) {}
friend bool operator <(const KeyView& lhs, const KeyView& rhs)
{
return lhs.mValue.get() < rhs.mValue.get();
}
private:
std::reference_wrapper<const std::string> mValue;
};
struct TileMap struct TileMap
{ {
std::map<std::string, ItemIterator> mMap; std::map<KeyView, ItemIterator> mMap;
}; };
std::mutex mMutex; std::mutex mMutex;