First off, I am not looking for a way to force the compiler to inline the implementation of every function.
To reduce the level of misguided answers make sure you understand what the inline
keyword actually means. Here is good description, inline vs static vs extern.
So my question, why not mark every function definition inline
? ie Ideally, the only compilation unit would be main.cpp
. Or possibly a few more for the functions that cannot be defined in a header file (pimpl idiom, etc).
The theory behind this odd request is it would give the optimizer maximum information to work with. It could inline function implementations of course, but it could also do "cross-module" optimization as there is only one module. Are there other advantages?
Has any one tried this in with a real application? Did the performance increase? decrease?!?
What are the disadvantages of marking all function definitions inline
?
- Compilation might be slower and will consume much more memory.
- Iterative builds are broken, the entire application will need to be rebuilt after every change.
- Link times might be astronomical
All of these disadvantage only effect the developer. What are the runtime disadvantages?
It is done already in some cases. It is very similar to the idea of unity builds, and the advantages and disadvantages are not fa from what you descibe:
But in cases where you already have a lot of header-only code (for example if you use a lot of Boost), it might be a very worthwhile optimization, both in terms of build time and executable performance.
As always though, when performance is involved, it depends. It's not a bad idea, but it's not universally applicable either.
As far as buld time goes, you have basically two ways to optimize it:
C code typically takes the second option, pretty much to its extreme: almost nothing apart from forward declarations and macros are kept in headers. C++ often lies around the middle, which is where you get the worst possible total build time (but PCH's and/or incremental builds may shave some time off it again), but going further in the other direction, minimizing the number of translation units can really do wonders for the total build time.
sqlite uses this idea. During development it uses a traditional source structure. But for actual use there is one huge c file (112k lines). They do this for maximum optimization. Claim about 5-10% performance improvement
http://www.sqlite.org/amalgamation.html
Little benefit On a good compiler for a modern platform,
inline
will affect only a very few functions. It is just a hint to the compiler, modern compilers are fairly good at making this decision themselves, and the the overhead of a function call has become rather small (often, the main benefit of inlining is not to reduce call overhead, but opening up further optimizations).Compile time However, since inline also changes semantics, you will have to
#include
everything into one huge compile unit. This usually increases compile time significantly, which is a killer on large projects.Code Size
if you move away from current desktop platforms and its high performance compilers, things change a lot. In this case, the increased code size generated by a less clever compiler will be a problem - so much that it makes the code significantly slower. On embedded platforms, code size is usually the first restriction.
Still, some projects can and do profit from "inline everything". It gives you the same effect as link time optimization, at least if your compiler doesn't blindly follow the
inline
.Interesting question! You are certainly right that all of the listed disadvantages are specific to the developer. I would suggest, however, that a disadvantaged developer is far less likely to produce a quality product. There may be no runtime disadvantages, but imagine how reluctant a developer will be to make small changes if each compile takes hours (or even days) to complete.
I would look at this from a "premature optimization" angle: modular code in multiple files makes life easier for the programmer, so there is an obvious benefit to doing things this way. Only if a specific application turns out to run too slow, and it can be shown that inlining everything makes a measured improvement, would I even consider inconveniencing the developers. Even then, it would be after a majority of the development has been done (so that it can be measured) and would probably only be done for production builds.
Suppose
foo()
andbar()
both call somehelper()
. If everything is in one compilation unit, the compiler might choose not to inlinehelper()
, in order to reduce total instruction size. This causesfoo()
to make a non-inlined function call tohelper()
.The compiler doesn't know that a nanosecond improvement to the running time of
foo()
adds $100/day to your bottom line in expectation. It doesn't know that a performance improvement or degradation of anything outside offoo()
has no impact on your bottom line.Only you as the programmer know these things (after careful profiling and analysis of course). The decision not to inline
bar()
is a way of telling the compiler what you know.