From 3df560646d6eb716ddf1ce22d18ae8fbf4f4104d Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 18 Jan 2025 09:41:19 +0000 Subject: [PATCH] Skip second BFS when checking for paths in both direction It's actually significantly faster to just check if the path you're trying to create has already been cached than to check if it actually already exists. This improves sorting performance by 15%. --- src/api/sorting/plugin_graph.cpp | 27 +++++++-------------------- src/api/sorting/plugin_graph.h | 3 +-- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/api/sorting/plugin_graph.cpp b/src/api/sorting/plugin_graph.cpp index f572bb5f..c1aee1ec 100644 --- a/src/api/sorting/plugin_graph.cpp +++ b/src/api/sorting/plugin_graph.cpp @@ -259,7 +259,8 @@ class GroupsVisitor : public boost::dfs_visitor<> { for (const auto& toVertex : toPlugins) { const auto& toPlugin = pluginGraph_->GetPlugin(toVertex); - if (!pluginGraph_->PathExistsInEitherDirection(toVertex, fromVertex)) { + if (!pluginGraph_->IsPathCached(fromVertex, toVertex) && + !pluginGraph_->PathExists(toVertex, fromVertex)) { const auto involvesUserMetadata = groupPathInvolvesUserMetadata || fromPlugin.IsGroupUserMetadata() || toPlugin.IsGroupUserMetadata(); @@ -738,24 +739,9 @@ bool PluginGraph::PathExists(const vertex_t& fromVertex, return loot::FindPath(graph_, fromVertex, toVertex, visitor); } -bool PluginGraph::PathExistsInEitherDirection(const vertex_t& vertex, - const vertex_t& otherVertex) { - if (pathsCache_.IsPathCached(vertex, otherVertex) || - pathsCache_.IsPathCached(otherVertex, vertex)) { - return true; - } - - { - PathCacher visitor(pathsCache_, vertex, otherVertex); - - if (loot::FindPath(graph_, vertex, otherVertex, visitor)) { - return true; - } - } - - PathCacher visitor(pathsCache_, otherVertex, vertex); - - return loot::FindPath(graph_, otherVertex, vertex, visitor); +bool PluginGraph::IsPathCached(const vertex_t& fromVertex, + const vertex_t& toVertex) { + return pathsCache_.IsPathCached(fromVertex, toVertex); } std::optional> PluginGraph::FindPath( @@ -1052,7 +1038,8 @@ void PluginGraph::AddOverlapEdges() { const auto fromVertex = thisPluginLoadsFirst ? vertex : otherVertex; const auto toVertex = thisPluginLoadsFirst ? otherVertex : vertex; - if (!PathExistsInEitherDirection(toVertex, fromVertex)) { + if (!IsPathCached(fromVertex, toVertex) && + !PathExists(toVertex, fromVertex)) { AddEdge(fromVertex, toVertex, edgeType); } else if (logger) { logger->debug( diff --git a/src/api/sorting/plugin_graph.h b/src/api/sorting/plugin_graph.h index 7e28c833..052caa7f 100644 --- a/src/api/sorting/plugin_graph.h +++ b/src/api/sorting/plugin_graph.h @@ -90,8 +90,7 @@ class PluginGraph { bool EdgeExists(const vertex_t& fromVertex, const vertex_t& toVertex); bool PathExists(const vertex_t& fromVertex, const vertex_t& toVertex); - bool PathExistsInEitherDirection(const vertex_t& vertex, - const vertex_t& otherVertex); + bool IsPathCached(const vertex_t& fromVertex, const vertex_t& toVertex); std::optional> FindPath(const vertex_t& fromVertex, const vertex_t& toVertex);