Inherent code performance advantages of D over C?

bearophile bearophileHUGS at lycos.com
Fri Dec 6 14:40:07 PST 2013


Walter Bright:

> comes up now and then. I think it's incorrect, D has many 
> inherent advantages in generating code over C:

I think in your list you have missed the point 8, that is 
templates allow for data specialization, or for specialization 
based on compile-time values.

The common example of the first is the C sort() function compared 
to the type specialized one.

An example for the second is code for the kD-tree that is 
specialized on the dimension (coordinate) to slice on:
http://rosettacode.org/wiki/K-d_tree#D

As you see the cyclic selection of the coordinate nextSplit is 
assigned to an enum:

struct KdTree(size_t k, F) {
     KdNode!(k, F)* n;
     Orthotope!(k, F) bounds;

     // Constructs a KdTree from a list of points...
     this(Point!(k, F)[] pts, in Orthotope!(k, F) bounds_) pure {
         static KdNode!(k, F)* nk2(size_t split)(Point!(k, F)[] 
exset)
         pure {
...
             enum nextSplit = (split + 1) % d.length;//cycle 
coordinates



> 2. D knows when functions are pure. C has to make worst case 
> assumptions.

Perhaps D purity were designed for usefulness, code correctness, 
etc. but not to help compilers. I remember some recent 
discussions in this newsgroup by developers of GDC that explained 
why the guarantees D offers over C can't lead to true 
improvements in the generated code. If this is true then perhaps 
D has some features that weren't designed in hindsight of what 
back-ends really need to optimize better.


On this whole subject I remember that pointers in Fortan are 
regarded as so dis-empowered that the Fortran compiler is able to 
optimize their usage better than any pointers in usual C 
programs, even C99 programs that use the "restrict" keyword.


There are also situations where D is slower than D: when D can't 
prove that an array will be accessed in bounds [*]. And when a D 
compiler because of separate compilation can't de-virtualize a 
virtual class method call.

Bye,
bearophile

[*] I will have to say more on this topic in few days.


More information about the Digitalmars-d mailing list