From c43c790b8c3613af49aea0abe83d587c4d363286 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C5=82awomir=20Lach?= <slawek@lach.art.pl>
Date: Sat, 8 Jan 2022 19:45:11 +0100
Subject: [PATCH 3/3] - Speedup ordering counters for more data

---
 server/savegame/savegame3.c | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)

diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c
index b2a7bab6bc..a860e48464 100644
--- a/server/savegame/savegame3.c
+++ b/server/savegame/savegame3.c
@@ -2586,6 +2586,7 @@ static void sg_load_counters (struct loaddata * loading)
   struct city *pcity;
   int i, j, k;
   size_t length;
+  int  counter_map[MAX_COUNTERS];
   int *city_count;
   int city_ccount = secfile_lookup_int_default(loading->file, 0,
                               "savefile.city_counters_order_size");
@@ -2599,6 +2600,21 @@ static void sg_load_counters (struct loaddata * loading)
                                       "savefile.city"
                                       "_counters_order_vector");
 
+  for (k = 0; k < counters_get_city_counters_count(); k++) {
+
+    for (j = k; j < counters_get_city_counters_count(); j++) {
+
+      const char *ctg_rule_name = counter_rule_name(
+        counter_by_index(j, CTGT_CITY));
+      if (0 == fc_strcasecmp(countnames[k], ctg_rule_name)) {
+
+        counter_map[k] = j;
+
+        break;
+      }
+    }
+  }
+
   i = 0;
   while (NULL != (city_count =
     secfile_lookup_int_vec(loading->file, &length,
@@ -2625,18 +2641,9 @@ static void sg_load_counters (struct loaddata * loading)
 
     for (k = 0; k < counters_get_city_counters_count(); k++) {
 
-      for (j = k; j < counters_get_city_counters_count(); j++) {
-
-        const char *ctg_rule_name = counter_rule_name(
-          counter_by_index(j, CTGT_CITY));
-        if (0 == fc_strcasecmp(countnames[k], ctg_rule_name)) {
-
-          int temp = pcity->counter_values[k];
-
-          pcity->counter_values[k] = pcity->counter_values[j];
-          pcity->counter_values[j] = temp;
-        }
-      }
+      int temp = pcity->counter_values[k];
+      pcity->counter_values[k] = pcity->counter_values[counter_map[k]];
+      pcity->counter_values[counter_map[k]] = temp;
     }
 
     i++;
-- 
2.34.1