Help with Template Code

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sun Apr 1 13:19:03 PDT 2007


Jarrett Billingsley wrote:
> "Max Samukha" <samukha at voliacable.com> wrote in message 
> news:nmmv03h5g5mtbkn6hetbnu33ei6nnnhd67 at 4ax.com...
>> I thought it should, too. But when tested on Windows with dmd 1.010,
>> the tuple version is significantly slower. I'm still not sure why.
> 
> Ahh, looking at the disassembly it makes sense now.  What happens is that 
> when you write:
> 
> foreach(i, arg; args)
>     t.tupleof[i] = arg;
> 
> It gets turned into something like _this_:
> 
> typeof(args[0]) arg0 = args[0];
> t.tupleof[0] = arg0;
> typeof(args[1]) arg1 = args[1];
> t.tupleof[1] = arg1;
> typeof(args[2]) arg2 = args[2];
> t.tupleof[2] = arg2;
> 
> Notice it copies the argument value into a temp variable, then that temp 
> variable into the struct.  Very inefficient.
> 
> Unfortunately I don't know of any way to get around this..

Yes, DMD does that, *unless you turn on optimizations* ;).
Measuring performance without optimization switches is pretty much useless.

With optimizations it just moves mem->reg, reg->mem. It generates code 
bit-for-bit identical to:
---
     static S opCall(int x_, float y_, char[] z_) {
	    S s = void;
	    s.x = x_;
	    s.y = y_;
	    s.z = z_;
	    return s;
     }
---
for the version Max posted (with =void)

(The only difference is the mangled name; the mixin name is in there for 
the mixed-in version)


More information about the Digitalmars-d-learn mailing list