Inexplicable LDC vs GDC speed difference

jerro a at a.com
Wed Feb 13 16:18:21 PST 2013


On Wednesday, 13 February 2013 at 15:07:04 UTC, Joseph Rushton 
Wakeling wrote:
> Following some code posted in d.learn, I've observed a bizarre 
> and (to me) inexplicable difference in code speed depending on 
> whether LDC or GDC is used as the compiler:
> http://forum.dlang.org/post/mailman.1239.1360764028.22503.digitalmars-d-learn@puremagic.com
>
> I'm using latest-from-GitHub versions of both compilers, 
> compiled as release versions.
>
> Anyone have any idea what could be the source of the speed 
> difference?

The speed difference is partly caused by the fact that GDC 
doesn't inline juliaFunction and squarePlusMag. I have a build of 
GDC with always_inline attribute enabled (I just copied a few 
lines of code from some old version of GDC to d-builtins.c of a 
recent version), so I tried adding pragma(attribute, 
always_inline) to those functions. It seems that GDC is unable to 
inline them for some reason.

When I added always_inline to juliaFunction, I got this error:

error: inlining failed in call to always_inline 
?main.Julia!(float).juliaFunction?: function body can be 
overwritten at link time

Here's reduced code that gives the same error when always_inline 
is added to bar:

int bar()(int x)
{
     if (x)
         return 0;

     return 1;
}

int foo(int a)
{
     return bar( a);
}

bar can be inlined if I remove the first pair of parentheses (so 
that it isn't a template).



When I add always_inline to squarePlusMag I get:

error: inlining failed in call to always_inline 
?main.Julia!(float).ComplexStruct.squarePlusMag?: mismatched 
arguments

Reduced code that gives the same error when always_inline is 
added to bar:

struct S
{
     int bar(const S s)
     {
         return 0;
     }
}

int foo()
{
     S s;
     return s.bar(s);
}

bar can be inlined if I remove const.

I have compiled all the samples with -c -O3 -finline-functions 
-frelease.


More information about the D.gnu mailing list