Inherent code performance advantages of D over C?

Simen Kjærås simen.kjaras at gmail.com
Sun Dec 8 11:10:46 PST 2013


On 07.12.2013 00:58, H. S. Teoh wrote:
> On Sat, Dec 07, 2013 at 12:40:35AM +0100, bearophile wrote:
> [...]
>> Regarding Java performance matters, from my experience another
>> significant source of optimization in the JavaVM that is often
>> overlooked is that the JavaVM is able to partially unroll even loops
>> with a statically-unknown number of cycles. Currently I think
>> GCC/DMD/LDC2 are not able or willing to do this.
> [...]
>
> Really? I've seen gcc/gdc unroll loops with unknown number of
> iterations, esp. when you're using -O3. It just unrolls into something
> like:
>
> 	loop_start:
> 		if (!loopCondition) goto end;
> 		loopBody();
> 		if (!loopCondition) goto end;
> 		loopBody();
> 		if (!loopCondition) goto end;
> 		loopBody();
> 		if (!loopCondition) goto end;
> 		loopBody();
> 		goto loop_start;
> 	end:	...
>
> I'm pretty sure I've seen gcc/gdc do this before.

The classic way of doing this (I have no idea if it's actually done by 
any compilers) is Duff's Device[1], which turns this code:

     do {
         *to++ = *from++;
     } while(--count > 0);}

into this:

     int n = (count + 7) / 8;
     switch(count % 8) {
     case 0: do {    *to++ = *from++;
     case 7:         *to++ = *from++;
     case 6:         *to++ = *from++;
     case 5:         *to++ = *from++;
     case 4:         *to++ = *from++;
     case 3:         *to++ = *from++;
     case 2:         *to++ = *from++;
     case 1:         *to++ = *from++;
             } while(--n > 0);

[1]: http://en.wikipedia.org/wiki/Duff's_device
-- 
   Simen


More information about the Digitalmars-d mailing list