Our series of blog posts on optimizing embedded software with the aim of improving (i.e. reducing) worst-case execution times continues with a second look at single path code.
Software optimization techniques which improve worst-case execution times #13: Single Path Code (II)
The compiler will typically allocate a number of registers for parameter passing. For example, four 32-bit registers might be used. In this case, functions taking more than 4 parameters will result in parameters being pushed onto the stack, and then accessed from the stack when the function is called, leading to a longer worst-case execution time. Ensuring that functions that are called many times on the worst-case path have 4 or fewer parameters (that all fit in registers) will result in an improved worst-case execution time.
Sometimes a large number of related parameters need to be passed to a function. In this case rather than pass the parameters individually (by value) it is often better to group them together into a structure and instead pass them by reference via a pointer to the structure.
The problem of register spilling (where variables are accessed on the stack) can also occur when large functions are used with many variables in scope at the same time. To avoid this, it is advisable to ensure that worst-case hotspot code is implemented in relatively small functions and that the scope of each variable is localised as far as possible.