C code nesting: -flto or not -flto
One of my recent programs is very dependent on how to make some hot features for performance. These hot functions are part of an external file .c
that I would rather not modify.
Unfortunately, while Visual is pretty good at this exercise, gcc and clang are not. Apparently due to the hot functions being within the other .c
, they cannot inline them.
This leaves me with 2 options:
- Alternatively, include the relevant code directly in the target file. In practice, this means
#include "perf.c"
instead of#include "perf.h"
. A trivial change, but it looks ugly. It is clear that this works. It's a little trickier to explain the build chain whatperf.c
should be there but not compile or link. -
Use
-flto
to optimize communication times. It looks cleaner and by default Visual reaches.The problem is that during
-flto
the link step, gcc generates several warnings that seem to be internal errors (they refer to a piece of code from the standard libraries, so I have little control over them). It's awkward when you target a "zero warning" policy (even though the generated binary works fine).As far as clang is concerned, it just fails with
-flto
a packaging error (error loading plugin: LLVMgold.so), which seems to be very common on several linux distributions.
2 questions:
- Is there a way to disable these warning messages when used
-flto
on gcc? - Which of the two methods above seems to be the best given pro and con?
- Optional: is there another solution?
According to your comment, you must support gcc 4.4. Since LTO started with gcc 4.5 (with caution regarding earlier versions) the answer should be clear. no-flto
.
So, #include
code with all due care, of course.
Update:
The file extension shouldn't be .c
, but for example .inc
( .i
also a bad idea). Better yet: .h
and change functions to static inline
. This still cannot guarantee nesting, but it is the same for all functions and maintains the appearance of a clean header (although a longer function inline
is still bad style).
Before doing all this, I profile correctly if the code actually has a problem. First of all, you should focus on writing readable and maintainable code.