Commit Graph

9 Commits

Author SHA1 Message Date
RoosterDragon
519be4374c 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.
2015-09-16 21:25:46 +01:00
RoosterDragon
ac55c5bf09 Fix pathfinding using PriorityQueue incorrectly.
By providing a comparer that could change over time (as estimated costs on the graph were updated), this meant the priority queue could have its heap property invalidated and thus not maintain a correct ordering. Instead we store elements into the queue with their estimations at the time. This preserves the heap property and thus ensures the queue returns properly ordered results, although it may contain out of date estimations.

This also improves performance. The fixed comparer need not perform expensive lookups into the graph, but can instead use the readily available value. This speeds up adds and removes on the queue significantly.
2015-09-01 17:29:36 +01:00
RoosterDragon
77923a27c1 Tweak IPathSearch to avoid exposing the OpenQueue directly. 2015-09-01 17:29:36 +01:00
RoosterDragon
774992c246 Cache only unit paths in the pathfinder.
The path caching works on the assumption that the time saved from reusing a cached path outweights the cost of caching it in the first place.

For unit paths, this assumption holds. For path searchs, we spend more time caching them then we save when we get to reuse these cached paths. This is because they are reused less often, and calculating their key is more expensive in comparison.
2015-09-01 17:29:35 +01:00
Oliver Brakmann
1e7da8514a Merge pull request #8735 from reaperrr/safe-pf-changes
Minor pathfinder-related changes (preparation for mobile refactor)
2015-07-22 14:42:52 +02:00
reaperrr
711ec0c600 Minor pathfinder changes 2015-07-22 06:36:00 +02:00
David Jiménez
787609d51e Improved the performance and intelligence of resource harvesting by
refactoring the Harvesters' pathfinding. Now they in first place assess
which is the closest resource inside their search area and then a path is
calculated

Changed the way harvesters find resources by always trying to find the
closest resource to their refinery.

Changed the strategy of finding to find resources in Annulus.
2015-07-22 02:31:26 +02:00
reaperrr
0ef5312931 Minor cosmetic cleanups for Harvester and BasePathSearch 2015-07-12 23:36:59 +02:00
David Jiménez
54ae572303 - Introduced Unit Testing capabilities to the PathFinder trait and algorithm.
Introduced also a small Unit test project to prove it.

- Separated caching capabilities from PathFinder class to increase cohesion and maintainability.
Refactored the pathfinding algorithm by extracting methods based on responsibilities like
calculating costs and reordering functions. These changes should provide a in average a small increase in
pathfinding performance and maintainability.

- Optimized the pathfinder algorithm to reuse calculations like the
MovementCost and heuristics.

- Introduced base classes, IPathSearch and IPriorityQueue interfaces,
and restructured code to ease readability and testability

- Renamed the PathFinder related classes to more appropriate names. Made the
traits rely on the interface IPathfinder instead of concrete PathFinder
implementation.

- Massive performance improvements

- Solved error with harvesters' Heuristic

- Updated the heuristic to ease redability and adjustability. D can be
adjusted to offer best paths by decreasing and more performance by
increasing it

- Refactored the CellLayer<CellInfo> creation in its own Singleton class

- Extracted the graph abstraction onto an IGraph interface, making the
Pathfinder agnostic to the definition of world and terrain. This
abstraction can help in the future to be able to cache graphs for similar
classes and their costs, speeding up the pathfinder and being able to feed
the A* algorithm with different types of graphs like Hierarchical graphs
2015-03-03 20:11:11 +01:00