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

dsimcha dsimcha at yahoo.com
Thu May 21 17:54:52 PDT 2009


== Quote from nobody (no at where.com)'s article
> $ g++     alloc.cpp   -o alloc
> $ time ./alloc
> real    0m1.946s
> user    0m1.688s
> sys     0m0.256s
> $ dmd -O -release allocd.d
> $ time ./allocd
> real    0m22.734s
> user    0m22.353s
> sys     0m0.360s
> $ cat alloc.cpp
> #include <vector>
> typedef std::vector<int> intvec;
> typedef intvec* intvecp;
> int main() {
>   int i, n = 20000000;
>   intvecp* iva;
>   iva = new intvecp[n];
>   for (i = n; i-- > 0; ) {
>     iva[i] = new intvec();
>   }
>   return 0;
> }
> $ cat allocd.d
> int main() {
>   int i, n = 20000000;
>   Object[] oa;
>   oa = new Object[n];
>   for (i = n; i-- > 0; ) {
>     oa[i] = new Object();
>   }
>   return 0;
> }

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.



More information about the Digitalmars-d mailing list