Re: [Freeciv-Dev] (PR#10301) cachemap is way too big

"Jason Short" <[email protected]>
Newsgroups gmane.games.freeciv.ai
Message-ID <[email protected]>
<URL: http://rt.freeciv.org/Ticket/Display.html?id=10301 >

Jason Short wrote:
> <URL: http://rt.freeciv.org/Ticket/Display.html?id=10301 >
> 
> Jason Short wrote:
> 
>><URL: http://rt.freeciv.org/Ticket/Display.html?id=10301 >
>>
>>In aisettler.c, the cachemap is declared to a static size of 520,200 bytes.
>>
>>This seems excessive, since if it were allocated dynamically it would 
>>normally have a size of 32,000 bytes.
> 
> 
> On a related note, the cache_map is used (in cityresult_fill) before it 
> is initialized (in ai_settler_init).

This patch fixes both problems.  It reduces the memory usage of the 
server by 500k.  Autogames are identical.

(Why does the cache_map get all values initialized to -1?)

(Along with the citymap patch, this reduced the memory footprint of a 
random civserver autogame I ran from 12.3 to 11.6 Mb.)

jason
aisettler.diff (text/x-patch, 3.9 KB)
? new
? orig
Index: ai/aisettler.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aisettler.c,v
retrieving revision 1.4
diff -u -r1.4 aisettler.c
--- ai/aisettler.c	26 Aug 2004 22:08:42 -0000	1.4
+++ ai/aisettler.c	26 Sep 2004 01:05:04 -0000
@@ -98,7 +98,8 @@
   char food;
   char trade;
   char shield;
-} cachemap[MAP_MAX_WIDTH][MAP_MAX_HEIGHT];
+} *cachemap;
+#define CACHEMAP(x, y) (cachemap[map_pos_to_index((x), (y))])
 
 /**************************************************************************
   Fill cityresult struct with useful info about the city spot. It must 
@@ -150,7 +151,7 @@
       result->tile[i][j].trade = 0;
       result->tile[i][j].food = 0;
       sum = 0;
-    } else if (cachemap[map_x][map_y].sum <= 0 || city_center) {
+    } else if (CACHEMAP(map_x, map_y).sum <= 0 || city_center) {
       /* We cannot read city center from cache */
 
       /* Food */
@@ -175,16 +176,16 @@
       if (!city_center && virtual_city) {
         /* real cities and any city center will give us spossibly
          * skewed results */
-        cachemap[map_x][map_y].sum = sum;
-        cachemap[map_x][map_y].trade = result->tile[i][j].trade;
-        cachemap[map_x][map_y].shield = result->tile[i][j].shield;
-        cachemap[map_x][map_y].food = result->tile[i][j].food;
+        CACHEMAP(map_x, map_y).sum = sum;
+        CACHEMAP(map_x, map_y).trade = result->tile[i][j].trade;
+        CACHEMAP(map_x, map_y).shield = result->tile[i][j].shield;
+        CACHEMAP(map_x, map_y).food = result->tile[i][j].food;
       }
     } else {
-      sum = cachemap[map_x][map_y].sum;
-      result->tile[i][j].shield = cachemap[map_x][map_y].shield;
-      result->tile[i][j].trade = cachemap[map_x][map_y].trade;
-      result->tile[i][j].food = cachemap[map_x][map_y].food;
+      sum = CACHEMAP(map_x, map_y).sum;
+      result->tile[i][j].shield = CACHEMAP(map_x, map_y).shield;
+      result->tile[i][j].trade = CACHEMAP(map_x, map_y).trade;
+      result->tile[i][j].food = CACHEMAP(map_x, map_y).food;
     }
     result->tile[i][j].reserved = reserved;
 
@@ -445,9 +446,10 @@
 /**************************************************************************
   Prime settler engine.
 **************************************************************************/
-void ai_settler_init(struct player *pplayer)
+void ai_settler_init(void)
 {
-  memset(&cachemap, -1, sizeof(cachemap));
+  cachemap = fc_realloc(cachemap, MAX_MAP_INDEX * sizeof(*cachemap));
+  memset(cachemap, -1, MAX_MAP_INDEX * sizeof(*cachemap));
 }
 
 /**************************************************************************
Index: ai/aisettler.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aisettler.h,v
retrieving revision 1.1
diff -u -r1.1 aisettler.h
--- ai/aisettler.h	5 Aug 2004 11:34:18 -0000	1.1
+++ ai/aisettler.h	26 Sep 2004 01:05:04 -0000
@@ -38,7 +38,7 @@
                      struct cityresult *result);
 void find_best_city_placement(struct unit *punit, struct cityresult *best, 
 			      bool look_for_boat, bool use_virt_boat);
-void ai_settler_init(struct player *pplayer);
+void ai_settler_init(void);
 void print_cityresult(struct player *pplayer, struct cityresult *cr,
                       struct ai_data *ai);
 
Index: server/srv_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/srv_main.c,v
retrieving revision 1.198
diff -u -r1.198 srv_main.c
--- server/srv_main.c	21 Sep 2004 05:51:12 -0000	1.198
+++ server/srv_main.c	26 Sep 2004 01:05:05 -0000
@@ -571,11 +571,9 @@
   nocity_send = TRUE;
 
   /* AI end of turn activities */
+  ai_settler_init(); /* This must be done before auto_settlers_init. */
   auto_settlers_init();
   players_iterate(pplayer) {
-    if (pplayer->ai.control) {
-      ai_settler_init(pplayer);
-    }
     auto_settlers_player(pplayer);
     if (pplayer->ai.control) {
       ai_do_last_activities(pplayer);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.