Real-Time Workshop | ![]() ![]() |
Expression folding is a code optimization technique that minimizes the computation of intermediate results at block outputs and the storage of such results in temporary buffers or variables. When expression folding is on, Real-Time Workshop collapses, or "folds," block computations into single expressions, instead of generating separate code statements and storage declarations for each block in the model.
Expression folding can dramatically improve the efficiency of generated code, frequently achieving results that compare favorably to hand-optimized code. In many cases, entire groups of model computations fold into a single highly optimized line of code.
By default, expression folding is on. The Real-Time Workshop code generation options are configured to use expression folding wherever possible. Most Simulink blocks support expression folding.
You can also take advantage of expression folding in your own inlined S-function blocks. See Supporting Expression Folding in S-Functions for information on how to do this.
In the code generation examples that follow, note that signal storage optimizations (Signal storage reuse, Buffer reuse and Local block outputs) are turned on.
![]() | General Modeling Techniques | Expression Folding Example | ![]() |