Package: coder

Control inlining in generated code




coder.inline('always') forces inlining of the current function in generated code.

coder.inline('never') prevents inlining of the current function in generated code. For example, you may want to prevent inlining to simplify the mapping between the MATLAB® source code and the generated code.

coder.inline('default') uses internal heuristics to determine whether or not to inline the current function.

In most cases, the heuristics used produce highly optimized code. Use coder.inline only when you need to fine-tune these optimizations.

Place the coder.inline directive inside the function to which it applies. The code generation software does not inline entry-point functions.

coder.inline('always') does not inline functions called from parfor-loops. The code generation software does not inline functions into parfor-loops.


Preventing Function Inlining

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

function y = foo(x)
  y = x;

Using coder.inline 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)
% Vector division produces a for-loop.
% Prohibit inlining to reduce code size.

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

y = dividend / divisor;

More About

collapse all


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.

Introduced in R2011a

Was this topic helpful?