Skip to content

Commit

Permalink
Remove overhead in get_hunger_description()
Browse files Browse the repository at this point in the history
c229fc4 eliminated most of the cost of
this function by deferring the translation of the hunger string, but
there is still per frame overhead from alloacting an unnecessary map
of std::strings. Making the map values const char* and changing to
a std::array lets us make the whole thing static const, and almost
constexpr except for the std::string 'values' remaining.
  • Loading branch information
akrieger authored and kevingranade committed Oct 9, 2020
1 parent 6ce5e46 commit fc29fb6
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/character.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "character.h"

#include <algorithm>
#include <array>
#include <cctype>
#include <climits>
#include <cmath>
Expand All @@ -10,6 +11,7 @@
#include <memory>
#include <numeric>
#include <ostream>
#include <tuple>
#include <type_traits>

#include "action.h"
Expand Down Expand Up @@ -5046,20 +5048,26 @@ std::pair<std::string, nc_color> Character::get_thirst_description() const

std::pair<std::string, nc_color> Character::get_hunger_description() const
{
std::map<efftype_id, std::pair<std::string, nc_color> > hunger_states = {
{ effect_hunger_engorged, std::make_pair( translate_marker( "Engorged" ), c_red ) },
{ effect_hunger_full, std::make_pair( translate_marker( "Full" ), c_yellow ) },
{ effect_hunger_satisfied, std::make_pair( translate_marker( "Satisfied" ), c_green ) },
{ effect_hunger_blank, std::make_pair( "", c_white ) },
{ effect_hunger_hungry, std::make_pair( translate_marker( "Hungry" ), c_yellow ) },
{ effect_hunger_very_hungry, std::make_pair( translate_marker( "Very Hungry" ), c_yellow ) },
{ effect_hunger_near_starving, std::make_pair( translate_marker( "Near starving" ), c_red ) },
{ effect_hunger_starving, std::make_pair( translate_marker( "Starving!" ), c_red ) },
{ effect_hunger_famished, std::make_pair( translate_marker( "Famished" ), c_light_red ) }
// clang 3.8 has some sort of issue where if the initializer list contains const arguments,
// like all of the effect_* string_id variables which are const string_id, then it fails to
// initialize the array with tuples successfully complaining that
// "chosen constructor is explicit in copy-initialization". Using std::forward_as_tuple
// returns a tuple consisting of correctly implcitly copyable types.
static const std::array<std::tuple<const efftype_id &, const char *, nc_color>, 9> hunger_states{ {
std::forward_as_tuple( effect_hunger_engorged, translate_marker( "Engorged" ), c_red ),
std::forward_as_tuple( effect_hunger_full, translate_marker( "Full" ), c_yellow ),
std::forward_as_tuple( effect_hunger_satisfied, translate_marker( "Satisfied" ), c_green ),
std::forward_as_tuple( effect_hunger_blank, "", c_white ),
std::forward_as_tuple( effect_hunger_hungry, translate_marker( "Hungry" ), c_yellow ),
std::forward_as_tuple( effect_hunger_very_hungry, translate_marker( "Very Hungry" ), c_yellow ),
std::forward_as_tuple( effect_hunger_near_starving, translate_marker( "Near starving" ), c_red ),
std::forward_as_tuple( effect_hunger_starving, translate_marker( "Starving!" ), c_red ),
std::forward_as_tuple( effect_hunger_famished, translate_marker( "Famished" ), c_light_red )
}
};
for( auto &hunger_state : hunger_states ) {
if( has_effect( hunger_state.first ) ) {
return std::make_pair( _( hunger_state.second.first ), hunger_state.second.second );
if( has_effect( std::get<0>( hunger_state ) ) ) {
return std::make_pair( _( std::get<1>( hunger_state ) ), std::get<2>( hunger_state ) );
}
}
return std::make_pair( _( "ERROR!" ), c_light_red );
Expand Down

0 comments on commit fc29fb6

Please sign in to comment.