Inherent code performance advantages of D over C?

Maxim Fomin maxim at maxim-fomin.ru
Fri Dec 6 15:06:58 PST 2013


On Friday, 6 December 2013 at 22:52:46 UTC, Walter Bright wrote:
> On 12/6/2013 2:40 PM, bearophile wrote:
>> 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.
>
> That's a good example.
>
>
>>> 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.
>
> dmd can and does remove multiple calls to strongly pure 
> functions with the same arguments.

and what about holes in immutable, pure and rest type system?

>
>> There are also situations where D is slower than D: when D 
>> can't prove that an
>> array will be accessed in bounds [*].
>
> In the cases where D cannot, can C? Nope. C doesn't even know 
> what an array is. Can any other language? Nope.
>
>
>> And when a D compiler because of separate compilation can't 
>> de-virtualize a virtual class method call.
>
> Can C devirtualize function calls? Nope.

C doesn't have virtual functions. By the way, does D devirtualize 
them? AFAIK it doesn't either, but I do remember spec page was 
talking about it (this is so Dish - advertize optimization trick 
in spec and do not implement it).


More information about the Digitalmars-d mailing list