D postmortem

bearophile bearophileHUGS at lycos.com
Tue Jun 24 11:30:50 PDT 2008


I was reading this thread:
http://www.reddit.com/r/programming/info/6ogl6/comments/

And it contains this:
WalterBright:
> I'm not sure what you're encountering with the struct return code. The D compiler does the named value return optimization, the same as C++. It will also return small structs in registers, as C++ compilers do. If you could email me (or put on bugzilla) an example of the problem, I can comment more.<

Stuct/classes usage in DMD is much slower than the same performed in C++ with MinGW.
After two days of benchmarks I have found a way that allows enough speed (and later I have written around that solution the tinyvect module you can find in my libs).

import d.func: Lets2;
struct Vec(TyDato, int n) {
  TyDato[n] data;
  void opAddAssign(Vec!(TyDato, n)* other) {
    mixin( Lets2!("this.data[%s] += other.data[%s];", n) );
  }
}

You have to allocate them like this, with a new:
auto v = new Vec!(long, 3);

The Lets2!() template generates n assignment lines at compile time, according to the fixed size of the array.
Every other way I have tried (about 30 different designs, I think) is 1.3-4 times slower, so if you need such speed you can't use classes, etc. But designing the struct this way doesn't allows to use operation overloading, I think.

Bye,
bearophile



More information about the Digitalmars-d mailing list