Complete.Org: Mailing Lists: Archives: freeciv-dev: October 2003:
[Freeciv-Dev] (PR#6574) don't recalculate cost in really_generate_warmap
Home

[Freeciv-Dev] (PR#6574) don't recalculate cost in really_generate_warmap

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#6574) don't recalculate cost in really_generate_warmap
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 19 Oct 2003 19:33:33 -0700
Reply-to: rt@xxxxxxxxxxxxxx

In really_generate_warmap(), for each node that is looked at 
WARMAP_[SEA]COST(x,y) (x,y is the position of the node) is in most cases 
called either 8 or 16 times (one or two times for each adjacent tile). 
But there's no reason to call it more than once.  The only cost of doing 
so is an extra check on move_type.

In my limited testing this resulted in a 0.6 - 1.9% decrease in runtime 
of an autogame.

We could also cache &WARMAP_[SEA]COST(x1,y1).  This value is called 2 or 
3 times for each tile.  The gaimes from this seem pretty minimal.

jason

Index: server/gotohand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/gotohand.c,v
retrieving revision 1.175
diff -u -r1.175 gotohand.c
--- server/gotohand.c   2003/10/10 19:29:06     1.175
+++ server/gotohand.c   2003/10/20 02:27:34
@@ -293,11 +293,16 @@
   /* (?) was punit->type == U_SETTLERS -- dwp */
 
   while (get_from_mapqueue(&x, &y)) {
+    /* Just look up the cost value once.  This is a minor optimization but
+     * it makes a big difference since this code is called so much. */
+    unsigned char cost = ((move_type == SEA_MOVING)
+                         ? WARMAP_SEACOST(x, y) : WARMAP_COST(x, y));
+
     ptile = map_get_tile(x, y);
     adjc_dir_iterate(x, y, x1, y1, dir) {
       switch (move_type) {
       case LAND_MOVING:
-       if (WARMAP_COST(x1, y1) <= WARMAP_COST(x, y))
+       if (WARMAP_COST(x1, y1) <= cost)
          continue; /* No need for all the calculations */
 
         if (is_ocean(map_get_terrain(x1, y1))) {
@@ -321,7 +326,7 @@
                       (ptile->move_cost[dir] > tmp ? 1 : 0))/2;
         }
 
-        move_cost += WARMAP_COST(x, y);
+        move_cost += cost;
         if (WARMAP_COST(x1, y1) > move_cost && move_cost < maxcost) {
           WARMAP_COST(x1, y1) = move_cost;
           add_to_mapqueue(move_cost, x1, y1);
@@ -331,7 +336,7 @@
 
       case SEA_MOVING:
         move_cost = SINGLE_MOVE;
-        move_cost += WARMAP_SEACOST(x, y);
+        move_cost += cost;
         if (WARMAP_SEACOST(x1, y1) > move_cost && move_cost < maxcost) {
          /* by adding the move_cost to the warmap regardless if we
             can move between we allow for shore bombardment/transport

[Prev in Thread] Current Thread [Next in Thread]