Optimizing Code Size in FX3 SDK - KBA230335

Version 3

    Version: **

     

    Translation - Japanese: タイトル:FX3 SDKのコードサイズの最適化- KBA230335- Community Translated (JA)

     

    Question:

    What are the recommended settings to optimize the code size in FX3 SDK?

     

    Answer:

    Identifying the space consumed by each symbol

    Sometimes, when a new feature is added to an existing project, code space might run out. A tool arm-none-eabi-nm bundled with GNU ARM toolchain can be used to identify the symbol consuming more code space.

     

    The GNU nm prints all symbols to an ELF file, while the --size option prints the size of each symbol, and sorts them by size if --size-sort is specified. So, you can identify the symbol consuming the most code space (in decimals) using the following command in command prompt:

     

    arm-none-eabi-nm --print-size –size-sort --radix=d “path of elf file”

     

    This command will print each symbol along with its size in ascending order.

     

    Optimizing the code size

    To optimize the code size, you can use the following methods:

     

    1. Building the project in Release Mode

    While building the project in Debug mode, debugging and symbolic information are included. Also, the optimization options are disabled to enable proper debugging. However, in Release mode, debugging and symbolic information are removed, and optimization options are enabled to reduce code size. Hence, it is recommended to build the project in Release mode to reduce code size. To build the project in Release mode, select Release (Release configuration) from the build drop-down list.

     

    Figure 1. Selecting Release Configuration for Building Project

      

     

    When the project is built using Release Configuration, the Optimization Level is automatically changed to Optimize most (-O3).

    To view the optimization options:

    1. Right-click the project in Project Explorer.
    2. Click Properties from the pop-up menu.
    3. In the Properties dialog, from the left pane, expand C/C++ Build. Click Settings.
    4. Go to the Tool Settings tab.
    5. Click Optimization to view the Optimization Level.

     

    Figure 2. Optimization Settings for a Project

      

     

    2. In some cases, you can reduce the size further by changing the optimization level from Optimize most (-O3) to
    Optimize size (-Os). Select Optimize size (-Os) from the Optimization Level drop-down list (see Figure 2).

     

    3. Another way to reduce the size of a .img file is to use link time optimization. Link time optimization is an optimization pass made across all compilation units, which helps in reducing the overall size of a binary. Use this technique only if you want to reduce the code size even further after using the first two methods.  Also, while this technique is used to reduce the code size, sufficient testing needs to be done to make sure that the functionality of the code is not broken.

     

    To enable link time optimization:

    1. Navigate to Optimization Settings for the project (see Figure 2).
    2. Select Link-time optimizer (-flto) (see Figure 3).

     

    Figure 3. Enabling Link-time Optimizer

      

     

    1. Click Apply, and then click OK.
    2. Before building the project, clean the project once. Right-click the project and click Clean Project.
    3. Now, build the project.