Skip to content

Commit

Permalink
Add ability to rent neighbor arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen committed Jan 20, 2024
1 parent 18287d7 commit 59c4f21
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 28 deletions.
5 changes: 5 additions & 0 deletions src/FactorioTools/FactorioTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<DefineConstants Condition="$(UseStackalloc) == 'true'">$(DefineConstants);USE_STACKALLOC</DefineConstants>
</PropertyGroup>

<PropertyGroup>
<RentNeighbors Condition="'$(RentNeighbors)' == ''">true</RentNeighbors>
<DefineConstants Condition="$(RentNeighbors) == 'true'">$(DefineConstants);RENT_NEIGHBORS</DefineConstants>
</PropertyGroup>

<PropertyGroup>
<EnableVisualizer Condition="'$(EnableVisualizer)' == '' and '$(Configuration)' == 'Debug'">$(UseDefines)</EnableVisualizer>
<EnableVisualizer Condition="'$(EnableVisualizer)' == '' and '$(Configuration)' != 'Debug'">false</EnableVisualizer>
Expand Down
8 changes: 8 additions & 0 deletions src/FactorioTools/OilField/Algorithms/AStar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ public static AStarResult GetShortestPath(Context context, SquareGrid grid, Loca

Location reachedGoal = Location.Invalid;
bool success = false;
#if RENT_NEIGHBORS
Location[] neighbors = context.SharedInstances.GetNeighborArray();
#else
#if USE_STACKALLOC && LOCATION_AS_STRUCT
Span<Location> neighbors = stackalloc Location[4];
#else
Span<Location> neighbors = new Location[4];
#endif
#endif

while (frontier.Count > 0)
Expand Down Expand Up @@ -132,6 +136,10 @@ public static AStarResult GetShortestPath(Context context, SquareGrid grid, Loca
}
}

#if RENT_NEIGHBORS
context.SharedInstances.ReturnNeighborArray(neighbors);
#endif

if (!success)
{
outputList = null;
Expand Down
8 changes: 8 additions & 0 deletions src/FactorioTools/OilField/Algorithms/BreadthFirstFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ public static class BreadthFirstFinder
#endif
toExplore.Enqueue(start);

#if RENT_NEIGHBORS
Location[] neighbors = context.SharedInstances.GetNeighborArray();
#else
#if USE_STACKALLOC && LOCATION_AS_STRUCT
Span<Location> neighbors = stackalloc Location[4];
#else
Span<Location> neighbors = new Location[4];
#endif
#endif

while (toExplore.Count > 0)
Expand Down Expand Up @@ -62,6 +66,10 @@ public static class BreadthFirstFinder
}
}

#if RENT_NEIGHBORS
context.SharedInstances.ReturnNeighborArray(neighbors);
#endif

return null;
#if USE_SHARED_INSTANCES
}
Expand Down
9 changes: 9 additions & 0 deletions src/FactorioTools/OilField/Algorithms/Dijkstras.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ public static DijkstrasResult GetShortestPaths(Context context, SquareGrid grid,
priorityQueue.Enqueue(start, 0);
inQueue.Add(start);

#if RENT_NEIGHBORS
Location[] neighbors = context.SharedInstances.GetNeighborArray();
#else
#if USE_STACKALLOC && LOCATION_AS_STRUCT
Span<Location> neighbors = stackalloc Location[4];
#else
Span<Location> neighbors = new Location[4];
#endif
#endif

while (priorityQueue.Count > 0)
Expand Down Expand Up @@ -80,6 +84,11 @@ public static DijkstrasResult GetShortestPaths(Context context, SquareGrid grid,
}
}
}

#if RENT_NEIGHBORS
context.SharedInstances.ReturnNeighborArray(neighbors);
#endif

#if USE_SHARED_INSTANCES
}
finally
Expand Down
19 changes: 19 additions & 0 deletions src/FactorioTools/OilField/Models/SharedInstances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ public SharedInstances(SquareGrid grid)
#endif
}

#if RENT_NEIGHBORS
private Queue<Location[]> _neighborArrays = new Queue<Location[]>();

public Location[] GetNeighborArray()
{
if (_neighborArrays.Count > 0)
{
return _neighborArrays.Dequeue();
}

return new Location[4];
}

public void ReturnNeighborArray(Location[] array)
{
_neighborArrays.Enqueue(array);
}
#endif

#if USE_SHARED_INSTANCES
public Queue<Location> LocationQueue = new();
public Location[] LocationArray = Array.Empty<Location>();
Expand Down
4 changes: 4 additions & 0 deletions src/FactorioTools/OilField/Steps/AddElectricPoles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,14 @@ private static void AddSinglePoleForConnection(Context context, ILocationDiction
candidates.Enqueue(idealPoint);
attempted.Add(idealPoint);

#if RENT_NEIGHBORS
Location[] neighbors = context.SharedInstances.GetNeighborArray();
#else
#if USE_STACKALLOC && LOCATION_AS_STRUCT
Span<Location> neighbors = stackalloc Location[4];
#else
Span<Location> neighbors = new Location[4];
#endif
#endif

while (candidates.Count > 0)
Expand Down
17 changes: 17 additions & 0 deletions src/FactorioTools/OilField/Steps/RotateOptimize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,14 @@ private static void ExplorePipes(ChildContext context, Location start, ILocation
toExplore.Enqueue(start);
pipes.Add(start);

#if RENT_NEIGHBORS
Location[] neighbors = context.ParentContext.SharedInstances.GetNeighborArray();
#else
#if USE_STACKALLOC && LOCATION_AS_STRUCT
Span<Location> neighbors = stackalloc Location[4];
#else
Span<Location> neighbors = new Location[4];
#endif
#endif

while (toExplore.Count > 0)
Expand All @@ -332,6 +336,11 @@ private static void ExplorePipes(ChildContext context, Location start, ILocation
}
}
}

#if RENT_NEIGHBORS
context.ParentContext.SharedInstances.ReturnNeighborArray(neighbors);
#endif

#if USE_SHARED_INSTANCES
}
finally
Expand All @@ -354,10 +363,14 @@ private static ExploredPaths ExplorePaths(ChildContext context, Location start)
var cameFrom = context.ParentContext.GetLocationDictionary<Location>();
cameFrom[start] = start;

#if RENT_NEIGHBORS
Location[] neighbors = context.ParentContext.SharedInstances.GetNeighborArray();
#else
#if USE_STACKALLOC && LOCATION_AS_STRUCT
Span<Location> neighbors = stackalloc Location[4];
#else
Span<Location> neighbors = new Location[4];
#endif
#endif

var reachedGoals = new List<Location>();
Expand Down Expand Up @@ -385,6 +398,10 @@ private static ExploredPaths ExplorePaths(ChildContext context, Location start)
}
}

#if RENT_NEIGHBORS
context.ParentContext.SharedInstances.ReturnNeighborArray(neighbors);
#endif

return new ExploredPaths(start, cameFrom, reachedGoals);
#if USE_SHARED_INSTANCES
}
Expand Down
12 changes: 9 additions & 3 deletions src/lua/FactorioTools/AStar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ System.namespace("Knapcode.FactorioTools.OilField", function (namespace)

local reachedGoal = KnapcodeOilField.Location.getInvalid()
local success = false
local neighbors = SpanLocation.ctorArray(ArrayLocation(4))

local neighbors = context.SharedInstances:GetNeighborArray()



while #frontier > 0 do
Expand All @@ -64,8 +66,8 @@ System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
local previous = cameFrom:get(current)
local currentCost = costSoFar:get(current)

grid:GetNeighbors(neighbors, current)
for i = 0, neighbors:getLength() - 1 do
grid:GetNeighbors(SpanLocation.ctorArray(neighbors), current)
for i = 0, #neighbors - 1 do
local continue
repeat
local next = neighbors:get(i)
Expand Down Expand Up @@ -106,6 +108,10 @@ System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
end
end


context.SharedInstances:ReturnNeighborArray(neighbors)


if not success then
outputList = nil
elseif outputList ~= nil then
Expand Down
10 changes: 5 additions & 5 deletions src/lua/FactorioTools/AddElectricPoles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ local KnapcodeFactorioTools
local KnapcodeOilField
local KnapcodeAddElectricPoles
local SpanLocation
local ArrayLocation
local QueueLocation
local ListILocationSet
local QueueElectricPoleCenter
Expand All @@ -18,7 +17,6 @@ System.import(function (out)
KnapcodeOilField = Knapcode.FactorioTools.OilField
KnapcodeAddElectricPoles = Knapcode.FactorioTools.OilField.AddElectricPoles
SpanLocation = System.Span(KnapcodeOilField.Location)
ArrayLocation = System.Array(KnapcodeOilField.Location)
QueueLocation = System.Queue(KnapcodeOilField.Location)
ListILocationSet = System.List(KnapcodeOilField.ILocationSet)
QueueElectricPoleCenter = System.Queue(KnapcodeOilField.ElectricPoleCenter)
Expand Down Expand Up @@ -631,7 +629,9 @@ System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
candidates:Enqueue(idealPoint)
attempted:Add(idealPoint)

local neighbors = SpanLocation.ctorArray(ArrayLocation(4))

local neighbors = context.SharedInstances:GetNeighborArray()



while #candidates > 0 do
Expand All @@ -642,8 +642,8 @@ System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
break
end

context.Grid:GetAdjacent(neighbors, candidate)
for i = 0, neighbors:getLength() - 1 do
context.Grid:GetAdjacent(SpanLocation.ctorArray(neighbors), candidate)
for i = 0, #neighbors - 1 do
if neighbors:get(i).IsValid and AreElectricPolesConnected(idealLine:get(0), neighbors:get(i), context.Options) and attempted:Add(neighbors:get(i)) then
candidates:Enqueue(neighbors:get(i))
end
Expand Down
14 changes: 9 additions & 5 deletions src/lua/FactorioTools/BreadthFirstFinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ local System = System
local KnapcodeOilField
local ListLocation
local SpanLocation
local ArrayLocation
local QueueLocation
System.import(function (out)
KnapcodeOilField = Knapcode.FactorioTools.OilField
ListLocation = System.List(KnapcodeOilField.Location)
SpanLocation = System.Span(KnapcodeOilField.Location)
ArrayLocation = System.Array(KnapcodeOilField.Location)
QueueLocation = System.Queue(KnapcodeOilField.Location)
end)
System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
Expand All @@ -21,7 +19,9 @@ System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
local visited = context:GetLocationSet1()
toExplore:Enqueue(start)

local neighbors = SpanLocation.ctorArray(ArrayLocation(4))

local neighbors = context.SharedInstances:GetNeighborArray()



while #toExplore > 0 do
Expand Down Expand Up @@ -50,8 +50,8 @@ System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
return output
end

context.Grid:GetNeighbors(neighbors, current)
for i = 0, neighbors:getLength() - 1 do
context.Grid:GetNeighbors(SpanLocation.ctorArray(neighbors), current)
for i = 0, #neighbors - 1 do
local continue
repeat
local next = neighbors:get(i)
Expand All @@ -74,6 +74,10 @@ System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
end
end


context.SharedInstances:ReturnNeighborArray(neighbors)


return nil
end
return {
Expand Down
15 changes: 10 additions & 5 deletions src/lua/FactorioTools/Dijkstras.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
local System = System
local KnapcodeOilField
local SpanLocation
local ArrayLocation
local PriorityQueueLocationDouble
System.import(function (out)
KnapcodeOilField = Knapcode.FactorioTools.OilField
SpanLocation = System.Span(KnapcodeOilField.Location)
ArrayLocation = System.Array(KnapcodeOilField.Location)
PriorityQueueLocationDouble = System.PriorityQueue(KnapcodeOilField.Location, System.Double)
end)
System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
Expand All @@ -28,7 +26,9 @@ System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
priorityQueue:Enqueue(start, 0)
inQueue:Add(start)

local neighbors = SpanLocation.ctorArray(ArrayLocation(4))

local neighbors = context.SharedInstances:GetNeighborArray()



while #priorityQueue > 0 do
Expand All @@ -46,8 +46,8 @@ System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
end
end

grid:GetNeighbors(neighbors, current)
for i = 0, neighbors:getLength() - 1 do
grid:GetNeighbors(SpanLocation.ctorArray(neighbors), current)
for i = 0, #neighbors - 1 do
local continue
repeat
local neighbor = neighbors:get(i)
Expand Down Expand Up @@ -86,6 +86,11 @@ System.namespace("Knapcode.FactorioTools.OilField", function (namespace)
end
end


context.SharedInstances:ReturnNeighborArray(neighbors)



return KnapcodeOilField.DijkstrasResult(cameFrom, reachedGoals)
end
return {
Expand Down
Loading

0 comments on commit 59c4f21

Please sign in to comment.