Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2003:
[Freeciv-Dev] Re: (PR#6182) remove some static map-sized arrays
Home

[Freeciv-Dev] Re: (PR#6182) remove some static map-sized arrays

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#6182) remove some static map-sized arrays
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 17 Sep 2003 12:14:19 -0700
Reply-to: rt@xxxxxxxxxxxxxx

Gregory Berkolaiko wrote:
> On Wed, 17 Sep 2003, Jason Short wrote:
> 
> 
>>There are two remaining MAP_MAX_WIDTH * MAP_MAX_HEIGHT arrays: in 
>>settlers.c.
>>
>>These present problems when increasing MAP_MAX_WIDTH and MAP_MAX_HEIGHT. 
>>  They also break under gen-topologies.  And of course they waste memory 
>>in general.
>>
>>The attached patch replaces them with dynamic arrays, as is used 
>>everywhere else.  Autogames are identical and slightly faster (probably 
>>because of the direct memset to territory).
> 
> 
> Also smaller memory chunks make a surprising difference.
> 
> BTW, I appreciate the elegance of using  
>       mem = realloc(mem, ..)                  (1)
> instead of
>       if (!mem) {
>         mem = malloc(...)                     (2)
>       }

Since you may run multiple games with one civserver, the realloc (or 
some other cleverness) is necessary.

> but I suspect some (naive) realloc implementations act like this:
>       new = malloc(..)
>       memcpy(old, new, ..)
>       free(old)
>       return new
> which means you are wasting a lot of time.

It's possible.  The minimap variable is only allocated once - at the 
beginning of each game, when generate_minimap is called - but the other 
one has realloc called much more often.  I considered renaming 
generate_minimap as init_settlers() and moving the realloc call in 
there.  Should I do that?

> Even cleverer implementations do a lot of cruft checks, to see if some 
> memory could be split off etc.  I think using good old (1) is more 
> transparent and reliable.

I tend to agree that having realloc be called every time is bad, 
although I did measure both ways and on my system there was no 
appreciable difference.

jason




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