Looking through the codebase, I see a lot of very short helper like
functions that are defined in .c files with prototypes in .h files. This
means that the compiler cannot inline them outside of that .c file. Am I
wrong? How is that not a performance hit?
-finline-small-functions
Integrate functions into their callers when their body is smaller than
expected function call code (so overall size of program gets smaller).
The compiler heuristically decides which functions are simple enough to
be worth integrating in this way.
Enabled at level -O2.
On Friday, February 14, 2014 04:59:26 PM atarob wrote:
On Friday, February 14, 2014 04:59:26 PM atarob wrote:
Looking through the codebase, I see a lot of very short helper like
functions that are defined in .c files with prototypes in .h files.
This
means that the compiler cannot inline them outside of that .c file.
Am I
wrong? How is that not a performance hit?
Think about it. -O2 is a compiler flag. Optimization happens at compile
time. NOT at link time. If the function is defined in another .c file,
how
can the compiler, which compiles on .c file at a time, possibly have
access
to the definition, let alone inline it. That’s why we put inline
function
that are supposed to be called by other .c files in a .h file. Am I
wrong?
On Fri, Feb 14, 2014 at 04:59:26PM -0500, atarob wrote:
Looking through the codebase, I see a lot of very short helper like
functions that are defined in .c files with prototypes in .h files. This
means that the compiler cannot inline them outside of that .c file. Am I
wrong? How is that not a performance hit?
In no particular order:
As already pointed out, smart enough compilers can inline
whatever they want.
Adding all functions to .h files results in unmanagable code, so
there should be a bar somewhere.
In many cases inlining may actually be a bad idea even from
performance point of view, see, e.g., [1].
If inlining is considired to be beneficial, in most
cases nginx uses macros rather than inline functions, see, e.g.,
src/core/ngx_queue.h.
If you think there are functions which needs be be made
inlineable - feel free to suggest.
I was very much unaware of this. The linker actually compiles… Wow.
LTO also allows the linker to silently drop code with undefined
behavior. The compiler and linker are free to do what they want with
UB, including making demons fly out your nose [0].
So if the code includes, for example, signed integer overflow, then it
may result in incorrect results. The baffling thing will be when the
test suite passed earlier becuase the UB was not detected under the
test suite.