Dynamic Code in D

bearophile bearophileHUGS at lycos.com
Sat Jan 12 13:23:15 PST 2008


Michael Wilson:

>No method inlining is a little more serious, but then I'll have to use GDC rather than DMD for the 64-bit support, and presumably GDC inlines methods ok? If not I could make the GP output stage do it, the call depth and function size is low enough that it shouldn't be difficult.<

Current DMD D is able to inline free functions and more. But Java code is often full of getters/setters, they don't seem to be inlined by DMD, so they may slow down code translated from Java to D.
To put some "facts" behind my words you can perform a quick benchmark:


import std.stdio: put = writef, putr = writefln;
import d.time: clock;

struct S1 { uint s; }

void inc_sA(ref S1 s1) {
    s1.s++;
}

void inc_sA(S1* s1) {
    s1.s++;
}

struct S2 {
    uint s;
    void inc() {
        this.s++;
    }
}

class C1 {
    uint s;
    void inc() {
        this.s++;
    }
}

final class C2 {
    uint s;
    final void inc() {
        this.s++;
    }
}

class C3a : C1 {
    void inc2() {
        this.s++;
    }
}

final class C3b : C1 {
    final void inc2() {
        this.s++;
    }
}


void main() {
    const uint N = 300_000_000;

    S1 s1;
    auto t = clock();
    for(uint i; i < N; ++i)
        s1.s++;
    putr(clock() - t, " ", s1.s);

    s1.s = 0;
    t = clock();
    for(uint i; i < N; ++i)
        inc_sA(s1);
    putr(clock() - t, " ", s1.s);

    s1.s = 0;
    t = clock();
    for(uint i; i < N; ++i)
        inc_sA(&s1); // inlined
    putr(clock() - t, " ", s1.s);

    S2 s2a;
    t = clock();
    for(uint i; i < N; ++i)
        s2a.inc;     // inlined
    putr(clock() - t, " ", s2a.s);

    auto c1a = new C1;
    t = clock();
    for(uint i; i < N; ++i)
        c1a.inc;
    putr(clock() - t, " ", c1a.s);

    auto c2a = new C2;
    t = clock();
    for(uint i; i < N; ++i)
        c2a.inc;
    putr(clock() - t, " ", c2a.s);

    auto c3a = new C3a;
    t = clock();
    for(uint i; i < N; ++i)
        c3a.inc2;
    putr(clock() - t, " ", c3a.s);

    auto c3b = new C3b;
    t = clock();
    for(uint i; i < N; ++i)
        c3b.inc2;
    putr(clock() - t, " ", c3b.s);

    c3a = new C3a;
    t = clock();
    for(uint i; i < N; ++i)
        c3a.inc;
    putr(clock() - t, " ", c3a.s);
}


Timings N = 300_000_000, DMD 1.025

1) -O -release
2) -O -release -inline

  1)     2)
 2.63   2.63
 4.29   4.30
 3.70   2.50 *
 4.30   2.69 *
 4.91   4.91
 4.32   4.29
 4.90   4.90
 4.28   4.29
 4.94   4.92

Someone can run that on GDC too.


>'it's very similar to Java, our developers can learn it easily'.<

Sometimes D can be seen as a superset of Java, but it has other things that you need time to learn, like all the juicy template tricks :-)


>Aside from that the other problems with using Lisp in this app are;<

About them you can post questions on a Lisp newsgroup.


>Otherwise, I could just implement my own synchronization, via inline assembly (since this doesn't have to be multi-platform) or C.<

You are a bold person :-)


>Interesting suggestion but unfotuantely TinyCC does not (AFAIK) target AMD64 - also a problem for a lot of CL implementations. I'll have a look around for similar projects that do though.<

>From the things you have said I think you don't need TinyCC at all.


>As you noted, Hotspot is really quite good these days.<

Behind HotSpot there are quite more developers than behind DMD (but I suspect Walter is better than most of them :-) ).


>LLVM looks much better at first glance and I should be able to target LLVM bytecode directly, but I'll have to investigate the feasibility of linking against D.<

LLVM seems a nice solution. And you can just use C/C++ with LLVM instead of D...

Bye,
bearophile



More information about the Digitalmars-d mailing list