Skip to content

Commit

Permalink
Slightly fewer allocations in npc::process_turn
Browse files Browse the repository at this point in the history
Makes `has_bodypart_with_flag` slightly faster, about 5% better turn
performance.

The method `Character::has_bodypart_with_flag` is called by
`Character::has_flag`, which in turn is called many times as part of
`npc::process_turn`. An example game was measured, which led to 57993
calls to `has_bodypart_with_flag` for 119 npc turns.

This commit changes the implementation in `has_bodypart_with_flag` to
use `Creature::get_body()` instead of `Creature::get_all_body_parts`,
which means that we can use the `bodypart` obj directly without going
through `get_part()` while iterating.
  • Loading branch information
inogenous committed Dec 28, 2023
1 parent f94a31e commit 8af0329
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11729,11 +11729,11 @@ int Character::count_bionic_with_flag( const json_character_flag &flag ) const

bool Character::has_bodypart_with_flag( const json_character_flag &flag ) const
{
for( const bodypart_id &bp : get_all_body_parts() ) {
if( bp->has_flag( flag ) ) {
for( const std::pair<const bodypart_str_id, bodypart> &elem : get_body() ) {
if( elem.first->has_flag( flag ) ) {
return true;
}
if( get_part( bp )->has_conditional_flag( flag ) ) {
if( elem.second.has_conditional_flag( flag ) ) {
return true;
}
}
Expand Down

0 comments on commit 8af0329

Please sign in to comment.