Abhi said:
- Are there any cardinal rules
as to when a function should not be inlined?
- If a function, say f, is being called
from multiple other functions/classes,
should that function f, be always inlined?
- Does the C++ standard talk about this?
What about gcc 3.x implementation?
- Any resources/pointers would be great help.
The purpose of inline functions is to discourage
manual inlining of functions by programmers
who are concerned about performance.
Programs are easier the read, understand and maintain
if they are decomposed into re-usable functions.
If the function definitions are visible to the compiler
it can *inline* these functions automatically
to eliminate the extra overhead of a function call
and to permit optimizations that are frustrated
by external function definitions.
The problem with inline functions is that
they must be parsed every time you recompile your program
during program development.
The solution is to define both inline and external version
of your function:
#ifndef GUARD_FILE_H
#define GUARD_FILE_H 1
#ifdef EFINE_INLINE
inline
double dot(const double x[], const double y[], int n) {
double t = 0.0;
for (int j = 0; j < n; ++j)
t += x[j]*y[j];
return t;
}
#else //EFINE_INLINE
double dot(const double x[], const double y[], int n);
#endif//EFINE_INLINE
#endif//GUARD_FILE_H
#undef EFINE_INLINE
#include <file.h>
double dot(const double x[], const double y[], int n) {
double t = 0.0;
for (int j = 0; j < n; ++j)
t += x[j]*y[j];
return t;
}
Application programs quickly compile and link
with the external version of the function
during program development, testing and debugging
then are recompiled with C preprocessor option
-DEFINE_INLINE
just before deployment. This final compilation may be much slower
but the result is code which runs much faster.