Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I inline a function partially/selectively?

Tags:

c

inline

c99

void run_hot(void) {
    // I am called very often!
    serve();
    // <more code here>
}

void run_cold(void) {
    // I am called only occasionally!
    serve();
    // <more code here>
}

???inline??? void serve(void) {
    // I only want to be called inline from hot functions!
    // <more code here>
}

Is there any way to explicitly inline a function A in a function B while explicitly not inlining the same function A in a function C? Or am I completely at the mercy of my compiler?

like image 287
Will Avatar asked Apr 26 '12 03:04

Will


People also ask

Which situation is not suitable for inline functions?

The compiler will not consider a function to be an inline function if it is recursive. If the function contains one or more static variables, the compiler will deny the request to make it an inline function.

What is extern inline?

The extern-inline module supports the use of C99-style extern inline functions so that the code still runs on compilers that do not support this feature correctly. C code ordinarily should not use inline .

When should I inline a function?

Inline functions are commonly used when the function definitions are small, and the functions are called several times in a program. Using inline functions saves time to transfer the control of the program from the calling function to the definition of the called function.

Can inline functions extern?

Similarly, if you define a function as extern inline , or redeclare an inline function as extern , the function simply becomes a regular, external function and is not inlined.


1 Answers

You are completely at the mercy of the compiler with inlining.
Leave aside partially, whether or not to inline a function is solely a decision that is best made by the compiler and you should rely on it to make the best decision.

like image 90
Alok Save Avatar answered Oct 11 '22 19:10

Alok Save