Package: coder
Control inlining in generated code
coder.inline('always')
coder.inline('never')
coder.inline('default')
coder.inline('always')
forces
inlining of the current
function in the generated code. Place the coder.inline
directive
inside the function to which it applies. The code generator does not inline entry-point
functions and recursive functions. Also, the code generator does not inline functions
into parfor
loops, or inline functions called from
parfor
loops.
coder.inline('never')
prevents
inlining of the current function in the generated code. Prevent inlining when you want
to simplify the mapping between the MATLAB® source code and the generated code. You can disable inlining for
all functions at the command line by using the
-O disable:inline
option of the codegen
command.
coder.inline('default')
uses internal heuristics to determine
whether to inline the current function. Usually, the heuristics produce highly optimized
code. Use coder.inline
only when you need to fine-tune these
optimizations.
In this example, function foo
is not inlined
in the generated
code:
function y = foo(x) coder.inline('never'); y = x; end
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 that you want to generate code for a division function used by a system with limited
memory. To optimize memory use in the generated code, the inline_division
function 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('Cannot divide by 0'); end y = dividend / divisor;