Why is not inlining that bad?

Bruce Adams tortoise_74 at yeah.who.co.uk
Mon Oct 8 01:09:34 PDT 2007


Janice Caron Wrote:

> Forgive me if this is a dumb question, but in other threads I've seen
> it argued that dereferencing an address from a register offset is not
> something that anyone needs to be worried about, and that array
> accesses are so fast that no one need worry about them, etc.
> 
> This being the case, why is anyone worried about the overhead of a
> function call? It's just a memory write and a few registers changing,
> surely? It's not a massively expensive operation like a thread switch
> or anything, so why worry?
> 
> If the hardware does memory caching, the return may not even need a
> memory access.
> 
> What am I missing? Is it just that D initializes all its local
> variables as part of calling a function? If so, there are plenty of
> ways around that.

Actually the overhead of a function call is significant, at least in some cases. You have to push all the variables onto the stack. If there are a lot of them and particularly if they are pass by value this makes a difference. You are changing the program counter to a different location which might well not be in the instruction cache. Anyway, if your loop is executed 10,000 times then this overhead may be significant. Also if the code is inline the compiler can optimise it as block. This might include moving some initialisation steps outside the loop. It can also ensure that variables stay in the same registers. 

In general you should prefer optimising your code more by changing the algorithm so the body of the loop is executed less often but sometimes inlining can make the difference.

Regards,

Bruce.



More information about the Digitalmars-d mailing list