Fixed pooling of layers used for pathfinding.
The previous implementation: - Was failing to dispose of pooled layers. - Was using a finalizer to allow undisposed layers to be reused. This means all pooled layers are kept alive indefinitely until the map changes. If the finalizer is slow for any reason then the pathfiinder will allocate new layers when the pool runs out. Since these new layers are eventually stuffed back into the pool when the finalizer does run, this can theoretically leak unbounded memory until the pool goes out of scope. In practice it would leak tens of megabytes. The new implementation ensures layers are disposed and pooled correctly to allow proper memory reuse. It also introduces some safeguards against memory leaks: - A cap is set on the number of pooled layers. If more concurrent layers are needed than this, then the excess layers will not be pooled but instead be allowed to be garbage collected. - No finalizer. An implementation that fails to call dispose simply allows the layer to be garbage collected instead.
This commit is contained in:
@@ -69,7 +69,7 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
}
|
||||
}
|
||||
|
||||
public class PathGraph : IGraph<CellInfo>
|
||||
sealed class PathGraph : IGraph<CellInfo>
|
||||
{
|
||||
public Actor Actor { get; private set; }
|
||||
public World World { get; private set; }
|
||||
@@ -82,11 +82,13 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
readonly CellConditions checkConditions;
|
||||
readonly MobileInfo mobileInfo;
|
||||
readonly MobileInfo.WorldMovementInfo worldMovementInfo;
|
||||
readonly CellInfoLayerPool.PooledCellInfoLayer pooledLayer;
|
||||
CellLayer<CellInfo> cellInfo;
|
||||
|
||||
public PathGraph(CellLayer<CellInfo> cellInfo, MobileInfo mobileInfo, Actor actor, World world, bool checkForBlocked)
|
||||
public PathGraph(CellInfoLayerPool layerPool, MobileInfo mobileInfo, Actor actor, World world, bool checkForBlocked)
|
||||
{
|
||||
this.cellInfo = cellInfo;
|
||||
pooledLayer = layerPool.Get();
|
||||
cellInfo = pooledLayer.Layer;
|
||||
World = world;
|
||||
this.mobileInfo = mobileInfo;
|
||||
worldMovementInfo = mobileInfo.GetWorldMovementInfo(world);
|
||||
@@ -174,26 +176,16 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
return cellCost;
|
||||
}
|
||||
|
||||
bool disposed;
|
||||
public void Dispose()
|
||||
{
|
||||
if (disposed)
|
||||
return;
|
||||
|
||||
disposed = true;
|
||||
|
||||
CellInfoLayerManager.Instance.PutBackIntoPool(cellInfo);
|
||||
cellInfo = null;
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~PathGraph() { Dispose(); }
|
||||
|
||||
public CellInfo this[CPos pos]
|
||||
{
|
||||
get { return cellInfo[pos]; }
|
||||
set { cellInfo[pos] = value; }
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
pooledLayer.Dispose();
|
||||
cellInfo = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user