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

bearophile bearophileHUGS at lycos.com
Fri May 22 09:20:30 PDT 2009


nobody, you can see how the GC interacts with your code also with the following small D1 program:

import std.c.stdlib: malloc;
struct S { int i; }
void main() {
    const N = 20_000_000;

    static if (true) // change this to false
        S** sa = cast(S**)calloc(N, (S*).sizeof);
    else
        auto sa = new S*[N];

    for (int i = N; i-- > 0; )
        sa[i] = new S;
}

(Here I have assumed that null == 0, this isn't true on all CPUs).
If you change the static if to false you will see the program become (on Windows) about 2.5 times slower (if you use malloc it gets even faster, but it's not a fair comparison, because new clears the array).
If you add a disable() before the array creation (and the static if is false), the code gets significantly faster.

Bye,
bearophile



More information about the Digitalmars-d mailing list