Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a modern C/C++ compiler optimize better with the code in header?

I've often heard that it is bad practice to place code in the header, but it's been common to place short functions in the headers, partly to help the compiler optimize better.

The inline keywords can help the compiler determine what functions should be inlined, but aside from that, is there still a reason to have short performance critical functions in the headers? Or does it not matter any more for modern compilers?

like image 836
Jens Andersson Avatar asked Jan 19 '15 15:01

Jens Andersson


1 Answers

Technically, the inline keyword only means that the definition is allowed in multiple translation units. That is, if you have an inline function defined in a header file, and that header gets included in multiple source files, then that is fine. For a non-inline, non-template function, that would be illegal.

But compilers can and do take advantage of being able to see the code of the function that is being called. This happens not only for inline functions but also any other functions whose code may be visible. Many compilers try to make a good guess about whether to inline the code. Having the code be inlined might make the program bigger or smaller, faster or slower. If the compiler can determine that the code is likely to be both faster and smaller when the code is inlined, then it will do it. Otherwise it has to consider the trade-off.

Many modern compilers can do link-time optimization, where code that wasn't inlined to begin with can be inlined during the link phase, with some cost in link time. There may be certain optimization opportunities that are lost when delayed until link-time as well.

In my own experience, I've found that making small functions be inline is typically always a win for both size and speed. For larger functions, I often see it make the programs faster but larger, but I've also seen it rarely make the programs slower and larger. If the performance of a particular function is important, you'll need to do measurements to help make the choice of whether to inline or not.

like image 182
Vaughn Cato Avatar answered Sep 30 '22 00:09

Vaughn Cato