inline functions

Jonathan M Davis jmdavisProg at gmx.com
Sat Mar 26 01:47:13 PDT 2011


On 2011-03-26 01:06, Caligo wrote:
> On Fri, Mar 25, 2011 at 11:56 PM, Jonathan M Davis <jmdavisProg at gmx.com> 
wrote:
> > On 2011-03-25 21:21, Caligo wrote:
> >> On Fri, Mar 25, 2011 at 10:49 PM, Jonathan M Davis <jmdavisProg at gmx.com>
> > 
> > wrote:
> >> > On 2011-03-25 19:04, Caligo wrote:
> >> >> T[3] data;
> >> >> 
> >> >> T dot(const ref Vector o){
> >> >>     return data[0] * o.data[0] + data[1] * o.data[1] + data[2] *
> >> >> o.data[2]; }
> >> >> 
> >> >> T LengthSquared_Fast(){ return data[0] * data[0] + data[1] * data[1]
> >> >> + data[2] * data[2]; }
> >> >> T LengthSquared_Slow(){ return dot(this); }
> >> >> 
> >> >> 
> >> >> The faster LengthSquared() is twice as fast, and I've test with GDC
> >> >> and DMD.  Is it because the compilers don't inline-expand the dot()
> >> >> function call?  I need the performance, but the faster version is too
> >> >> verbose.
> >> > 
> >> > It sure sounds like it didn't inline it. Did you compile with -inline?
> >> > If you didn't then it definitely won't inline it.
> >> > 
> >> > - Jonathan M Davis
> >> 
> >> I didn't know I had to supply GDC with -inline, so I did, and it did
> >> not help.  In fact, with the -inline option the performance gets worse
> >> (for DMD and GDC), even for code that doesn't contain any function
> >> calls.  In any case, code compiled with DMD is always behind GDC when
> >> it comes to performance.
> > 
> > I don't know what gdc does, but you have to use -inline with dmd if you
> > want it to inline anything. It also really doesn't make any sense at all
> > that inlining would harm performance. If that's the case, something
> > weird is going on. I don't see how inlining could _ever_ harm
> > performance unless it just makes the program's binary so big that _that_
> > harms performance. That isn't very likely though. So, if using -inline
> > is harming performance, then something weird is definitely going on.
> > 
> > - Jonathan M Davis
> 
> The only time that -inline has no effect is when I turn on -O3.  This
> is also when the code performs the best.  I've never used -O3 in my
> C++ code, but I guess things are different in D even with the same
> back-end.

I really don't know what gdc does. With dmd, inlining is not turned on unless 
-inline is used. Also, -inline with dmd does not force inlining, it merely 
turns on the optimization. The compiler still chooses where and when it's best 
to inline.

With gcc, I believe that inlining is normally turned on at a pretty low 
optimization level (probably -O), and like dmd, it chooses where and when it's 
best to inline, but unlike dmd, it uses the inline keyword in C++ as a hint as 
to what it should do. However, -O3 forces inlining on all functions marked 
with inline. How gdc deals with that given that D doesn't have an inline 
keyword, I don't know.

Regardless, given what inlining does, I have a _very_ hard time believing that 
it would ever degrade performance unless it's buggy.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list