Main Content

Enable Component Reuse During Compilation

Optimization techniques for speeding up compilation of large models include scalable compilation, incremental compilation, and memory caching for model compilation artifacts. Memory caching is performed automatically. To take advantage of scalable and incremental compilation, you need to designate reusable components and enable component reuse.

Component Reuse Workflows

Component reuse during compilation helps you to speed up compilation of large models. When you enable component reuse:

  • During the first compilation, the solver compiles one instance of each reusable component and then reuses these compilation artifacts for repeated instances in the model.

  • During subsequent compilations, the compilation time is further reduced because if a reusable component is unchanged, the solver does not recompile it. This optimization applies to all the reusable blocks and subsystems in the model, independent of whether there are multiple instances or just a single instance.

For scalable or incremental compilation, the mechanism for designating reusable components is the same. However, which components you designate as reusable depends on your model composition and the task that you perform. For example:

  • If your model consists of a pattern of repeated components, such as a transmission line or a battery pack, scalable compilation can significantly improve its compilation time. However, you have to consider the complexity level of reusable components, as described in Determine Optimal Complexity Level for Reusable Components. You can try different ways of restructuring your model into repeated components, or you might need to disable scalable compilation for certain subsystems, blocks, or linked libraries. Use the Advisory tool to determine the optimal configuration.

  • If you are making a series of modifications to a subsystem within a large model, designating this subsystem as reusable can increase the compilation speed for subsequent simulations within the same MATLAB® session. Because of incremental compilation, changes made within the reusable subsystem do not cause the rest of the model to recompile.

How to Designate Reusable Components

The mechanism for designating reusable components for scalable and incremental compilation is the same.

To designate a subsystem or block as reusable:


where blockPath is the path to the block or subsystem from the root of the model.

To disable compilation reuse for a block or subsystem within a model, enter:


To query the reuse setting for a block or subsystem, enter:

setting = simscape.reuse.getConfig(blockPath)

To designate textual components as reusable, use the CompileReuse attribute. For more information, see Reuse Compilation Artifacts of Textual Components.

How to Turn On Component Reuse

To turn on component reuse for a model:

  1. In the model window, open the Modeling tab and click Model Settings. The Configuration Parameters dialog box opens.

  2. On the Simscape pane, select the Reuse components during compilation check box.

You can also use the equivalent command-line interface to set the model configuration parameter:


How to Enable or Disable Multithreaded Compilation

Multithreaded compilation helps reduce compilation time by compiling different reusable components on separate threads. Even if you do not enable component reuse, multithreaded compilation can help reduce compilation time because of other optimizations.

On a multicore machine, multithreaded compilation is enabled by default. However, if you require single-thread compilation, such as when you are profiling the compilation performance of a model over multiple runs and want to get comparable results, you can disable multithreaded compilation. In the Configuration Parameters dialog box, at the bottom of the Simscape pane, under Advanced Parameters, clear the Enable multithreaded compilation check box.

You can also use the equivalent command-line interface to set the model configuration parameter:


Related Topics