From a4137e941c293e094f6c417ca9cc6f01d1560476 Mon Sep 17 00:00:00 2001 From: "florent.teppe" Date: Sat, 21 Jan 2023 17:45:23 +0100 Subject: [PATCH] std::string_view can be used in map and unordered map This avoids some unecessary dynamic allocations. Also applies some review advice. --- apps/openmw/mwmechanics/aifollow.cpp | 4 +--- apps/openmw/mwworld/store.cpp | 8 +++----- apps/openmw/mwworld/store.hpp | 3 ++- apps/openmw/mwworld/worldimp.cpp | 2 +- apps/openmw/mwworld/worldmodel.cpp | 4 ++-- .../detournavigator/tilecachedrecastmeshmanager.cpp | 2 +- components/misc/strings/algorithm.hpp | 2 ++ 7 files changed, 12 insertions(+), 13 deletions(-) diff --git a/apps/openmw/mwmechanics/aifollow.cpp b/apps/openmw/mwmechanics/aifollow.cpp index 091b5ef0c6..12b19611e0 100644 --- a/apps/openmw/mwmechanics/aifollow.cpp +++ b/apps/openmw/mwmechanics/aifollow.cpp @@ -36,7 +36,6 @@ namespace MWMechanics , mX(x) , mY(y) , mZ(z) - , mCellId("") , mActive(false) , mFollowIndex(mFollowIndexCounter++) { @@ -67,7 +66,6 @@ namespace MWMechanics , mX(0) , mY(0) , mZ(0) - , mCellId("") , mActive(false) , mFollowIndex(mFollowIndexCounter++) { @@ -170,7 +168,7 @@ namespace MWMechanics { if (actor.getCell()->isExterior()) // Outside? { - if (mCellId == "") // No cell to travel to + if (mCellId.empty()) // No cell to travel to { mRemainingDuration = mDuration; return true; diff --git a/apps/openmw/mwworld/store.cpp b/apps/openmw/mwworld/store.cpp index 5a1e508cb1..dbc6b31f18 100644 --- a/apps/openmw/mwworld/store.cpp +++ b/apps/openmw/mwworld/store.cpp @@ -515,14 +515,13 @@ namespace MWWorld } const ESM::Cell* Store::search(std::string_view name) const { - const std::string nameString = std::string(name); - DynamicInt::const_iterator it = mInt.find(nameString); + DynamicInt::const_iterator it = mInt.find(name); if (it != mInt.end()) { return &(it->second); } - DynamicInt::const_iterator dit = mDynamicInt.find(nameString); + DynamicInt::const_iterator dit = mDynamicInt.find(name); if (dit != mDynamicInt.end()) { return &dit->second; @@ -815,8 +814,7 @@ namespace MWWorld } bool Store::erase(std::string_view name) { - const std::string nameString = std::string(name); - auto it = mDynamicInt.find(nameString); + auto it = mDynamicInt.find(name); if (it == mDynamicInt.end()) { diff --git a/apps/openmw/mwworld/store.hpp b/apps/openmw/mwworld/store.hpp index 5da7722aab..0b9ef56113 100644 --- a/apps/openmw/mwworld/store.hpp +++ b/apps/openmw/mwworld/store.hpp @@ -332,7 +332,8 @@ namespace MWWorld } }; - typedef std::unordered_map DynamicInt; + typedef std::unordered_map + DynamicInt; typedef std::map, ESM::Cell, DynamicExtCmp> DynamicExt; diff --git a/apps/openmw/mwworld/worldimp.cpp b/apps/openmw/mwworld/worldimp.cpp index 8e7d1405ac..938a3c3bcf 100644 --- a/apps/openmw/mwworld/worldimp.cpp +++ b/apps/openmw/mwworld/worldimp.cpp @@ -1399,7 +1399,7 @@ namespace MWWorld esmPos.pos[0] = traced.x(); esmPos.pos[1] = traced.y(); esmPos.pos[2] = traced.z(); - std::string_view cell = ""; + std::string_view cell; if (!actor.getCell()->isExterior()) cell = actor.getCell()->getCell()->mName; MWWorld::ActionTeleport(cell, esmPos, false).execute(actor); diff --git a/apps/openmw/mwworld/worldmodel.cpp b/apps/openmw/mwworld/worldmodel.cpp index 39fc960ce8..13e73415cc 100644 --- a/apps/openmw/mwworld/worldmodel.cpp +++ b/apps/openmw/mwworld/worldmodel.cpp @@ -64,7 +64,7 @@ MWWorld::CellStore* MWWorld::WorldModel::getCellStore(const ESM::Cell* cell) { if (cell->mData.mFlags & ESM::Cell::Interior) { - auto result = mInteriors.find(std::string(cell->mName)); + auto result = mInteriors.find(cell->mName); if (result == mInteriors.end()) result = mInteriors.emplace(cell->mName, CellStore(cell, mStore, mReaders)).first; @@ -198,7 +198,7 @@ MWWorld::CellStore* MWWorld::WorldModel::getExterior(int x, int y) MWWorld::CellStore* MWWorld::WorldModel::getInterior(std::string_view name) { - auto result = mInteriors.find(std::string(name)); + auto result = mInteriors.find(name); if (result == mInteriors.end()) { diff --git a/apps/openmw_test_suite/detournavigator/tilecachedrecastmeshmanager.cpp b/apps/openmw_test_suite/detournavigator/tilecachedrecastmeshmanager.cpp index 92799b6dd1..df695ec254 100644 --- a/apps/openmw_test_suite/detournavigator/tilecachedrecastmeshmanager.cpp +++ b/apps/openmw_test_suite/detournavigator/tilecachedrecastmeshmanager.cpp @@ -175,7 +175,7 @@ namespace .mEnd = TilePosition(1, 1), }; manager.setRange(range, nullptr); - manager.setWorldspace(mWorldspace, nullptr); + manager.setWorldspace("worldspace", nullptr); const btBoxShape boxShape(btVector3(20, 20, 100)); const btTransform transform( diff --git a/components/misc/strings/algorithm.hpp b/components/misc/strings/algorithm.hpp index 1e1eba30a9..885b9e872e 100644 --- a/components/misc/strings/algorithm.hpp +++ b/components/misc/strings/algorithm.hpp @@ -94,6 +94,8 @@ namespace Misc::StringUtils struct CiComp { + using is_transparent = void; + bool operator()(std::string_view left, std::string_view right) const { return ciLess(left, right); } };