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.
This commit is contained in:
RoosterDragon
2015-08-20 20:35:14 +01:00
parent 7d44eb953e
commit 76303e9699

View File

@@ -23,7 +23,7 @@ namespace OpenRA.Mods.Common.Pathfinder
/// <summary> /// <summary>
/// Gets all the Connections for a given node in the graph /// Gets all the Connections for a given node in the graph
/// </summary> /// </summary>
IEnumerable<GraphConnection> GetConnections(CPos position); List<GraphConnection> GetConnections(CPos position);
/// <summary> /// <summary>
/// Retrieves an object given a node in the graph /// 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) }, new[] { new CVec(1, -1), new CVec(1, 0), new CVec(-1, 1), new CVec(0, 1), new CVec(1, 1) },
}; };
public IEnumerable<GraphConnection> GetConnections(CPos position) public List<GraphConnection> GetConnections(CPos position)
{ {
var previousPos = cellInfo[position].PreviousPos; var previousPos = cellInfo[position].PreviousPos;
@@ -108,14 +108,14 @@ namespace OpenRA.Mods.Common.Pathfinder
var dy = position.Y - previousPos.Y; var dy = position.Y - previousPos.Y;
var index = dy * 3 + dx + 4; var index = dy * 3 + dx + 4;
var validNeighbors = new LinkedList<GraphConnection>();
var directions = DirectedNeighbors[index]; var directions = DirectedNeighbors[index];
var validNeighbors = new List<GraphConnection>(directions.Length);
for (var i = 0; i < directions.Length; i++) for (var i = 0; i < directions.Length; i++)
{ {
var neighbor = position + directions[i]; var neighbor = position + directions[i];
var movementCost = GetCostToNode(neighbor, directions[i]); var movementCost = GetCostToNode(neighbor, directions[i]);
if (movementCost != Constants.InvalidNode) if (movementCost != Constants.InvalidNode)
validNeighbors.AddLast(new GraphConnection(neighbor, movementCost)); validNeighbors.Add(new GraphConnection(neighbor, movementCost));
} }
return validNeighbors; return validNeighbors;