D 50% slower than C++. What I'm doing wrong?

Jonathan M Davis jmdavisProg at gmx.com
Sat Apr 14 19:36:12 PDT 2012


On Saturday, April 14, 2012 19:31:40 Jonathan M Davis wrote:
> On Sunday, April 15, 2012 04:21:09 Joseph Rushton Wakeling wrote:
> > On 14/04/12 23:03, q66 wrote:
> > > He also uses a class. And -noboundscheck should be automatically induced
> > > by
> > > -release.
> > 
> > ... but the methods are marked as final -- shouldn't that substantially
> > reduce any speed hit from using class instead of struct?
> 
> In theory. If they don't override anything, then that signals to the
> compiler that they don't need to be virtual, in which case, they
> _shouldn't_ be virtual, but that's up to the compiler to optimize, and I
> don't know how good it is about that right now. Certainly, if you had code
> like
> 
> class C
> {
>     final int foo() { return 42;}
> }
> 
> and benchmarking showed that it was the same speed as
> 
> class C
> {
>     int foo() { return 42;}
> }
> 
> when compiled with -O and -inline, then I'd submit a bug report (maybe an
> enhancement request?) on the compiler failing to make final functions non-
> virtual.

Actually, if you try and benchmark it, make sure that the code can't know that 
the reference is exactly a C. In theory, the compiler could be smart enough to 
know in a case such as

auto c = new C;
auto a = c.foo();

that c is exactly a C and that therefore, it can just inline the call to foo 
even if it's virtual. If c is set from another function, it can't do that. 
e.g.

auto c = bar();
auto a = c.foo();

The compiler _probably_ isn't that smart, but it might be, so you'd have to be 
careful about that.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list