enforce()?

Steven Schveighoffer schveiguy at yahoo.com
Mon Jul 19 14:17:16 PDT 2010


On Mon, 19 Jul 2010 17:01:34 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Steven Schveighoffer:
>> That is the cost of allocation schemes that use fixed size memory  
>> blocks.
>> Especially when they grow in powers of 2.  Tune your app for it, and you
>> won't have this problem.
>
> I did know about the power of 2 allocations for small memory blocks, and  
> I know it's useful to reduce memory fragmentation. So I have tuned my  
> code for that, that's why I have several structs 16 bytes long, but now  
> I have to target 15 bytes, that is not a power of 2 :o)

Hm... unfortunately, I think you will end up in the same boat.  Because  
any struct of size 15 is aligned to be on a 16-byte boundary.  From my  
memory, I don't think the array allocation code takes into account if the  
final element coincides with the pad byte, but I may be wrong.  Make sure  
to test this theory before going through and trying to trim bytes off all  
your structs.  I think if you use 12-byte structs, it will fit fine, but  
then of course, you are wasting 25% memory :)

If you can deal with some manual memory management, you may want to  
pre-allocate a large array of the structs and then use a free list to  
"allocate" and "deallocate" them.  This should pack them in as tightly as  
possible with almost no overhead.  Of course, if you depend on the GC to  
free your elements, then it might be more of a burden to change all your  
code to contain manual memory management.

-Steve


More information about the Digitalmars-d mailing list