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

perf: change frequency of cached weapon value update #3283

Merged

Conversation

OrenAudeles
Copy link
Collaborator

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 where ai_cache.my_weapon_value is used we see a single access location in npc::character_danger which is itself only called from npc::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.

Mode Run 1 Run 2 Run 3 Run 4 Run 5 Average
Pre #3277 38.87 38.92 38.63 39.36 38.86 38.928
#3277 31.94 32.40 32.50 32.05 31.75 32.128
This PR 23.51 23.20 23.04 23.12 23.74 23.322

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)

@github-actions github-actions bot added the src changes related to source code. label Sep 27, 2023
@scarf005 scarf005 self-requested a review September 27, 2023 03:31
@scarf005 scarf005 self-assigned this Sep 27, 2023
Copy link
Member

@scarf005 scarf005 left a comment

Choose a reason for hiding this comment

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

image

impressive.

test method: waited 6h at refugee center with debug sight and timers enabled

@scarf005 scarf005 added this pull request to the merge queue Sep 27, 2023
Merged via the queue into cataclysmbnteam:upload with commit 6d4eaba Sep 27, 2023
13 of 14 checks passed
@OrenAudeles OrenAudeles deleted the periodic-weapon-evaluation branch September 27, 2023 03:41
scarf005 pushed a commit to scarf005/Cataclysm-BN that referenced this pull request Sep 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
src changes related to source code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants