Improve performance of materials::get_rotting #44544
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
SUMMARY: Performance "Improve performance of materials::get_rotting"
Purpose of change
While waiting for the long time near a building with many rotting corpses, I noticed that
materials::get_rotting
shows up in profiler.Describe the solution
Before
materials::get_rotting
was filtering the whole list of materials to find those susceptible to rot.I reused the new versioning mechanism in
generic_factory
(see #44261) to create a static cache that is invalidated with corresponding generic_factory.Then I used such cache to cache the set of materials inside the
materials::get_rotting
.Describe alternatives you've considered
At first I thought of implementing "versioned cache" in the following way:
However, I figured the reference to the generic_factory version
int64_t& factory_verion;
is unsafe if generic factory is not static, so I went with simpler and safer albeit less "pretty" solution.Testing
With debugger I ensured that cache is initialized and reused correctly, and also invalidated and reinitialized when game reloads.
Also added unit tests.
Additional context
Profiling results.
Before:
![image](https://user-images.githubusercontent.com/2865203/94847622-b08fd400-03d7-11eb-87c0-b889029eabc5.png)
After:
![image](https://user-images.githubusercontent.com/2865203/94847641-b5ed1e80-03d7-11eb-944f-be58098f97cb.png)
Note, that although the set of rotting materials is passed around by value, compiler seems to optimize that away, as the set is constant.