From 213faa669521521a6fa137629abb312e6fa1036a Mon Sep 17 00:00:00 2001 From: Bo Svensson <90132211+bosvensson1@users.noreply.github.com> Date: Tue, 2 Nov 2021 13:46:41 +0000 Subject: [PATCH] restores countRecords optimisations (#3210) With this PR we restore @elsid 's optimisations of countRecords we have unintentionally discarded in PR #3197. In addition, we give it a more appropriate name and add comments concerning its peculiar background. --- apps/openmw/mwworld/esmstore.cpp | 12 +++++++++--- apps/openmw/mwworld/esmstore.hpp | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/apps/openmw/mwworld/esmstore.cpp b/apps/openmw/mwworld/esmstore.cpp index 3cedcd457a..5ec95ecf47 100644 --- a/apps/openmw/mwworld/esmstore.cpp +++ b/apps/openmw/mwworld/esmstore.cpp @@ -29,6 +29,7 @@ namespace void readRefs(const ESM::Cell& cell, std::vector& refs, std::vector& refIDs, std::vector& readers) { + // TODO: we have many similar copies of this code. for (size_t i = 0; i < cell.mContextList.size(); i++) { size_t index = cell.mContextList[i].index; @@ -301,12 +302,14 @@ void ESMStore::setUp(bool validateRecords) if (validateRecords) { validate(); - countRecords(); + countAllCellRefs(); } } -void ESMStore::countRecords() +void ESMStore::countAllCellRefs() { + // TODO: We currently need to read entire files here again. + // We should consider consolidating or deferring this reading. if(!mRefCount.empty()) return; std::vector refs; @@ -324,6 +327,8 @@ void ESMStore::countRecords() if (value.mRefID != deletedRefID) { std::string& refId = refIDs[value.mRefID]; + // We manually lower case IDs here for the time being to improve performance. + Misc::StringUtils::lowerCaseInPlace(refId); ++mRefCount[std::move(refId)]; } }; @@ -332,7 +337,8 @@ void ESMStore::countRecords() int ESMStore::getRefCount(const std::string& id) const { - auto it = mRefCount.find(id); + const std::string lowerId = Misc::StringUtils::lowerCase(id); + auto it = mRefCount.find(lowerId); if(it == mRefCount.end()) return 0; return it->second; diff --git a/apps/openmw/mwworld/esmstore.hpp b/apps/openmw/mwworld/esmstore.hpp index d1a4942cd3..8582a1daca 100644 --- a/apps/openmw/mwworld/esmstore.hpp +++ b/apps/openmw/mwworld/esmstore.hpp @@ -79,7 +79,7 @@ namespace MWWorld IDMap mIds; IDMap mStaticIds; - IDMap mRefCount; + std::unordered_map mRefCount; std::map mStores; @@ -90,7 +90,7 @@ namespace MWWorld /// Validate entries in store after setup void validate(); - void countRecords(); + void countAllCellRefs(); template void removeMissingObjects(Store& store);