Now, I know it\'s because there\'s not the overhead of calling a function, but is the overhead of calling a function really that heavy (and worth the bloat of having it inli
let
int sum(const int &a,const int &b)
{
return a + b;
}
int a = sum(b,c);
is equal to
int a = b + c
No jump - no overhead
(and worth the bloat of having it inlined)
It is not always the case that in-lining results in larger code. For example a simple data access function such as:
int getData()
{
return data ;
}
will result in significantly more instruction cycles as a function call than as an in-line, and such functions are best suited to in-lining.
If the function body contains a significant amount of code the function call overhead will indeed be insignificant, and if it is called from a number of locations, it may indeed result in code bloat - although your compiler is as likely to simply ignore the inline directive in such cases.
You should also consider the frequency of calling; even for a large-ish code body, if the function is called frequently from one location, the saving may in some cases be worthwhile. It comes down to the ratio of call-overhead to code body size, and the frequency of use.
Of course you could just leave it up to your compiler to decide. I only ever explicitly in-line functions that comprise of a single statement not involving a further function call, and that is more for speed of development of class methods than for performance.
"A few pushes and a jump to call a function, is there really that much overhead?"
It depends on the function.
If the body of the function is just one machine code instruction, the call and return overhead can be many many hundred %. Say, 6 times, 500% overhead. Then if your program consists of nothing but a gazillion calls to that function, with no inlining you've increased the running time by 500%.
However, in the other direction inlining can have a detrimental effect, e.g. because code that without inlining would fit in one page of memory doesn't.
So the answer is always when it comes to optimization, first of all MEASURE.
There are multiple reasons for inlining to be faster, only one of which is obvious:
The cache utilization can also work against you - if inlining makes the code larger, there's more possibility of cache misses. That's a much less likely case though.
Optimizing compilers apply a set of heuristics to determine whether or not inlining will be beneficial.
Sometimes gain from the lack of function call will outweigh the potential cost of the extra code, sometimes not.
There is no calling and stack activity, which certainly saves a few CPU cycles. In modern CPU's, code locality also matters: doing a call can flush the instruction pipeline and force the CPU to wait for memory being fetched. This matters a lot in tight loops, since primary memory is quite a lot slower than modern CPU's.
However, don't worry about inlining if your code is only being called a few times in your application. Worry, a lot, if it's being called millions of times while the user waits for answers!