Trouble with object pooling
I'm trying to implement object pooling on A* nodes, but when I put in the object pooling code, the pathfinding is unable to find a path. From what I can figure out, the pooled nodes are not being fully cleaned before being pulled from the pool, and the pathfinding is suffering because of this.
Here is a trimmed down version of my class, with the pooling code.
public class ShippingRouteNode : IAStarNode {
// either "node" or "line" currently, but making it as a string to potentially support modding
public string Type;
// only non-null if type is transitline
public TransitLineScript TransitLine;
// machine readable name for the transit type in use
public string TransitTypeKey;
// only non-null if type is node
public TransitNodeScript Node;
private ShippingRouteNode() {
}
private static Queue<ShippingRouteNode> _nodePool;
public static ShippingRouteNode FromPool() {
ShippingRouteNode node;
if (_nodePool == null) {
_nodePool = new Queue<ShippingRouteNode>();
}
if (_nodePool.Count == 0) {
node = new ShippingRouteNode();
} else {
node = _nodePool.Dequeue();
}
return node;
}
public static void ToPool(ShippingRouteNode node) {
node.Type = "";
node.TransitLine = null;
node.TransitTypeKey = "";
node.Node = null;
_nodePool.Enqueue(node);
}
public static void ToPool(IEnumerable<ShippingRouteNode> nodes) {
foreach (var node in nodes) {
ToPool(node);
}
}
public void Dispose() {
ToPool(this);
}
public static int PoolSize() {
if (_nodePool == null) return 0;
return _nodePool.Count;
}
}
IAStarNode is an interface, so there is no lingering fields from there, and my ToPool function clears out the only 4 fields this class has. If I change the FromPool function to this, it all works:
public static ShippingRouteNode FromPool() {
return new ShippingRouteNode();
}
This makes me sure that somehow the objects coming from the pool are not perfectly fresh. I just can't figure out why they would contain old data.
I've checked the entire solution for all instances of new ShippingRouteNode(), and the only time is in FromPool.
Does anyone have any idea why my pooling is causing the pathfinding to fail?
Answer by kingcoyote · Jun 04, 2016 at 07:03 PM
I found the answer. Sorta.
On some nodes, I'm calling ToPool repeatedly. This puts the node in queue more than once. When it is dequeued the first time and modified, the second reference still in queue is also modified. So when that second reference is dequeued, I now have a dirty node.
I need to find out why I'm putting them back in the pool repeatedly and stop that from happening.
Answer by Chris333 · Jun 04, 2016 at 06:27 PM
Hi,
are you reseting the neighbours and all other fields of each node which get pulled from the pool or are they stay the same. Idk how you implemenmted a* but maybe this could be a possible source of error.
I added some code to clear out the node at the end of FromPool() and all that did was throw an exception in a really bizarre place. I added breakpoints in FromPool and ToPool to monitor the status of every node going into or out of the pool and I observed that nodes going into the pool are scrubbed, but while in the pool some will suddenly be dirty.
I wonder if there is a reference issue somewhere, allowing a node to be both in the pool and accessible elsewhere simultaneously. I think I need to look at every place I pull from the pool and see what I do with the node.
Your answer
