Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Dynamic memory allocation question

Author: Ratko V Tomic

Date: 23:49:57 12/02/02

Go up one level in this thread


Back in the days of 266 MHz PII (4-5 years ago) I wrote a little
test program to malloc & free few thousand blocks in random order
and in random block sizes and I timed each call with microsecond
timer. On 266 MHz machine (and MSVC6) the malloc-s were on average
about 8 microseconds, free-s about 11. Since the app I was writing
couldn't tolerate such delays and preallocating wasn't viable (since
it wasn't known in advance how much it might need in all cases),
I ended up writing an allocator which used malloc() to get larger
blocks only when needed. Inside these blocks I would use buckets
of given block sizes (indexed by array with 4-8 byte block granularity)
so that once a small block was alloced and freed it would be added
to a linked list via pointer free_lst[size]. Next alloc of the same
size (within granularity) would pick from free_lst[size]. This way
the code practically operated as if memory was preallocated except
that instead of having to know in advance how much to reserve for
each block size, it used situation specific high-water mark for
each size. The average alloc/free were about 8-10 times faster
than using malloc/free. Since I knew (from the application) on each
free how large the block was, additional advantage vs malloc/free
was that there was no memory overhead for each block, i.e. if I
asked for say 96 bytes, the returned block would use exactly 96 bytes
from the larger heap, while malloc would have needed 96+8=104 bytes
(8 bytes for block header so it could free/skip it).




This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.