Dynamic Arrays & Class Properties

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Aug 28 07:28:28 PDT 2008


"Mason Green" <mason.green at gmail.com> wrote in message 
news:g96346$94o$1 at digitalmars.com...
> Jarrett Billingsley Wrote:
>
>>
>> There's no memory/register overhead; ConstArrayReference!(T).sizeof ==
>> (T[]).sizeof.
>>
>> And if you use -inline, there's no function call overhead either.  :)
>>
>>
>
> Ah, thanks for the tip!
>
> BTW, how much does using the -inline compiler flag have an effect on 
> improving performance? Anyone have a good explanation as to how it works?
>
> Thanks,
> Mason

It uh, inlines function calls.  So if you have a small function like:

int getSomething(ref SomeStruct s)
{
    return s.x * SomeConstant;
}

and you call it:

auto foo = getSomething(s);

It can inline the call to that function, making it effectively:

auto foo = s.x * SomeConstant;

but with the advantage that you don't have to break the rules of the 
language to do it (i.e. even if s.x is private, this will still work. 
Removing function calls, especially in tight loops and with 
very-often-called functions, *can* really improve performance.

The compiler has a cost/benefit heuristic that it uses to decide what 
functions should be inlined and what functions should just be called at 
runtime.  I'm not entirely sure the rules it uses but I know that any loops 
used in the function automatically disqualifies it for inlining.  More or 
less if your function is a one-liner that just evaluates an expression you 
can almost be guaranteed that it'll be inlined.

Notice before I said that inlining *can* improve performance.  It also *can* 
make it worse.  If it inlines too much, tight loops can become too large to 
fit into the processor's instruction cache and the code can actually run 
slower than if it were a separate function.  But in many cases, inlining 
improves perfomance.  How much is certainly a function of your coding style, 
what mood the compiler is in, and the phase of the moon.

Be warned though: the DMD backend sometimes generates buggy or incorrect 
code when using -inline, so be sure to test thoroughly both with and without 
the flag. 




More information about the Digitalmars-d-learn mailing list