Inline Code

Inlining is a technique that replaces a function call with the contents (body) of that function. Inlining eliminates the overhead of a function call, but can produce larger C/C++ code. Inlining can create opportunities for further optimization of the generated C/C++ code. The code generator uses internal heuristics to determine whether to inline functions in the generated code. You can use the coder.inline directive to fine-tune these heuristics for individual functions. For more information, see coder.inline.

Prevent Function Inlining

In this example, function foo is not inlined in the generated code:

function y = foo(x)
  coder.inline('never');
  y = x;
end

Use Inlining in Control Flow Statements

You can use coder.inline in control flow code. If the software detects contradictory coder.inline directives, the generated code uses the default inlining heuristic and issues a warning.

Suppose you want to generate code for a division function that will be embedded in a system with limited memory. To optimize memory use in the generated code, the following function, inline_division, manually controls inlining based on whether it performs scalar division or vector division:

function y = inline_division(dividend, divisor)

% For scalar division, inlining produces smaller code
% than the function call itself.  
if isscalar(dividend) && isscalar(divisor)
   coder.inline('always');
else
% Vector division produces a for-loop.
% Prohibit inlining to reduce code size.
   coder.inline('never');
end

if any(divisor == 0)
   error('Can not divide by 0');
end

y = dividend / divisor;

Related Examples

Was this topic helpful?