Skip to content

Commit

Permalink
Skip second BFS when checking for paths in both direction
Browse files Browse the repository at this point in the history
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%.
  • Loading branch information
Ortham committed Jan 18, 2025
1 parent 628666e commit 3df5606
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
27 changes: 7 additions & 20 deletions src/api/sorting/plugin_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<std::vector<vertex_t>> PluginGraph::FindPath(
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions src/api/sorting/plugin_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<vertex_t>> FindPath(const vertex_t& fromVertex,
const vertex_t& toVertex);
Expand Down

0 comments on commit 3df5606

Please sign in to comment.