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:
RoosterDragon
2015-08-13 23:38:23 +01:00
parent 59edf85513
commit 519be4374c
11 changed files with 158 additions and 170 deletions

View File

@@ -165,23 +165,24 @@ namespace OpenRA.Mods.Common.Traits
select new { Location = r.Actor.Location + r.Trait.DeliveryOffset, Actor = r.Actor, Occupancy = linkedHarvs }).ToDictionary(r => r.Location);
// Start a search from each refinery's delivery location:
List<CPos> path;
var mi = self.Info.Traits.Get<MobileInfo>();
var path = self.World.WorldActor.Trait<IPathFinder>().FindPath(
PathSearch.FromPoints(self.World, mi, self, refs.Values.Select(r => r.Location), self.Location, false)
.WithCustomCost(loc =>
{
if (!refs.ContainsKey(loc))
return 0;
using (var search = PathSearch.FromPoints(self.World, mi, self, refs.Values.Select(r => r.Location), self.Location, false)
.WithCustomCost(loc =>
{
if (!refs.ContainsKey(loc))
return 0;
var occupancy = refs[loc].Occupancy;
var occupancy = refs[loc].Occupancy;
// 4 harvesters clogs up the refinery's delivery location:
if (occupancy >= 3)
return Constants.InvalidNode;
// 4 harvesters clogs up the refinery's delivery location:
if (occupancy >= 3)
return Constants.InvalidNode;
// Prefer refineries with less occupancy (multiplier is to offset distance cost):
return occupancy * 12;
}));
// Prefer refineries with less occupancy (multiplier is to offset distance cost):
return occupancy * 12;
}))
path = self.World.WorldActor.Trait<IPathFinder>().FindPath(search);
if (path.Count != 0)
return refs[path.Last()].Actor;