内存使用量
通过生成的代码优化 RAM、ROM 和堆栈空间的使用
可以通过以下方法控制内存消耗:指定代码生成器将变量定义为局部变量或全局变量,以及指定数据如何传递给子系统函数。修改配置设置以减少用于存储数据的内存量。
代码生成器通过合并控制流构造、删除不影响计算结果的模块的无效代码路径和代码来减少 ROM 消耗。您可以通过删除可能不需要的代码(如初始化代码、重置和禁用函数以及防御性代码)来进一步减少 ROM 消耗。
主题
定义局部变量或全局变量
- Customize Stack Space Allocation
Control the maximum allowable stack size to provide some control over whether data is defined locally or globally in the generated code. - 减少信号的内存使用量
启用提供重用为信号分配的内存的能力的参数。 - Enable and Reuse Local Block Outputs in Generated Code
Where possible, the code generator declares block outputs as local variables, so that it can potentially reuse these variables. - 在生成的代码中重用全局模块输出
代码生成器尝试重用全局变量。 - 优化全局变量使用
选择全局变量引用优化来满足您的内存使用量和执行速度要求。 - Optimize Generated Code by Passing Reusable Subsystem Outputs as Individual Arguments
The code generator eliminates data copies from local variables back to global block I/O structures by passing reusable subsystem outputs as individual arguments instead of as a pointer to a structure stored in global memory. - 减少不可重用的子系统函数中的全局变量
为了减少不可重用子系统的全局 RAM,请生成一个通过参量而不是全局变量传递数据的函数接口。 - Reduce Memory Usage for Models Containing Referenced Models
Reduce RAM usage for a referenced model by generating optimized code that reuses buffers or generates reusable temporary buffers to hold referenced model outputs.
减小代码大小
- 删除零初始化代码
控制是否在生成的代码中初始化内部数据(模块状态和模块输出)和外部数据(值为零的根输入端口和输出端口)。 - Remove Reset and Disable Functions from the Generated Code
Remove unreachable (dead-code) instances of the reset and disable functions from the generated code for ERT-based systems that include model referencing hierarchies. - Inline Invariant Signals
Use the numerical values of invariant signals instead of their symbolic names in the generated code. - Inline Numeric Values of Block Parameters
Reduce global RAM usage by inlining the literal numeric values of block parameters. - Optimize Generated Code by Consolidating Redundant If-Else Statements
The code generator optimizes the generated code by combiningif-else
statements that share the same condition. - Eliminate Dead Code Paths in Generated Code
The code generator eliminates dead (that is, unused) code paths from the generated code. - Optimize Generated Code by Combining Multiple for Constructs
The code generator uses data dependency analysis to combinefor
constructs to reduce static code size and runtime branching. - 控制将局部变量设置为零的初始化代码的生成
默认情况下,代码生成器不会明确地将局部变量初始化为零。这会产生更高效的代码,但违反某些编码标准,例如MISRA C++:2008 Rule 0-1-4
(Polyspace Bug Finder)。此示例显示如何禁用此优化以从设置为零的局部变量中删除初始化代码。
减少用于存储数据的内存量
- 重用不同大小和维度的缓冲区
对具有不同大小和形状的矩阵重用缓冲区。 - Reduce Memory Usage for Boolean and State Configuration Variables
Reduce the amount of memory that stores state configuration variables andBoolean
variables. - 优化时间计数器的内存使用量
优化代码生成器为经过时间计数器分配的内存。 - 使用布尔数据优化逻辑信号生成的代码
代码生成器通过将逻辑信号存储为Boolean
数据类型来优化生成的代码。 - 通过将布尔数据打包到位字段来优化生成的代码
代码生成器通过将布尔数据打包到 1 位位域来减少 RAM 消耗。 - 向量运算优化
代码生成器通过用局部变量替换临时局部数组来减少堆栈内存。 - Specify Single-Precision Data Type for Embedded Application
Generate code whose floating-point data types are only single precision, for example, for targeting a single-precision processor. - Generate Efficient Code by Specifying Data Types for Block Parameters
To generate more efficient code, match parameter data types with signal data types or store parameters in smaller data types. - Control Operator Type in Generated Code
Generate code with Logical or Bitwise Operators or a combination of both operators. - Generate Efficient Code Using Unified Analysis
Reduce RAM usage and data copies performing unified analysis at model-level.