From 76303e96992c28dc599aa2f4ecd47c0746c2544d Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Thu, 20 Aug 2015 20:35:14 +0100 Subject: [PATCH] In PathGraph.GetConnections, return a List of neighbors directly, rather than a LinkedList typed an IEnumerable. The caller can enumerate the list more efficiently without the IEnumerable indirection, and the reduced memory allocation is marginally faster than allocating a linked list and several nodes. --- OpenRA.Mods.Common/Pathfinder/PathGraph.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/OpenRA.Mods.Common/Pathfinder/PathGraph.cs b/OpenRA.Mods.Common/Pathfinder/PathGraph.cs index 603a8f8cd1..3204b5282e 100644 --- a/OpenRA.Mods.Common/Pathfinder/PathGraph.cs +++ b/OpenRA.Mods.Common/Pathfinder/PathGraph.cs @@ -23,7 +23,7 @@ namespace OpenRA.Mods.Common.Pathfinder /// /// Gets all the Connections for a given node in the graph /// - IEnumerable GetConnections(CPos position); + List GetConnections(CPos position); /// /// Retrieves an object given a node in the graph @@ -100,7 +100,7 @@ namespace OpenRA.Mods.Common.Pathfinder new[] { new CVec(1, -1), new CVec(1, 0), new CVec(-1, 1), new CVec(0, 1), new CVec(1, 1) }, }; - public IEnumerable GetConnections(CPos position) + public List GetConnections(CPos position) { var previousPos = cellInfo[position].PreviousPos; @@ -108,14 +108,14 @@ namespace OpenRA.Mods.Common.Pathfinder var dy = position.Y - previousPos.Y; var index = dy * 3 + dx + 4; - var validNeighbors = new LinkedList(); var directions = DirectedNeighbors[index]; + var validNeighbors = new List(directions.Length); for (var i = 0; i < directions.Length; i++) { var neighbor = position + directions[i]; var movementCost = GetCostToNode(neighbor, directions[i]); if (movementCost != Constants.InvalidNode) - validNeighbors.AddLast(new GraphConnection(neighbor, movementCost)); + validNeighbors.Add(new GraphConnection(neighbor, movementCost)); } return validNeighbors;