From 774992c246a0c2576534ce57de721f529ebb193c Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Fri, 21 Aug 2015 21:27:20 +0100 Subject: [PATCH] Cache only unit paths in the pathfinder. The path caching works on the assumption that the time saved from reusing a cached path outweights the cost of caching it in the first place. For unit paths, this assumption holds. For path searchs, we spend more time caching them then we save when we get to reuse these cached paths. This is because they are reused less often, and calculating their key is more expensive in comparison. --- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 2 +- .../Pathfinder/BasePathSearch.cs | 33 ------------------ ...cs => PathFinderUnitPathCacheDecorator.cs} | 34 +++---------------- OpenRA.Mods.Common/Traits/World/PathFinder.cs | 2 +- 4 files changed, 7 insertions(+), 64 deletions(-) rename OpenRA.Mods.Common/Pathfinder/{PathFinderCacheDecorator.cs => PathFinderUnitPathCacheDecorator.cs} (71%) diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 3314d73807..479b58949b 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -508,7 +508,7 @@ - + diff --git a/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs b/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs index 57fb3c3cad..13c8c0e79d 100644 --- a/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs +++ b/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs @@ -18,8 +18,6 @@ namespace OpenRA.Mods.Common.Pathfinder { public interface IPathSearch { - string Id { get; } - /// /// The Graph used by the A* /// @@ -71,36 +69,6 @@ namespace OpenRA.Mods.Common.Pathfinder { public IGraph Graph { get; set; } - // The Id of a Pathsearch is computed by its properties. - // So two PathSearch instances with the same parameters will - // Compute the same Id. This is used for caching purposes. - public string Id - { - get - { - if (string.IsNullOrEmpty(id)) - { - var builder = new StringBuilder(); - builder.Append(Graph.Actor.ActorID); - while (!startPoints.Empty) - { - var startpoint = startPoints.Pop(); - builder.Append(startpoint.X); - builder.Append(startpoint.Y); - builder.Append(Graph[startpoint].EstimatedTotal); - } - - builder.Append(Graph.InReverse); - if (Graph.IgnoredActor != null) builder.Append(Graph.IgnoredActor.ActorID); - builder.Append(Graph.LaneBias); - - id = builder.ToString(); - } - - return id; - } - } - public IPriorityQueue OpenQueue { get; protected set; } public abstract IEnumerable> Considered { get; } @@ -108,7 +76,6 @@ namespace OpenRA.Mods.Common.Pathfinder public Player Owner { get { return Graph.Actor.Owner; } } public int MaxCost { get; protected set; } public bool Debug { get; set; } - string id; protected Func heuristic; protected Func isGoal; diff --git a/OpenRA.Mods.Common/Pathfinder/PathFinderCacheDecorator.cs b/OpenRA.Mods.Common/Pathfinder/PathFinderUnitPathCacheDecorator.cs similarity index 71% rename from OpenRA.Mods.Common/Pathfinder/PathFinderCacheDecorator.cs rename to OpenRA.Mods.Common/Pathfinder/PathFinderUnitPathCacheDecorator.cs index f4fad6cf99..1aeaf768ac 100644 --- a/OpenRA.Mods.Common/Pathfinder/PathFinderCacheDecorator.cs +++ b/OpenRA.Mods.Common/Pathfinder/PathFinderUnitPathCacheDecorator.cs @@ -16,14 +16,14 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Pathfinder { /// - /// A decorator used to cache the pathfinder (Decorator design pattern) + /// A decorator used to cache FindUnitPath and FindUnitPathToRange (Decorator design pattern) /// - public class PathFinderCacheDecorator : IPathFinder + public class PathFinderUnitPathCacheDecorator : IPathFinder { readonly IPathFinder pathFinder; readonly ICacheStorage> cacheStorage; - public PathFinderCacheDecorator(IPathFinder pathFinder, ICacheStorage> cacheStorage) + public PathFinderUnitPathCacheDecorator(IPathFinder pathFinder, ICacheStorage> cacheStorage) { this.pathFinder = pathFinder; this.cacheStorage = cacheStorage; @@ -68,37 +68,13 @@ namespace OpenRA.Mods.Common.Pathfinder public List FindPath(IPathSearch search) { using (new PerfSample("Pathfinder")) - { - var key = "FindPath" + search.Id; - var cachedPath = cacheStorage.Retrieve(key); - - if (cachedPath != null) - return cachedPath; - - var pb = pathFinder.FindPath(search); - - cacheStorage.Store(key, pb); - - return pb; - } + return pathFinder.FindPath(search); } public List FindBidiPath(IPathSearch fromSrc, IPathSearch fromDest) { using (new PerfSample("Pathfinder")) - { - var key = "FindBidiPath" + fromSrc.Id + fromDest.Id; - var cachedPath = cacheStorage.Retrieve(key); - - if (cachedPath != null) - return cachedPath; - - var pb = pathFinder.FindBidiPath(fromSrc, fromDest); - - cacheStorage.Store(key, pb); - - return pb; - } + return pathFinder.FindBidiPath(fromSrc, fromDest); } } } diff --git a/OpenRA.Mods.Common/Traits/World/PathFinder.cs b/OpenRA.Mods.Common/Traits/World/PathFinder.cs index 70e71fcfc6..6524d67775 100644 --- a/OpenRA.Mods.Common/Traits/World/PathFinder.cs +++ b/OpenRA.Mods.Common/Traits/World/PathFinder.cs @@ -22,7 +22,7 @@ namespace OpenRA.Mods.Common.Traits { public object Create(ActorInitializer init) { - return new PathFinderCacheDecorator(new PathFinder(init.World), new PathCacheStorage(init.World)); + return new PathFinderUnitPathCacheDecorator(new PathFinder(init.World), new PathCacheStorage(init.World)); } }