mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
Use boost::unordered_flat_map in wide strings cache
This improves sorting performance by 7%. The change also revealed an iterator invalidation bug in the previous implementation, so now it inserts both strings into the cache before getting their wide values.
This commit is contained in:
@@ -591,6 +591,15 @@ void PathsCache::CachePath(const vertex_t& fromVertex,
|
||||
}
|
||||
|
||||
#if _WIN32
|
||||
const std::wstring& WideStringsCache::Get(const std::string& narrowString) {
|
||||
auto vertexNameIt = wideStringsCache_.find(narrowString);
|
||||
if (vertexNameIt == wideStringsCache_.end()) {
|
||||
throw std::invalid_argument("Given string was not already cached");
|
||||
}
|
||||
|
||||
return vertexNameIt->second;
|
||||
}
|
||||
|
||||
const std::wstring& WideStringsCache::GetOrInsert(
|
||||
const std::string& narrowString) {
|
||||
auto vertexNameIt = wideStringsCache_.find(narrowString);
|
||||
@@ -601,6 +610,12 @@ const std::wstring& WideStringsCache::GetOrInsert(
|
||||
|
||||
return vertexNameIt->second;
|
||||
}
|
||||
|
||||
void WideStringsCache::Insert(const std::string& narrowString) {
|
||||
if (!wideStringsCache_.contains(narrowString)) {
|
||||
wideStringsCache_.emplace(narrowString, ToWinWide(narrowString));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
size_t PluginGraph::CountVertices() const {
|
||||
@@ -615,9 +630,10 @@ std::optional<vertex_t> PluginGraph::GetVertexByName(
|
||||
const std::string& name) const {
|
||||
for (const auto& vertex : boost::make_iterator_range(GetVertices())) {
|
||||
#if _WIN32
|
||||
auto& wideVertexName =
|
||||
wideStringCache_.GetOrInsert(GetPlugin(vertex).GetName());
|
||||
const auto& vertexName = GetPlugin(vertex).GetName();
|
||||
wideStringCache_.Insert(vertexName);
|
||||
auto& wideName = wideStringCache_.GetOrInsert(name);
|
||||
auto& wideVertexName = wideStringCache_.Get(vertexName);
|
||||
|
||||
int comparison = CompareFilenames(wideVertexName, wideName);
|
||||
#else
|
||||
|
||||
@@ -61,10 +61,12 @@ private:
|
||||
#if _WIN32
|
||||
class WideStringsCache {
|
||||
public:
|
||||
void Insert(const std::string& narrowString);
|
||||
const std::wstring& Get(const std::string& narrowString);
|
||||
const std::wstring& GetOrInsert(const std::string& narrowString);
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, std::wstring> wideStringsCache_;
|
||||
boost::unordered_flat_map<std::string, std::wstring> wideStringsCache_;
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user