perf: change frequency of cached weapon value update #3283
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 "Change frequency of NPC cached weapon value update"
Purpose of change
One of the hotspots of NPC update is regeneration of the AI cache. One part in particular,
ai_cache.my_weapon_value = npc_ai::wielded_value( *this );
, is very slow to process taking around 1/3 of the total singular NPC update time. Looking deeper at whereai_cache.my_weapon_value
is used we see a single access location innpc::character_danger
which is itself only called fromnpc::evaluate_enemy
when the passed parameter is an NPC or Player.Because
ai_cache.my_weapon_value
is not the only consideration when evaluating perceived danger I don't think it will be harmful to the NPC to be a little inaccurate in that consideration by having mildly outdated information.Describe the solution
Pretty simple. Wrapped the line so that it only is evaluated once per minute instead of every turn. 1/60th the executions => 1/60th the execution time at the expense of a little information inaccuracy.
Testing
Started a new world, new character in the refugee center as in #3277 and repeatedly wait for 1 hour, timing with phone timer. Times are in seconds to wait 1 hour in the refugee center with 37 NPCs and 19 Monsters in the reality bubble.
Average time saved per waited hour relative to PR-3277 : 8.806s (27.41% better)
Average time saved per waited hour relative to pre-3277 : 15.606s (40.09% better)