From e97e4d07ddd91bedf7ad10eea3dfd5eccc46ba38 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Thu, 6 May 2021 22:41:20 +0200 Subject: [PATCH 1/3] Teensy optimisation for esmtool - Use an unordered_set instead of a list to keep track of skipped records. - Reduce the number of conditions when parsing 4-letters records by using a switch-case instead of cascading conditions. - Add a const --- apps/esmtool/esmtool.cpp | 9 +++++---- apps/esmtool/record.cpp | 2 +- components/esm/esmcommon.hpp | 17 ++++++++--------- components/esm/esmreader.cpp | 2 +- components/esm/esmreader.hpp | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/apps/esmtool/esmtool.cpp b/apps/esmtool/esmtool.cpp index 579f5f67d..60483f981 100644 --- a/apps/esmtool/esmtool.cpp +++ b/apps/esmtool/esmtool.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -322,7 +323,7 @@ int load(Arguments& info) std::string filename = info.filename; std::cout << "Loading file: " << filename << std::endl; - std::list skipped; + std::unordered_set skipped; try { @@ -364,17 +365,17 @@ int load(Arguments& info) // Loop through all records while(esm.hasMoreRecs()) { - ESM::NAME n = esm.getRecName(); + const ESM::NAME n = esm.getRecName(); uint32_t flags; esm.getRecHeader(flags); EsmTool::RecordBase *record = EsmTool::RecordBase::create(n); if (record == nullptr) { - if (std::find(skipped.begin(), skipped.end(), n.intval) == skipped.end()) + if (skipped.count(n.intval) == 0) { std::cout << "Skipping " << n.toString() << " records." << std::endl; - skipped.push_back(n.intval); + skipped.emplace(n.intval); } esm.skipRecord(); diff --git a/apps/esmtool/record.cpp b/apps/esmtool/record.cpp index 3679184a6..55170e232 100644 --- a/apps/esmtool/record.cpp +++ b/apps/esmtool/record.cpp @@ -172,7 +172,7 @@ void printTransport(const std::vector& transport) namespace EsmTool { RecordBase * -RecordBase::create(ESM::NAME type) +RecordBase::create(const ESM::NAME type) { RecordBase *record = nullptr; diff --git a/components/esm/esmcommon.hpp b/components/esm/esmcommon.hpp index 232a24fcf..6087de83d 100644 --- a/components/esm/esmcommon.hpp +++ b/components/esm/esmcommon.hpp @@ -110,15 +110,14 @@ struct FIXED_STRING<4> : public FIXED_STRING_BASE void assign(const std::string& value) { intval = 0; - size_t length = value.size(); - if (length == 0) return; - data[0] = value[0]; - if (length == 1) return; - data[1] = value[1]; - if (length == 2) return; - data[2] = value[2]; - if (length == 3) return; - data[3] = value[3]; + switch(value.size()) { + case 4: data[3] = value[3]; + case 3: data[2] = value[2]; + case 2: data[1] = value[1]; + case 1: data[0] = value[0]; + default: break; + } + } char const* ro_data() const { return data; } diff --git a/components/esm/esmreader.cpp b/components/esm/esmreader.cpp index 4e7dce876..0ee47cab6 100644 --- a/components/esm/esmreader.cpp +++ b/components/esm/esmreader.cpp @@ -373,7 +373,7 @@ void ESMReader::setEncoder(ToUTF8::Utf8Encoder* encoder) mEncoder = encoder; } -size_t ESMReader::getFileOffset() +size_t ESMReader::getFileOffset() const { return mEsm->tellg(); } diff --git a/components/esm/esmreader.hpp b/components/esm/esmreader.hpp index 761756e8f..721c91802 100644 --- a/components/esm/esmreader.hpp +++ b/components/esm/esmreader.hpp @@ -73,7 +73,7 @@ public: void openRaw(const std::string &filename); /// Get the current position in the file. Make sure that the file has been opened! - size_t getFileOffset(); + size_t getFileOffset() const; // This is a quick hack for multiple esm/esp files. Each plugin introduces its own // terrain palette, but ESMReader does not pass a reference to the correct plugin From fbeccc2908d82fb646fb0beb8ed31b5a4396b9aa Mon Sep 17 00:00:00 2001 From: elsid Date: Fri, 7 May 2021 09:28:47 +0000 Subject: [PATCH 2/3] Apply 1 suggestion(s) to 1 file(s) --- components/esm/esmcommon.hpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/components/esm/esmcommon.hpp b/components/esm/esmcommon.hpp index 6087de83d..e4f3b636d 100644 --- a/components/esm/esmcommon.hpp +++ b/components/esm/esmcommon.hpp @@ -110,13 +110,7 @@ struct FIXED_STRING<4> : public FIXED_STRING_BASE void assign(const std::string& value) { intval = 0; - switch(value.size()) { - case 4: data[3] = value[3]; - case 3: data[2] = value[2]; - case 2: data[1] = value[1]; - case 1: data[0] = value[0]; - default: break; - } + std::memcpy(data, value.data(), std::min(value.size(), 4)); } From 8a56ba6aaaabfe6a553b37c1e6d518187c2e0ffb Mon Sep 17 00:00:00 2001 From: jvoisin Date: Fri, 7 May 2021 13:58:30 +0200 Subject: [PATCH 3/3] Fix compilation --- components/esm/esmcommon.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esm/esmcommon.hpp b/components/esm/esmcommon.hpp index e4f3b636d..bb4069d26 100644 --- a/components/esm/esmcommon.hpp +++ b/components/esm/esmcommon.hpp @@ -110,7 +110,7 @@ struct FIXED_STRING<4> : public FIXED_STRING_BASE void assign(const std::string& value) { intval = 0; - std::memcpy(data, value.data(), std::min(value.size(), 4)); + std::memcpy(data, value.data(), (value.size() < 4)? value.size(): 4); }