Change paths cache storage stype

It's about 10% faster with a map of sets vs. a set of pairs.
This commit is contained in:
Oliver Hamlet
2022-12-02 17:21:43 +00:00
parent 85decd2746
commit d2ebefc40a
2 changed files with 15 additions and 16 deletions
+14 -2
View File
@@ -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<vertex_t>({toVertex}));
} else {
descendents->second.insert(toVertex);
}
}
size_t PluginGraph::CountVertices() const {
+1 -14
View File
@@ -39,19 +39,6 @@
#include "api/sorting/plugin_sorting_data.h"
#include "loot/exception/cyclic_interaction_error.h"
namespace std {
template<typename A, typename B>
struct hash<pair<A, B>> {
size_t operator()(const pair<A, B>& 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<boost::listS,
boost::listS,
@@ -71,7 +58,7 @@ public:
void CachePath(const vertex_t& fromVertex, const vertex_t& toVertex);
private:
std::unordered_set<std::pair<vertex_t, vertex_t>> pathsCache_;
std::unordered_map<vertex_t, std::unordered_set<vertex_t>> pathsCache_;
};
class PluginGraph {