Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not mark everything inline?

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?

like image 867
deft_code Avatar asked Oct 22 '10 18:10

deft_code


People also ask

Should you inline every function?

First, you cannot always inline, e.g. recursive functions might not be always inlinable (but a program containing a recursive definition of fact with just a printing of fact(8) could be inlined). Then, inlining is not always beneficial.

What are disadvantages of inline?

Disadvantages :- 1) May increase function size so that it may not fit on the cache, causing lots of cahce miss. 2) After in-lining function if variables number which are going to use register increases than they may create overhead on register variable resource utilization.

What function should not be inline?

We should not use functions that are I/O bound as inline functions. When large code is used in some function, then we should avoid the inline. When recursion is used, inline function may not work properly.

Is inline keyword useless?

As compilers became better at optimising, this functionality has receded, and using inline as a suggestion to inline a function is indeed obsolete. The compiler will happily ignore it and inline something else entirely if it finds that's a better optimisation.


1 Answers

Did you really mean #include everything? That would give you only a single module and let the optimizer see the entire program at once.

Actually, Microsoft's Visual C++ does exactly this when you use the /GL (Whole Program Optimization) switch, it doesn't actually compile anything until the linker runs and has access to all code. Other compilers have similar options.

like image 139
Ben Voigt Avatar answered Oct 03 '22 20:10

Ben Voigt