why allocation of large amount of small objects so slow (x10) in D?

Sean Kelly sean at invisibleduck.org
Thu May 21 21:55:40 PDT 2009


nobody wrote:
>> You've probably hit a corner case for the garbage collector.  When you allocate
>> 20,000,000 Object instances and hold onto the references, the garbage collector
>> probably runs about a zillion times and never finds anything to delete.  If this
>> is a bottleneck, you should temporarily disable the garbage collector in these
>> situations, until you are done with all these allocations, then re-enable it.
> 
> Thanks.  Disble gc improve the speed by 5x:
> 
> $ dmd -O -release allocd.d
> $ time ./allocd
> real    0m4.080s
> user    0m3.644s
> sys     0m0.420s
> 
> 
> $ cat allocd.d
> import core.memory;
> 
> int main() {
>   core.memory.GC.disable();
>   int i, n = 20000000;
>   Object[] oa;
>   oa = new Object[n];
>   for (i = n; i-- > 0; ) {
>     oa[i] = new Object();
>   }
>   core.memory.GC.enable();
> 
>   return 0;
> }

Try:

import core.memory;

int main() {
   core.memory.GC.reserve(80000000);
   core.memory.GC.disable();
   int i, n = 20000000;
   Object[] oa;
   oa = new Object[n];
   for (i = n; i-- > 0; ) {
     oa[i] = new Object();
   }
   core.memory.GC.enable();

   return 0;
}

If the call to reserve fails you could try breaking it up into two calls 
for half the space each.



More information about the Digitalmars-d mailing list