Rate This Document
Findability
Accuracy
Completeness
Readability

Function Inlining

Principles

For a small function that is frequently called, the time overhead for calling the function may be much more than that of the function execution. In this case, you can add inline before the function definition to define the function as an inline function. This helps to reduce the time overhead for calling the function and improve program performance.

Function inlining is to embed the function body into the statement that calls the function during compilation. Attributes of function inlining lists the attributes of function inlining in the GCC compiler. According to the following table, functions with the inline attribute may not be inlined. The compiler determines whether to inline a function based on the compilation options and optimization levels used by the program. In addition, factors such as the function body size, whether recursion exists in the function body, and whether the parameters are optional are considered.

Table 1 Attributes of function inlining

Attribute

Description

inline

Recommends the compiler to inline the function, but the operation may not be performed.

always_inline

Forcible inlining.

noline

Prevents function inlining.

Compilation options and optimization levels lists the compilation options during GCC inlining and the optimization levels.

Table 2 Compilation options and optimization levels

Compilation Option

Optimization Level

Description

early-inlining

-O0, -O1, -O2, -O3, -Os

For two types of functions (functions marked as always_inline and functions whose execution time is less than the call time), inlining is completed before the compiler performs inline analysis.

inline-functions-called-once

-O1, -O2, -O3, -Os

Static functions that are called once can be inlined to their callers even if they are not marked as inline. If a function is integrated with a call of a given function, the function itself is not output as assembly code.

inline-small-functions

-O2, -O3, -Os

When the function body is smaller than the expected function call code, the function is integrated into its caller. Therefore, the program size becomes smaller. The compiler tentatively decides which functions are simple enough to be integrated in this way. This option applies to all functions, even those that are not declared as inline.

inline-functions

-O2, -O3, -Os

All functions can be inlined even if they are not declared as inline. The compiler tentatively determines which functions can be integrated in this way.

If a function is integrated with all calls of a given function, and the function is declared static, the function itself is not output as assembly code.

For details, see the GCC manual:

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Modification Method

Before:

int func(int a, int b)
{
    return (a + b)*(a + b);
} 

After:

/* Function inlining is recommended. */
static inline int func(int a, int b)
{
    return (a + b)*(a + b) ;
}

Or:

/* Forcible inlining */
static inline
__attribute__((always_inline)) int func(int a, int b) 
{
    return (a + b)*(a + b) ;
}