Inlining embeds a function directly into the calling function. The goal of this technique is to minimize the execution time of a coded algorithm. To learn how this technique are helpful, let’s look at the example code in the first image below.
Figure 1. Convolution Routines – unoptimized
The vdotp() function is performing a vector dot product, and is called in the for loop of the convolve function. The vdotp() is good function to be inlined because the vdotp() is small in size and is called many times. By adding the inline attribute to the function definition, it will indicate to the compiler this function is inlineable. When the compiler inlines this function into convolve(), variables no longer need to be pushed and popped from the stack to support execution of this code, saving multiple instructions per loop iteration. The tradeoff for inlining is an increase in the program memory size.
Figure 2. Convolution Routine – C level inlining
Figure 2 illustrates how the inline attribute will be translated by the compiler as if it the inlining was performed directly at the C level. Functions that are good targets for inlining are:
- Basic math routines
- Function called many times
- Functions small in size