Main Content

Control Inlining to Fine-Tune Performance and Readability of Generated Code

Inlining is an optimization technique that replaces a function call with the body of that function. Inlining eliminates the overhead of a function call, which can improve speed. Inlining can also create opportunities to further optimize the generated C/C++ code.

However, depending on your application, too much code inlining can also have disadvantages:

  • Inlining can increase the size of the generated C/C++ code and reduce code readability. For example, suppose that you call a certain function foo many times in your source MATLAB® code. If the code generator always inlines foo, the generated code size increases because foo is inlined every time it is called.

  • When you inline a function, the variables defined in the body of the function are part of the scope of the calling function. Therefore, the space allocated to these variables is not released from the stack when the inlined function completes its execution. When you do not inline a function, the allocated stack space is released when the function returns.

By default, the code generator prioritizes speed when generating standalone code. However, when standalone code contains user-written functions that call MathWorks® functions or MathWorks functions that call user-written functions, the code generator prioritizes code readability. In these cases, except for very small functions, the code generator usually does not inline calls between MathWorks code and user-written code. This behavior can keep the generated code cleaner by preserving the separation between user-written code and MathWorks code. You can change these defaults to fine-tune the inlining settings and generate code that meets the speed, readability, and stack space requirements of your application.

When inlining instructions conflict, the code generator follows a hierarchy of inlining controls to determine whether to inline a function in the generated code:

  • Calling a function using coder.inlineCall or coder.nonInlineCall overrides all other inlining controls.

  • The coder.inline("always") or coder.inline("never") directive inside the body of a MATLAB function overrides global inlining settings and code configuration settings.

  • If no other inlining controls conflict, the code generator exhibits the inlining behavior dictated by code configuration settings.

Control Inlining of a Specific MATLAB Function at the Call Site

To control the inlining of a specific MATLAB function at the call site, call the function using coder.inlineCall or coder.nonInlineCall. These functions override all other inlining controls, including coder.inline directives in the body of the called function, global inlining settings, and code configuration settings.

Control Inlining of a Specific MATLAB Function Within the Function

To instruct the code generator to either always or never inline a certain MATLAB function, use the coder.inline("always") or coder.inline("never") directives inside the body of the function. To learn more about these directives, see coder.inline.

The coder.inline("always") or coder.inline("never") directive inside the body of a MATLAB function overrides global inlining settings and code configuration settings. Some MathWorks functions include a call to the coder.inline directive. This directive thus overrides your global inlining settings and code configuration settings. To force or prevent inlining of such a function, use coder.inlineCall or coder.nonInlineCall.

Control Inlining Using Code Configuration Settings

Your speed and readability requirements for the code generated for user-written functions and the code generated for MathWorks functions might differ from the code generation defaults. You can use code configuration settings to control inlining behavior when MathWorks functions call other MathWorks functions, when user-written functions call other user-written functions, and when user-written functions call or are called by MathWorks functions.

This table shows how to achieve different inlining goals. Set the parameters by navigating to Code Generation > Optimization > Advanced Parameters in the Configuration Parameters dialog box of the Simulink® Coder™ App or set properties in a code configuration object.

Inlining GoalCode Configuration Object Property

Model Configuration Parameter

Control inlining behavior at all call sites where a user-written function calls another user-written function

InlineBetweenUserFunctions

On the Advanced Parameters tab, Inline between user functions

Control inlining behavior at all call sites where a MathWorks function calls another MathWorks function InlineBetweenMathWorksFunctions

On the Advanced Parameters tab, Inline between MathWorks functions

Control inlining behavior at all call sites where user-written function calls a MathWorks function or a MathWorks function calls a user-written function

InlineBetweenUserAndMathWorksFunctions

On the Advanced Parameters tab, Inline between user and MathWorks functions

Set these parameters depending on the needs of your application:

  • "Always" – Inline all functions.

  • "Speed" – Use internal heuristics to determine whether to inline a function. This setting prioritizes optimized code.

  • "Readability" – Prioritize readability by only inlining very small functions. This setting balances code modularity and speed to produce readable code without negatively impacting performance.

  • "Never" – Never inline functions. This setting can negatively impact the performance of the generated code.

When configuring Simulink models with model reference hierarchy, maintain consistent function inlining settings between parent models and their referenced models for compatibility. For example, if the parent model has inlining parameters all set to the option "Readability", then the referenced model should also have these same options set to "Readability". Use the Code Generation Advisor to configure the inlining settings of your Simulink model according to recommended values, optimizing for code generation efficiency and simplifying the debugging process.

Example Inlining Strategy

To balance the speed and readability of the generated code, you can instruct the code generator to perform these actions:

  • Preserve the modularity of the user-written code for better readability, even if doing so reduces the speed of the generated code. For this behavior, set InlineBetweenUserFunctions to "Readability".

  • Generate highly optimized code for MathWorks functions, even if doing so results in less readable code because you are less likely to inspect this part of your code base. For this behavior, set InlineBetweenMathWorksFunctions to "Speed".

  • Preserve modularity between user-written code and MathWorks code. For this behavior, set InlineBetweenUserAndMathWorksFunctions to "Readability". Because only very small MathWorks functions are inlined when inlining is set to "Readability", the generated code looks similar to your MATLAB code.

See Also

| | | | | |

Topics