From 569bacdd6b0f1682e0cf6267fa07a6ac5739a63a Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Sat, 6 Jul 2024 20:37:45 +0200 Subject: [PATCH] Swap two loops in compute_max_stats_heuristics Profiling has shown city_tile_iterate to be expensive, with the governors spending 20% of their time in it. They are used in a double loop with output types. Swap the two loops to have the expensive one outside, speeding up governors by about 15% in LTT. --- common/aicore/cm.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/common/aicore/cm.cpp b/common/aicore/cm.cpp index 3160135d6c..c3f00e302e 100644 --- a/common/aicore/cm.cpp +++ b/common/aicore/cm.cpp @@ -1600,18 +1600,21 @@ static void compute_max_stats_heuristic(const struct cm_state *state, output_type_iterate(stat_index) { - int base = production[stat_index]; + pcity->citizen_base[stat_index] = production[stat_index]; + } - city_tile_iterate(city_map_radius_sq_get(pcity), pcenter, ptile) - { - if (is_free_worked(pcity, ptile)) { - base += city_tile_output(pcity, ptile, is_celebrating, stat_index); + city_tile_iterate(city_map_radius_sq_get(pcity), pcenter, ptile) + { + if (is_free_worked(pcity, ptile)) { + output_type_iterate(stat_index) + { + pcity->citizen_base[stat_index] += + city_tile_output(pcity, ptile, is_celebrating, stat_index); } + output_type_iterate_end; } - city_tile_iterate_end; - pcity->citizen_base[stat_index] = base; } - output_type_iterate_end; + city_tile_iterate_end; set_city_production(pcity); memcpy(production, pcity->prod, sizeof(pcity->prod));