Cache some commonly used constants

All constants were stored in a g_datalist structure, which has very slow
lookups.  This was not ideally for constansts used during game play
which are accessed thousands of times.  Caching these values makes the
game simulations about 2 times faster.
This commit is contained in:
Tom Stellard 2020-09-12 21:27:49 -07:00
parent f23d0a733a
commit a31ea36c08
9 changed files with 139 additions and 60 deletions

View File

@ -652,6 +652,48 @@ file_load_opt_file(const gchar *filename, OptionList *optionlist, gboolean sort)
new.string_value = g_strdup(opt_value);
g_array_append_val(optionlist->list, new);
}
/* Cache some commonly used options, since datalist lookup can be slow. */
//printf("%s\n", new.name);
#define cache_option(option_name) \
if (!strcmp(new.name, #option_name)) { \
if (!strncmp("float_", new.name, 6)) \
optionlist->option_name = (gfloat)new.value / OPTION_FLOAT_DIVISOR; \
else \
optionlist->option_name = new.value; \
}
cache_option(float_player_streak_influence_skill);
cache_option(float_player_fitness_exponent);
cache_option(float_player_boost_skill_effect);
cache_option(float_player_team_weight_forward_attack);
cache_option(float_player_team_weight_forward_midfield);
cache_option(float_player_team_weight_forward_defense);
cache_option(float_player_team_weight_midfielder_attack);
cache_option(float_player_team_weight_midfielder_midfield);
cache_option(float_player_team_weight_midfielder_defense);
cache_option(float_player_team_weight_defender_attack);
cache_option(float_player_team_weight_defender_midfield);
cache_option(float_player_team_weight_defender_defense);
cache_option(float_player_fitness_decrease_add);
cache_option(float_player_streak_influence_fitness_decrease);
cache_option(float_player_boost_fitness_effect);
cache_option(float_player_fitness_decrease_factor_goalie);
cache_option(float_player_lsu_update_limit);
cache_option(float_player_streak_prob_zero);
cache_option(float_player_fitness_increase_variance);
cache_option(float_player_fitness_decrease_younger_factor);
cache_option(float_player_max_skill);
cache_option(float_player_skill_update_younger_add);
cache_option(float_player_skill_update_younger_factor);
cache_option(float_player_streak_prob_max);
cache_option(float_player_streak_influence_fitness_increase);
cache_option(float_player_fitness_increase_add);
cache_option(float_player_streak_length_lower);
cache_option(float_player_streak_length_upper);
cache_option(float_player_etal_scout_factor);
cache_option(float_player_fitness_increase_younger_factor);
//printf("streak: %f\n", optionlist->float_player_streak_influence_skill);
}
if(sort)

View File

@ -109,15 +109,15 @@ game_get_player_contribution(const Player *pl, gint type, gboolean special)
a match. Rows are player position, columns value type.
@see game_get_player_contribution() */
gfloat player_weights[3][3] =
{{const_float("float_player_team_weight_defender_defense"),
const_float("float_player_team_weight_defender_midfield"),
const_float("float_player_team_weight_defender_attack")},
{const_float("float_player_team_weight_midfielder_defense"),
const_float("float_player_team_weight_midfielder_midfield"),
const_float("float_player_team_weight_midfielder_attack")},
{const_float("float_player_team_weight_forward_defense"),
const_float("float_player_team_weight_forward_midfield"),
const_float("float_player_team_weight_forward_attack")}};
{{const_float_fast(float_player_team_weight_defender_defense),
const_float_fast(float_player_team_weight_defender_midfield),
const_float_fast(float_player_team_weight_defender_attack)},
{const_float_fast(float_player_team_weight_midfielder_defense),
const_float_fast(float_player_team_weight_midfielder_midfield),
const_float_fast(float_player_team_weight_midfielder_attack)},
{const_float_fast(float_player_team_weight_forward_defense),
const_float_fast(float_player_team_weight_forward_midfield),
const_float_fast(float_player_team_weight_forward_attack)}};
return player_get_game_skill(pl, FALSE, special) *
player_weights[pl->cpos - 1][type - GAME_TEAM_VALUE_DEFEND];
@ -228,7 +228,7 @@ game_get_player_probs(GArray *players, gfloat *probs, gfloat *weights, gboolean
(g_array_index(players, Player, 1).cskill != 0);
probs[0] *= (1 + (gfloat)g_array_index(players, Player, 1).streak *
const_float("float_player_streak_influence_skill"));
const_float_fast(float_player_streak_influence_skill));
for(i=1;i<10;i++)
@ -240,7 +240,7 @@ game_get_player_probs(GArray *players, gfloat *probs, gfloat *weights, gboolean
weights[g_array_index(players, Player, i + 1).cpos - 1] *
(g_array_index(players, Player, i + 1).cskill != 0));
probs[i] *= (1 + (gfloat)g_array_index(players, Player, i + 1).streak *
const_float("float_player_streak_influence_skill"));
const_float_fast(float_player_streak_influence_skill));
}
}
@ -1198,11 +1198,11 @@ game_get_max_values(gfloat max_values[4])
gint i, j;
Player pl;
pl.skill = pl.cskill = const_float("float_player_max_skill");
pl.skill = pl.cskill = const_float_fast(float_player_max_skill);
pl.fitness = 1;
pl.streak = 0;
max_values[0] = const_float("float_player_max_skill");
max_values[0] = const_float_fast(float_player_max_skill);
for(i=1;i<4;i++)
{

View File

@ -62,6 +62,7 @@
#define const_str(name) option_string(name, &constants)
#define const_int(name) option_int(name, &constants)
#define const_float(name) option_float(name, &constants)
#define const_float_fast(name) (constants.name)
#define sett_int(name) option_int(name, &settings)
#define sett_set_int(name, value) option_set_int(name, &settings, value)

View File

@ -39,6 +39,42 @@ typedef struct
{
GArray *list;
GData *datalist;
/* Some extra options for faster look up.
* FIXME: We could possibly have separate options types, since we have
* different options instances used in this program.
*/
gfloat float_player_streak_influence_skill;
gfloat float_player_fitness_exponent;
gfloat float_player_boost_skill_effect;
gfloat float_player_team_weight_forward_attack;
gfloat float_player_team_weight_forward_midfield;
gfloat float_player_team_weight_forward_defense;
gfloat float_player_team_weight_midfielder_attack;
gfloat float_player_team_weight_midfielder_midfield;
gfloat float_player_team_weight_midfielder_defense;
gfloat float_player_team_weight_defender_attack;
gfloat float_player_team_weight_defender_midfield;
gfloat float_player_team_weight_defender_defense;
gfloat float_player_fitness_decrease_add;
gfloat float_player_streak_influence_fitness_decrease;
gfloat float_player_boost_fitness_effect;
gfloat float_player_fitness_decrease_factor_goalie;
gfloat float_player_lsu_update_limit;
gfloat float_player_streak_prob_zero;
gfloat float_player_fitness_increase_variance;
gfloat float_player_fitness_decrease_younger_factor;
gfloat float_player_max_skill;
gfloat float_player_skill_update_younger_add;
gfloat float_player_skill_update_younger_factor;
gfloat float_player_streak_prob_max;
gfloat float_player_streak_influence_fitness_increase;
gfloat float_player_fitness_increase_add;
gfloat float_player_streak_length_lower;
gfloat float_player_streak_length_upper;
gfloat float_player_etal_scout_factor;
gfloat float_player_fitness_increase_younger_factor;
} OptionList;
#endif

View File

@ -77,7 +77,7 @@ player_new(Team *tm, gfloat average_talent, gboolean new_id)
new.talent =
CLAMP(average_talent * skill_factor, 0,
const_float("float_player_max_skill"));;
const_float_fast(float_player_max_skill));;
new.skill = player_skill_from_talent(&new);
new.cskill = new.skill;
@ -147,7 +147,7 @@ player_complete_def(Player *pl, gfloat average_talent)
const_float("float_player_peak_region_upper"));
pl->talent = CLAMP(average_talent * skill_factor, 0,
const_float("float_player_max_skill"));
const_float_fast(float_player_max_skill));
pl->skill = player_skill_from_talent(pl);
}
@ -228,18 +228,18 @@ player_skill_from_talent(const Player *pl)
{
while(cur_age > pl->age)
{
cur_age -= ((const_float("float_player_lsu_update_limit") + 2) * 0.0192);
cur_age -= ((const_float_fast(float_player_lsu_update_limit) + 2) * 0.0192);
if(pl->peak_age - cur_age > pl->peak_region)
skill *= (1 - ((pl->peak_age - cur_age) *
const_float("float_player_skill_update_younger_factor") +
const_float("float_player_skill_update_younger_add")));
const_float_fast(float_player_skill_update_younger_factor) +
const_float_fast(float_player_skill_update_younger_add)));
}
}
else
{
while(cur_age < pl->age)
{
cur_age += ((const_float("float_player_lsu_update_limit") + 2) * 0.0192);
cur_age += ((const_float_fast(float_player_lsu_update_limit) + 2) * 0.0192);
if(cur_age - pl->peak_age > pl->peak_region)
skill *= (1 - ((cur_age - pl->peak_age) *
const_float("float_player_skill_update_older_factor") +
@ -267,12 +267,12 @@ player_estimate_talent(Player *pl)
/* the maximal deviance in both directions */
gfloat deviance_bound[2] =
{pl->talent - pl->skill, const_float("float_player_max_skill") - pl->talent};
{pl->talent - pl->skill, const_float_fast(float_player_max_skill) - pl->talent};
for(i=0;i<QUALITY_END;i++)
{
scout_deviance[i] = (i + 1) * const_float("float_player_max_skill") *
(const_float("float_player_etal_scout_factor") / 100);
scout_deviance[i] = (i + 1) * const_float_fast(float_player_max_skill) *
(const_float_fast(float_player_etal_scout_factor) / 100);
/* adjust deviance_bounds with regard to the scout's
deviance */
for(j=0;j<2;j++)
@ -541,9 +541,9 @@ player_compare_substitute_func(gconstpointer a, gconstpointer b, gpointer data)
const Player *pl2 = *(const Player**)b;
gint position = GPOINTER_TO_INT(data);
gfloat skill_for_pos1 = player_get_cskill(pl1, position, FALSE) *
powf(pl1->fitness, const_float("float_player_fitness_exponent")),
powf(pl1->fitness, const_float_fast(float_player_fitness_exponent)),
skill_for_pos2 = player_get_cskill(pl2, position, FALSE) *
powf(pl2->fitness, const_float("float_player_fitness_exponent"));
powf(pl2->fitness, const_float_fast(float_player_fitness_exponent));
gfloat game_skill1 = player_get_game_skill(pl1, FALSE, TRUE),
game_skill2 = player_get_game_skill(pl2, FALSE, TRUE);
gboolean good_structure1 =
@ -781,14 +781,14 @@ gfloat
player_get_game_skill(const Player *pl, gboolean skill, gboolean count_special)
{
gfloat boost = (count_special) ?
1 + const_float("float_player_boost_skill_effect") * pl->team->boost : 1;
1 + const_float_fast(float_player_boost_skill_effect) * pl->team->boost : 1;
gfloat streak = (count_special) ?
1 + (gfloat)pl->streak * const_float("float_player_streak_influence_skill") : 1;
1 + (gfloat)pl->streak * const_float_fast(float_player_streak_influence_skill) : 1;
return (skill) ? pl->skill * boost * streak *
powf(pl->fitness, const_float("float_player_fitness_exponent")) :
powf(pl->fitness, const_float_fast(float_player_fitness_exponent)) :
pl->cskill * boost * streak *
powf(pl->fitness, const_float("float_player_fitness_exponent"));
powf(pl->fitness, const_float_fast(float_player_fitness_exponent));
}
/** Decrease a player's fitness during a match.
@ -801,31 +801,31 @@ player_decrease_fitness(Player *pl)
#endif
gfloat goalie_factor =
1 - const_float("float_player_fitness_decrease_factor_goalie") *
1 - const_float_fast(float_player_fitness_decrease_factor_goalie) *
(pl->cpos == 0);
gfloat boost_factor =
1 + (gfloat)pl->team->boost *
const_float("float_player_boost_fitness_effect");
const_float_fast(float_player_boost_fitness_effect);
gfloat streak_factor = 1 + (gfloat)pl->streak *
const_float("float_player_streak_influence_fitness_decrease");
const_float_fast(float_player_streak_influence_fitness_decrease);
if(pl->age < pl->peak_age - pl->peak_region)
{
pl->fitness -= (((pl->peak_age - pl->peak_region - pl->age) *
const_float("float_player_fitness_decrease_younger_factor") +
const_float("float_player_fitness_decrease_add")) *
const_float_fast(float_player_fitness_decrease_younger_factor) +
const_float_fast(float_player_fitness_decrease_add)) *
goalie_factor * boost_factor * streak_factor);
}
else if(pl->age > pl->peak_age + pl->peak_region)
{
pl->fitness -= (((pl->age - pl->peak_age - pl->peak_region) *
const_float("float_player_fitness_decrease_older_factor") +
const_float("float_player_fitness_decrease_add")) *
const_float_fast(float_player_fitness_decrease_add)) *
goalie_factor * boost_factor * streak_factor);
}
else
{
pl->fitness -= (const_float("float_player_fitness_decrease_add") *
pl->fitness -= (const_float_fast(float_player_fitness_decrease_add) *
goalie_factor * boost_factor * streak_factor);
}
@ -847,10 +847,10 @@ player_update_fitness(Player *pl)
#endif
gfloat variance =
math_rnd(1 - const_float("float_player_fitness_increase_variance"),
1 + const_float("float_player_fitness_increase_variance"));
math_rnd(1 - const_float_fast(float_player_fitness_increase_variance),
1 + const_float_fast(float_player_fitness_increase_variance));
gfloat streak_factor =
1 + (pl->streak * const_float("float_player_streak_influence_fitness_increase"));
1 + (pl->streak * const_float_fast(float_player_streak_influence_fitness_increase));
if(pl->participation)
{
@ -860,16 +860,16 @@ player_update_fitness(Player *pl)
if(pl->age < pl->peak_age - pl->peak_region)
pl->fitness += (((pl->peak_age - pl->peak_region - pl->age) *
const_float("float_player_fitness_increase_younger_factor") +
const_float("float_player_fitness_increase_add")) *
const_float_fast(float_player_fitness_increase_younger_factor) +
const_float_fast(float_player_fitness_increase_add)) *
variance * streak_factor);
else if(pl->age > pl->peak_age + pl->peak_region)
pl->fitness += (((pl->age - pl->peak_age - pl->peak_region) *
const_float("float_player_fitness_increase_older_factor") +
const_float("float_player_fitness_increase_add")) *
const_float_fast(float_player_fitness_increase_add)) *
variance * streak_factor);
else
pl->fitness += (const_float("float_player_fitness_increase_add") *
pl->fitness += (const_float_fast(float_player_fitness_increase_add) *
variance * streak_factor);
pl->fitness = MIN(pl->fitness, 1);
@ -1054,17 +1054,17 @@ player_update_skill(Player *pl)
(user_from_team(pl->team)->youth_academy.av_coach *
const_float("float_youth_academy_lsu_penalty"));
if(pl->lsu < const_float("float_player_lsu_update_limit") ||
if(pl->lsu < const_float_fast(float_player_lsu_update_limit) ||
math_rnd(0, 1) < powf(const_float("float_player_lsu_update_base_prob"),
pl->lsu - const_float("float_player_lsu_update_limit")))
pl->lsu - const_float_fast(float_player_lsu_update_limit)))
return;
pl->lsu = 0;
if(pl->age < pl->peak_age - pl->peak_region)
pl->skill *= (1 + ((pl->peak_age - pl->age) *
const_float("float_player_skill_update_younger_factor") +
const_float("float_player_skill_update_younger_add")));
const_float_fast(float_player_skill_update_younger_factor) +
const_float_fast(float_player_skill_update_younger_add)));
else if(pl->age > pl->peak_age + pl->peak_region)
pl->skill *= (1 - ((pl->age - pl->peak_age) *
const_float("float_player_skill_update_older_factor") +
@ -1145,9 +1145,9 @@ player_update_streak(Player *pl)
gfloat streak_type, streak_prob,
streak_length, decrease_factor = 0;
gfloat streak_prob_factor =
const_float("float_player_streak_prob_max") -
const_float("float_player_streak_prob_zero"),
streak_prob_add = const_float("float_player_streak_prob_zero");
const_float_fast(float_player_streak_prob_max) -
const_float_fast(float_player_streak_prob_zero),
streak_prob_add = const_float_fast(float_player_streak_prob_zero);
/** Player streak is locked. */
if(pl->streak_count < 0)
@ -1189,8 +1189,8 @@ player_update_streak(Player *pl)
/** Now let's find out whether there's a new streak. */
streak_type = math_rnd(-1, 1);
streak_prob = math_rnd(0, 1);
streak_length = math_rnd(const_float("float_player_streak_length_lower"),
const_float("float_player_streak_length_upper"));
streak_length = math_rnd(const_float_fast(float_player_streak_length_lower),
const_float_fast(float_player_streak_length_upper));
if(streak_type < pl->streak_prob &&
((pl->streak_prob > 0 &&
@ -1473,7 +1473,7 @@ player_season_start(Player *pl, gfloat skill_change)
if(skill_change != 0)
{
pl->talent *= (1 + skill_change);
pl->talent = CLAMP(pl->talent, 0, const_float("float_player_max_skill"));
pl->talent = CLAMP(pl->talent, 0, const_float_fast(float_player_max_skill));
pl->skill *= (1 + skill_change);
pl->skill = CLAMP(pl->skill, 0, pl->talent);

View File

@ -115,7 +115,7 @@ team_generate_players_stadium(Team *tm, gfloat av_talent)
average_talent *= skill_factor;
}
average_talent = CLAMP(average_talent, 0, const_float("float_player_max_skill"));
average_talent = CLAMP(average_talent, 0, const_float_fast(float_player_max_skill));
tm->average_talent = average_talent;
if(def_file == NULL)

View File

@ -396,7 +396,7 @@ xml_league_read_text (GMarkupParseContext *context,
new_league.yellow_red = int_value;
else if(state == STATE_AVERAGE_TALENT)
new_league.average_talent =
(float_value / 10000) * const_float("float_player_max_skill");
(float_value / 10000) * const_float_fast(float_player_max_skill);
else if(state == STATE_NAMES_FILE)
misc_string_assign(&new_league.names_file, buf);
else if(state == STATE_BREAK)
@ -465,7 +465,7 @@ xml_league_read_text (GMarkupParseContext *context,
else if(state == STATE_TEAM_AVERAGE_TALENT)
g_array_index(new_league.teams, Team,
new_league.teams->len - 1).average_talent =
(float_value / 10000) * const_float("float_player_max_skill");
(float_value / 10000) * const_float_fast(float_player_max_skill);
else if(state == STATE_TEAM_DEF_FILE)
misc_string_assign(&g_array_index(new_league.teams, Team, new_league.teams->len - 1).def_file, buf);
}

View File

@ -105,7 +105,7 @@ xml_team_read_start_element (GMarkupParseContext *context,
{
state = STATE_PLAYER;
new_player = player_new(team, ((gfloat)team->average_talent / 10000) *
const_float("float_player_max_skill"), TRUE);
const_float_fast(float_player_max_skill), TRUE);
}
else if(strcmp(element_name, TAG_PLAYER_NAME) == 0)
state = STATE_PLAYER_NAME;
@ -211,7 +211,7 @@ xml_team_read_text (GMarkupParseContext *context,
misc_string_assign(&team->symbol, buf);
else if(state == STATE_AVERAGE_TALENT && opt_int("int_opt_load_defs") == 1)
team->average_talent =
(float_value / 10000) * const_float("float_player_max_skill");
(float_value / 10000) * const_float_fast(float_player_max_skill);
else if(state == STATE_FORMATION)
team->structure = int_value;
else if(state == STATE_NAMES_FILE)
@ -224,10 +224,10 @@ xml_team_read_text (GMarkupParseContext *context,
new_player.age = misc_get_age_from_birth(birth_year, int_value);
else if(state == STATE_PLAYER_SKILL && opt_int("int_opt_load_defs") == 1)
new_player.skill = ((gfloat)int_value / 10000) *
const_float("float_player_max_skill");
const_float_fast(float_player_max_skill);
else if(state == STATE_PLAYER_TALENT && opt_int("int_opt_load_defs") == 1)
new_player.talent = ((gfloat)int_value / 10000) *
const_float("float_player_max_skill");
const_float_fast(float_player_max_skill);
else if(state == STATE_PLAYER_POSITION)
new_player.pos = int_value;
}

View File

@ -145,7 +145,7 @@ youth_academy_add_new_player(YouthAcademy *youth_academy)
math_gauss_dist(const_float("float_player_peak_region_lower"),
const_float("float_player_peak_region_upper"));//2;
new.talent = CLAMP(new.talent, 0, const_float("float_player_max_skill"));
new.talent = CLAMP(new.talent, 0, const_float_fast(float_player_max_skill));
new.skill = player_skill_from_talent(&new);
new.cskill = new.skill;
player_estimate_talent(&new);