New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add domain checks to HierarchicalPathFinder #21164
Conversation
The domains in HierarchicalPathFinder can be compared to find disjoint areas. For example islands on a water map will belong to different domains. Use these domains in path searches to allow us to bail out early if a path is impossible, e.g. trying to path between different islands. Keeping the domains updated via the RebuildDomains method adds some cost to the average path search, but that savings from path searches that can bail early pays for this many times over.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code changes LGTM, and works as advertised on RA map Pool Party.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I ask why you hadn't added this functionality before? Because you were afraid of the negative performance impact?
Correct. Calling |
Shouldn't |
Yes, most of the time it's a no-op. But when it isn't a no-op, it felt like it would be expensive because it clears and rebuilds domains for the whole map. Compare this to I was probably conditioned into this thinking. Rebuilding dirty grids for the whole map was horrendously expensive so I had to invest time writing the convoluted method to do incremental updates so it was fast enough. Having spent time writing that I wasn't keen to be doing any more refreshing-the-whole-map operations. But the tests above show that the domain refresh happens to be about the same as the grid refresh in practise, so it turns out this is acceptable. |
If performance did turn out to be a problem, would it be feasible to incrementally update the domains before we need to access them, using any spare time that might be left over at the end of several ticks? |
Changing to an incremental update should reduce the cost by 10-100x and make the impact negligible. We don't even need to get fancy about using extra times between ticks. It'd just be complicated to write (the incremental update, that is). |
(note: this discussion has now diverged from being relevant to this PR, but I think its useful to carry it through here as it may prompt future optimisation PRs). Thinking out loud... Assumption 1: A (the most?) common cause for dirtying the HPF is going to be resource patches growing/being mined, which changes the terrain type / pathing cost. Assumption 2: Most (in the default mods, all) units have the same reachability for clear/resource cells, just different movement speeds. We could therefore win a lot by checking the abstract edges before/after updating a dirty cell, and only clear the domain cache if connections are added or removed. |
Enterprising readers will find the relevant code in this method. We could compare old grid to new grid and only perform the OpenRA/OpenRA.Mods.Common/Pathfinder/HierarchicalPathFinder.cs Lines 981 to 999 in 216758d
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The domains in HierarchicalPathFinder can be compared to find disjoint areas. For example islands on a water map will belong to different domains. Use these domains in path searches to allow us to bail out early if a path is impossible, e.g. trying to path between different islands. Keeping the domains updated via the RebuildDomains method adds some cost to the average path search, but that savings from path searches that can bail early pays for this many times over.
In the replay from #21132, the changes the time spent as follows:
Before
After
The extra RebuildDomains call isn't free, but the path searches it allows short-circuiting more than pays for itself.