Throttle NPC item search activity alternative #33885
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 "Throttle NPC item search."
Purpose of change
Fixes #19240
Fixes #22345
Alternative to #33856
As reported in #19240, NPCs were invoking item::tname() on egregiously large numbers of items (everything within 6 tiles) on every turn when they didn't have anything better to do.
Describe the solution
Adds a little NPC ai cache of tiles where the NPC recently searched for items, It stores the number of items in a given tile and skips processing the tile in the future as long as the number of items stays the same.
Describe alternatives you've considered
item::tname() looks quite difficult to cache or optimize, it checks many attributes of items in order to build the descriptive name, any one of which can trigger changing the string used.
It doesn't look like there's a good alternative to tname either, the player is going to intuitively use the display name to build their filter rules.
I also implemented a coarser cache in #33856 that ends up being slightly more efficient but also less correct because it can cause NPCs to miss new items appearing on the map.
Additional context
The speedup from this in some scenarios is massive, I reproduced this with a very simple test with a single NPC and a moderate amount of items, and when waiting the magnitude of the work the NPC spent searching for items was similar to the work spent generating the vision cache for the player, which is considerable. Various players could easily have 10x as many NPCs and 10x or more the items near them.