[GRASS-dev] char[1024]

Michael Tiemann tiemann at redhat.com
Tue Nov 21 08:14:45 EST 2006


On Tue, 2006-11-21 at 16:42 +1300, Hamish wrote:
> may string variables allocate memory in powers of 2, e.g. char[128] or
> char[1024]. Lots of places in GRASS you see char[100] or char[1000] for
> buffers. Is there a big advantage to fitting exactly in 1024 bytes for
> something like a G_getl() string input buffer?

In my dark and distant past when I was a developer, we always peered
deeply into the implementation of malloc to try to figure out the best
and fastest way to allocate memory.  One particularly popular
implementation of malloc at the time (this is the 1980s) was an
allocator that maintained buckets of memory that were powers of 2 in
length, making it very quick (and sometimes inefficient) to allocate and
deallocate memory.  malloc itself tacked on some overhead, so
allocations of 1024 were actually terrible (it would overflow into the
2048 allocation), leaving 1020 bytes of wasted space, which in turn
would impact swapping performance on 4MB machines if you did that 1,000
times.  But if you allocated the correct amount (power of two minus
overhead size), you were OK.

In today's world where 512MB is normal for laptops, where 1+ GHz
processors are standard (and 2+G Ghz dual quad cores are around the
corner for desktops), and where GNU malloc is now relatively efficient
for all sizes of allocations, the best thing is to allocate what you
actually need and let the computer figure out how to give it to you.

M





More information about the grass-dev mailing list