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

Cache the exposed area used for pathfinding and rendering FoW #3745

Merged

Conversation

kwvanderlinde
Copy link
Collaborator

@kwvanderlinde kwvanderlinde commented Nov 23, 2022

Identify the Bug or Feature request

Addresses #3744

Description of the Change

This PR adds caching of the view-specific exposed areas. There are a few separate changes that come together to make this happen, which can be seen more easily in the commit history rather than the full diff.

The first major change is to simplify the algorithm for calculating the exposed area. The behaviour is the same, but three of the four special cases have been merged as they actually do the same thing (the fourth case is fundamentally different). Also the calculation of the exposed area is now separated from the rendering logic as the rendering logic was the same for all four cases.

The second major change is to move the exposed area calculation into ZoneView where it can be cached. Both ZoneRenderer and AbstractAStarCellWalker now make use that cache to improve their responsiveness.

The final major change is to remove the existing cache of the fog buffer contents. Now that the expensive portion of renderFog() is cached in its own right, there is much less benefit to cache the buffer contents. And importantly, the buffer contents would be invalidated anyways in some of the most user-noticeable cases, namely panning/zooming the map. Removing this cache means one less thing for ZoneRenderer to keep track of.

Sample times

Some (highly variable) timings for typical renderFog runs using the linked campaign in #3744:

  • On some modernish hardware, went from ~300ms to <20ms
  • On some older hardware, went from ~350ms-400ms to <50ms
  • On my RPi, went from ~900ms to 50-100ms

Of course, when the cache is invalidated, times still aren't especially good, but are typically about 2x better than before the change.

Possible Drawbacks

This change is meant to be a pure refactoring, so shouldn't be any issues unless I changed some subtle behaviour or didn't get the cache invalidation correct.

Documentation Notes

N/A

Release Notes

  • Improved FoW rendering and pathfinding performance for complicated FoW with several owned tokens.

This change is Reviewable

kwvanderlinde and others added 6 commits November 22, 2022 12:55
…kenView()

In the future, this could enable a complete separation of whether tokens are available vs whether we actually want a
global view. For now, it makes the calling code more self-descriptive as the logic is now clearly about whether we want
a global view and not so much whether there happens to be tokens available.
After removing `tempArea` and recognizing that the `combinedView.isEmpty()` case didn't add anything, it was clear that
three of the four cases were really the same thing and could be combined.

The fourth case really is distinct from the others as it does not render according to the current player view. In
practice, it only renders any previously exposed area for owned tokens that no longer have sight.
Since the rendering logic is identical for any exposed areas, there is no need to include it in each case. And now that
it is separated, we can time each portion separately rather than sometimes including the extra work in one timer that
really belongs in another.
The `ZoneRenderer` and `AbstractAStarWalker` can make use of this cache to avoid repeating the expensive
calculations. The exposed area cache is invalidated exactly whenever the visible area cache is invalidated as the two
are related.
Preserving the fog buffer no longer carries much benefit as the expensive part (calculated the exposed area) is now
cached on its own, and the rendering itself is typically quite cheap. And since the buffer contents have to be
invalidated during the key points of user interaction (e.g., moving the map), caching the contents does not offer a
perceivable benefit to the user.
Copy link
Contributor

@Phergus Phergus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @kwvanderlinde)

@Phergus Phergus merged commit 305d92a into RPTools:develop Nov 23, 2022
@kwvanderlinde kwvanderlinde deleted the refactor/3744-cache-exposed-area branch November 24, 2022 00:06
@cwisniew cwisniew added the performance A performance or quality of life improvement label Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance A performance or quality of life improvement
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

None yet

3 participants