Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(How) Can I inline a particular function call?

Tags:

c

function

inline

Let's say that I have a function that gets called in multiple parts of a program. Let's also say that I have a particular call to that function that is in an extremely performance-sensitive section of code (e.g., a loop that iterates tens of millions of times and where each microsecond counts). Is there a way that I can force the complier (gcc in my case) to inline that single, particular function call, without inlining the others?

EDIT: Let me make this completely clear: this question is NOT about forcing gcc (or any other compiler) to inline all calls to a function; rather, it it about requesting that the compiler inline a particular call to a function.

like image 754
fouric Avatar asked Jan 28 '13 21:01

fouric


People also ask

How do you call a function inline?

To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line.

What is an inline call?

Answer. An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

What is inline function give example?

Example 1. C++ Copy. // inline_keyword1.cpp // compile with: /c inline int max( int a , int b ) { if( a > b ) return a; return b; } A class's member functions can be declared inline, either by using the inline keyword or by placing the function definition within the class definition.

Can we call inline function recursive?

Inline functions can also be recursive. Since the call to an inline function is replaced with the function itself, the overhead of building a parameter list and calling the function is eliminated in the calling function.


Video Answer


2 Answers

In C (as opposed to C++) there's no standard way to suggest that a function should be inlined. It's only vender-specific extensions.

However you specify it, as far as I know the compiler will always try to inline every instance, so use that function only once:

original:

   int MyFunc()  { /* do stuff */  } 

change to:

   inline int MyFunc_inlined()  { /* do stuff */  }     int MyFunc()  { return MyFunc_inlined(); } 

Now, in theplaces where you want it inlined, use MyFunc_inlined()

Note: "inline" keyword in the above is just a placeholder for whatever syntax gcc uses to force an inlining. If H2CO3's deleted answer is to be trusted, that would be:

static inline __attribute__((always_inline)) int MyFunc_inlined()  { /* do stuff */  } 
like image 183
James Curran Avatar answered Oct 18 '22 18:10

James Curran


It is possible to enable inlining per translation unit (but not per call). Though this is not an answer for the question and is an ugly trick, it conforms to C standard and may be interesting as related stuff.

The trick is to use extern definition where you do not want to inline, and extern inline where you need inlining.

Example:

$ cat func.h  int func();  $ cat func.c  int func() { return 10; }  $ cat func_inline.h  extern inline int func() { return 5; }  $ cat main.c        #include <stdio.h>  #ifdef USE_INLINE # include "func_inline.h" #else # include "func.h" #endif  int main() { printf("%d\n", func()); return 0; }  $ gcc main.c func.c && ./a.out 10                                                // non-inlined version  $ gcc main.c func.c -DUSE_INLINE && ./a.out 10                                                // non-inlined version  $ gcc main.c func.c -DUSE_INLINE -O2 && ./a.out 5                                                 // inlined! 

You can also use non-standard attribute (e.g. __attribute__(always_inline)) in GCC) for extern inline definition, instead of relying on -O2.

BTW, the trick is used in glibc.

like image 38
gavv Avatar answered Oct 18 '22 19:10

gavv