Skip to content
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

Performance: Optimize map::route and map::get_path_avoid. #69198

Closed
wants to merge 14 commits into from

Conversation

prharvey
Copy link
Contributor

@prharvey prharvey commented Nov 9, 2023

Summary

Performance "Increase speed of pathfinding substantially."

Purpose of change

These two functions are a giant hotspot when waiting in high traffic areas.

Describe the solution

This PR makes three two broad changes, somewhat interlinked:

  1. The current code base uses get_path_avoid() to evaluate locations to avoid before running A*, however this defeats the entire point of A*. Replaced the function with lazy evaluation alternatives should_path_avoid() and get_should_path_avoid_functor() so the pathfinder can evaluate these locations as they are visited. This however means the pathfinder must evaluate more tiles, which brings us to change 2.
  2. Aggressively increase the amount of information that is being cached in the pathfinder, and allow incremental updates to the pathfinder cache. This drastically reduces the per-tile evaluation cost of the pathfinder, which lets the change in 1 have a real impact.
  3. Use a flood fill to detect areas where monsters are trapped, but otherwise want to leave. This is a common occurrence in underground locations with reinforced windows or bars, and results in the monster running A* on the whole room every tick. This marks such zones with a minimum bash strength needed to escape, and if the target is outside of this zone, pathfinding is skipped entirely. Flood fill is being moved to a separate PR.

Testing

Tests pass, game appears to work. Needs more playtesting from my end, so PR is still in progress.

Additional context

Flame graph before:
before

@github-actions github-actions bot added Code: Performance Performance boosting code (CPU, memory, etc.) NPC / Factions NPCs, AI, Speech, Factions, Ownership [C++] Changes (can be) made in C++. Previously named `Code` json-styled JSON lint passed, label assigned by github actions astyled astyled PR, label is assigned by github actions labels Nov 9, 2023
@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label Nov 9, 2023
@github-actions github-actions bot removed the BasicBuildPassed This PR builds correctly, label assigned by github actions label Nov 10, 2023
@prharvey prharvey changed the title Performance: Lazy evaluation of avoidance in pathfinder. Performance: Optimize map::route and map::get_path_avoid. Nov 10, 2023
@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label Nov 10, 2023
@github-actions github-actions bot removed the BasicBuildPassed This PR builds correctly, label assigned by github actions label Nov 10, 2023
@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label Nov 11, 2023
@prharvey
Copy link
Contributor Author

There's currently a problem with this, in that some changes I made to monster's should_path_avoid got dropped on the floor when I was messing around trying to rebase stuff. I need to take some time to move those changes back in here.

Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Please do not bump or comment on this issue unless you are actively working on it. Stale issues, and stale issues that are closed are still considered.

@github-actions github-actions bot added the stale Closed for lack of activity, but still valid. label Dec 12, 2023
@prharvey
Copy link
Contributor Author

Superseded by #70274

@prharvey prharvey closed this Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
astyled astyled PR, label is assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions [C++] Changes (can be) made in C++. Previously named `Code` Code: Performance Performance boosting code (CPU, memory, etc.) json-styled JSON lint passed, label assigned by github actions NPC / Factions NPCs, AI, Speech, Factions, Ownership stale Closed for lack of activity, but still valid.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant