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

Throttle NPC item search activity alternative #33885

Merged

Conversation

kevingranade
Copy link
Member

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.

@ZhilkinSerg ZhilkinSerg added [C++] Changes (can be) made in C++. Previously named `Code` Code: Performance Performance boosting code (CPU, memory, etc.) NPC / Factions NPCs, AI, Speech, Factions, Ownership labels Sep 8, 2019
@ZhilkinSerg ZhilkinSerg merged commit 8e061f3 into CleverRaven:master Sep 10, 2019
@kevingranade kevingranade deleted the throttle-npc-find-item-v2 branch December 16, 2019 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C++] Changes (can be) made in C++. Previously named `Code` Code: Performance Performance boosting code (CPU, memory, etc.) NPC / Factions NPCs, AI, Speech, Factions, Ownership
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NPC seems to slow down reading/crafting item::tname() the bottleneck with NPC auto-pickup on
2 participants