The latest in our series of blog posts on optimizing embedded software with the aim of improving (i.e. reducing) worst-case execution times looks at leaf functions.
Software optimization techniques which improve worst-case execution times #12: Leaf Functions
Leaf function is the name given to a C function that does not make any function calls. Compilers can typically generate very efficient machine code for leaf functions. This is because the set of registers that would normally be reserved for making function calls and parameter passing is made available for general computation in a leaf function.
It is therefore possible to obtain reductions in the overall worst-case execution time by ensuring that worst-case hotspot code is placed in leaf functions. How this is achieved depends upon the structure of the code. Sometimes it may be worthwhile creating a new function that contains just the hotspot code to ensure that the compiler generates the most efficient machine code implementation. Alternatively, it may be possible to in-line other small functions or implement them via macros (#defines) so that the function containing the hotspot becomes a leaf function.