Are there any general rules I can use for evaluating whether a modern compiler will inline a function? What is the relative cost of an extra stack frame (I know it’s very small, but is there any way to generally quantify it – within an order of magnitude or so)?
I’m also particularly interested in:
- Can a compiler inline methods defined in a cpp?
- I know some compilers implement some optimizations even in debug (VS uses RVO in debug but not NRVO) – What’s the situation for inlining? I would imagine that it’s disabled so that we can see an expected call stack for debugging.
I’m currently trying to micro-optimize a memory tracking system, specifically ones that also apply without optimization enabled (in debug).
It’s easy to predict and hard to predict. Simple expressions, like:
get optimized with the simplest optimizations (the
(2 * c)“common subexpression” will only be computed once.In C/C++, methods declared inlined generally will be (though not always).
Trickier are loop optimizations and the like. Eg,
the expression
(2 * c)will usually get pulled out of the loop, in a compiler that does “global optimization”, but not in one that does only “local optimization”. And, of course, expressions can get much more complicated and convoluted.Change the body of the above loop to
a = i * (2 * c);, and you progress to a slightly higher level of global optimization known as “loop induction”. A “smart” compiler will figure out to just add2 * c(as precomputed) toafor each iteration through the loop, vs doing the (more expensive) multiply on each iteration.And that’s just scratching the surface.
But I have no idea what the Visual Studio compilers are capable of.