Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2004:
[Freeciv-Dev] (PR#10985) Calculating city->ai.founder_want kills perform
Home

[Freeciv-Dev] (PR#10985) Calculating city->ai.founder_want kills perform

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#10985) Calculating city->ai.founder_want kills performance; simple patch included
From: "Knut Omang" <komang@xxxxxxxxxxxx>
Date: Thu, 11 Nov 2004 14:58:45 -0800
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=10985 >

I include a small patch I made to speed up the ai calculations which are
very slow in 2.0 compared to before..

In one scenario (a large map, 6 ai's, 2 humans and somewhat into the
game) my Athlon 1600 spent up to two minutes on switching turn.
Fairly early in the game more than 80% of the CPU cycles were spent
calculating ai.founder_want, see included log, first round (-d 3)

I added a parameter similar to the ai.next_recalc for the founder_want
calculation. This made overhead drop from 30 to 3-4 secs from the 1st
round to the following rounds (this log from restarting after fix):

------------------(start log)--------------------
Starter spillet.
> 
Spiller 'Henry Grattan' har nå AI vanskelighetsgrad 'easy'.
Spiller 'Osman' har nå AI vanskelighetsgrad 'easy'.
Spiller 'Jan III Sobieski' har nå AI vanskelighetsgrad 'easy'.
Spiller 'Llywelyn ap Gruffudd' har nå AI vanskelighetsgrad 'easy'.
Spiller 'Peregrin' har nå AI vanskelighetsgrad 'easy'.
Spiller 'Batthyány' har nå AI vanskelighetsgrad 'easy'.
3: End/start-turn server/ai activities: 0,99 seconds
> 
3: Harald HÃ¥rfagre's autosettlers consumed 10 milliseconds.
3: George Washington's autosettlers consumed 10 milliseconds.
3: Henry Grattan's autosettlers consumed 1320 milliseconds.
3: Osman's autosettlers consumed 960 milliseconds.
3: Suggesting city name for Jan III Sobieski at (48,141)
3: Jan III Sobieski's autosettlers consumed 1570 milliseconds.
3: Llywelyn ap Gruffudd's autosettlers consumed 950 milliseconds.
3: Suggesting city name for Peregrin at (165,157)
3: Peregrin's autosettlers consumed 1030 milliseconds.
3: Batthyány's autosettlers consumed 1290 milliseconds.
3: autoattack consumed 10 milliseconds.
3: End/start-turn server/ai activities: 35,89 seconds

3: Harald HÃ¥rfagre's autosettlers consumed 0 milliseconds.
3: George Washington's autosettlers consumed 10 milliseconds.
3: Suggesting city name for Henry Grattan at (139,52)
3: Henry Grattan's autosettlers consumed 380 milliseconds.
3: Suggesting city name for Osman at (68,47)
3: Suggesting city name for Osman at (89,39)
3: Suggesting city name for Osman at (91,33)
3: Suggesting city name for Osman at (99,50)
3: Osman's autosettlers consumed 360 milliseconds.
3: Suggesting city name for Jan III Sobieski at (56,131)
3: Jan III Sobieski's autosettlers consumed 60 milliseconds.
3: Suggesting city name for Llywelyn ap Gruffudd at (22,168)
3: Suggesting city name for Llywelyn ap Gruffudd at (26,21)
3: Llywelyn ap Gruffudd's autosettlers consumed 40 milliseconds.
3: Peregrin's autosettlers consumed 30 milliseconds.
3: Suggesting city name for Batthyány at (17,62)
3: Suggesting city name for Batthyány at (9,30)
3: Batthyány's autosettlers consumed 560 milliseconds.
3: autoattack consumed 0 milliseconds.
3: End/start-turn server/ai activities: 3,61 seconds
...
3: End/start-turn server/ai activities: 4,06 seconds
...
3: End/start-turn server/ai activities: 3,93 seconds
----------(end log)---------------

Included patch from CVS, 2.0 branch as of today,

Regards,

-- 
Knut Omang, Ph.D.               Mail:   Knut.Omang@xxxxxxx 
Senior Research Scientist       Web:    http://www.fast.no/ 
Fast Search & Transfer ASA      Phone:  +47 23 01 12 17
P.O. Box 1677 Vika              Fax:    +47 23 01 12 01
NO-0120 Oslo, NORWAY            Mob:    +47 48 01 12 17

? manual/manual1.html
? manual/manual2.html
? manual/manual3.html
? manual/manual4.html
? manual/manual5.html
Index: ai/aicity.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aicity.c,v
retrieving revision 1.176.2.1
diff -u -r1.176.2.1 aicity.c
--- ai/aicity.c 31 Oct 2004 21:53:07 -0000      1.176.2.1
+++ ai/aicity.c 11 Nov 2004 22:55:43 -0000
@@ -973,8 +973,13 @@
     establish_city_distances(pplayer, pcity);
     /* Will record its findings in pcity->settler_want */ 
     contemplate_terrain_improvements(pcity);
-    /* Will record its findings in pcity->founder_want */ 
-    contemplate_new_city(pcity);
+
+    if (pcity->ai.next_founder_want_recalc <= game.turn) {
+      /* Will record its findings in pcity->founder_want */ 
+      contemplate_new_city(pcity);
+      /* Avoid recalculating all the time.. */
+      pcity->ai.next_founder_want_recalc = game.turn + myrand(RECALC_SPEED) + 
RECALC_SPEED;
+    } 
   } city_list_iterate_end;
 
   city_list_iterate(pplayer->cities, pcity) {
Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.249.2.4
diff -u -r1.249.2.4 city.c
--- common/city.c       10 Nov 2004 17:02:03 -0000      1.249.2.4
+++ common/city.c       11 Nov 2004 22:55:45 -0000
@@ -2466,6 +2466,9 @@
   pcity->ai.attack = 0;
   pcity->ai.next_recalc = 0;
 
+  pcity->ai.founder_want = 0; /* calculating this is really expensive */
+  pcity->ai.next_founder_want_recalc = 0; /* avoid burning CPU on it */
+
   pcity->corruption = 0;
   pcity->shield_waste = 0;
   pcity->shield_bonus = 100;
Index: common/city.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.h,v
retrieving revision 1.163.2.2
diff -u -r1.163.2.2 city.h
--- common/city.h       10 Nov 2004 17:02:03 -0000      1.163.2.2
+++ common/city.h       11 Nov 2004 22:55:46 -0000
@@ -202,6 +202,7 @@
 
   /* so we can contemplate with warmap fresh and decide later */
   int settler_want, founder_want; /* for builder (F_SETTLERS) and founder 
(F_CITIES) */
+  int next_founder_want_recalc; /* Not recalc founder_want every turn */
   bool founder_boat; /* if the city founder will need a boat */
   int invasion; /* who's coming to kill us, for attack co-ordination */
   int attack, bcost; /* This is also for invasion - total power and value of

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#10985) Calculating city->ai.founder_want kills performance; simple patch included, Knut Omang <=