From d2ebefc40a42f6838d95f3cd2a27419d52a09264 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Fri, 2 Dec 2022 15:24:08 +0000 Subject: [PATCH] Change paths cache storage stype It's about 10% faster with a map of sets vs. a set of pairs. --- src/api/sorting/plugin_graph.cpp | 16 ++++++++++++++-- src/api/sorting/plugin_graph.h | 15 +-------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/api/sorting/plugin_graph.cpp b/src/api/sorting/plugin_graph.cpp index 448d0c2e..287e4a23 100644 --- a/src/api/sorting/plugin_graph.cpp +++ b/src/api/sorting/plugin_graph.cpp @@ -117,12 +117,24 @@ std::string describeEdgeType(EdgeType edgeType) { bool PathsCache::IsPathCached(const vertex_t& fromVertex, const vertex_t& toVertex) const { - return pathsCache_.count({fromVertex, toVertex}); + const auto descendents = pathsCache_.find(fromVertex); + + if (descendents == pathsCache_.end()) { + return false; + } + + return descendents->second.count(toVertex); } void PathsCache::CachePath(const vertex_t& fromVertex, const vertex_t& toVertex) { - pathsCache_.insert({fromVertex, toVertex}); + auto descendents = pathsCache_.find(fromVertex); + + if (descendents == pathsCache_.end()) { + pathsCache_.emplace(fromVertex, std::unordered_set({toVertex})); + } else { + descendents->second.insert(toVertex); + } } size_t PluginGraph::CountVertices() const { diff --git a/src/api/sorting/plugin_graph.h b/src/api/sorting/plugin_graph.h index 5591b5cc..b42f41f0 100644 --- a/src/api/sorting/plugin_graph.h +++ b/src/api/sorting/plugin_graph.h @@ -39,19 +39,6 @@ #include "api/sorting/plugin_sorting_data.h" #include "loot/exception/cyclic_interaction_error.h" -namespace std { -template -struct hash> { - size_t operator()(const pair& pair) const { - size_t seed = 0; - boost::hash_combine(seed, pair.first); - boost::hash_combine(seed, pair.second); - - return seed; - } -}; -} - namespace loot { typedef boost::adjacency_list> pathsCache_; + std::unordered_map> pathsCache_; }; class PluginGraph {