From 3cddc1488024d27d4179224b835e80af2f5c2f5a Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 25 May 2018 14:13:22 -0400 Subject: [PATCH] perf: BreadthFirstSearchPathManyDestinations optimizations --- ICD.Common.Utils/RecursionUtils.cs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/ICD.Common.Utils/RecursionUtils.cs b/ICD.Common.Utils/RecursionUtils.cs index 22721ae..6cfec06 100644 --- a/ICD.Common.Utils/RecursionUtils.cs +++ b/ICD.Common.Utils/RecursionUtils.cs @@ -245,20 +245,16 @@ namespace ICD.Common.Utils throw new ArgumentNullException("comparer"); IcdHashSet destinationsToBeProcessed = new IcdHashSet(destinations); - IcdHashSet destinationsProcessed = new IcdHashSet(); Dictionary> pathsToReturn = new Dictionary>(); // Edge case, root is the destination foreach (T destination in - destinationsToBeProcessed.Where(destination => comparer.Equals(root, destination))) + destinationsToBeProcessed.Where(destination => comparer.Equals(root, destination)).ToArray()) { - destinationsProcessed.Add(destination); + destinationsToBeProcessed.Remove(destination); pathsToReturn.Add(destination, new[] {root}); } - foreach (T destination in destinationsProcessed) - destinationsToBeProcessed.Remove(destination); - destinationsProcessed.Clear(); if (destinationsToBeProcessed.Count == 0) return pathsToReturn; @@ -278,17 +274,12 @@ namespace ICD.Common.Utils T closureNode = node; foreach (T destination in - destinationsToBeProcessed.Where(destination => comparer.Equals(closureNode, destination))) + destinationsToBeProcessed.Where(destination => comparer.Equals(closureNode, destination)).ToArray()) { - destinationsProcessed.Add(destination); + destinationsToBeProcessed.Remove(destination); pathsToReturn.Add(destination, GetPath(destination, root, nodeParents, comparer).Reverse()); } - foreach (T destination in destinationsProcessed) - destinationsToBeProcessed.Remove(destination); - - destinationsProcessed.Clear(); - if (destinationsToBeProcessed.Count == 0) return pathsToReturn; }