Inherent code performance advantages of D over C?

bearophile bearophileHUGS at lycos.com
Fri Dec 6 16:29:33 PST 2013


H. S. Teoh:

> 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.


deadalnix:

> LLVM is also able to generate Duff's device on the fly for 
> loops where it make sense.


I have not seen this optimization done on my code (both ldc2 and 
gcc), but I am glad to be wrong on this.

The OracleVM uses a very different unrolling strategy: it splits 
the loop in two loops, the first loop has 2, 4 (or sometimes 8 
times) unrolling and it doesn't contain tests beside one at the 
start and end, followed by a second normal (not unrolled) loop of 
the remaining n % 8 times.
I was able to reach the same performance as Java using this 
strategy manually in D using ldc2.

Bye,
bearophile


More information about the Digitalmars-d mailing list