Translation - Japanese: タイトル：FX3 SDKのコードサイズの最適化- KBA230335- Community Translated (JA)
What are the recommended settings to optimize the code size in FX3 SDK?
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
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:
- Right-click the project in Project Explorer.
- Click Properties from the pop-up menu.
- In the Properties dialog, from the left pane, expand C/C++ Build. Click Settings.
- Go to the Tool Settings tab.
- 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:
- Navigate to Optimization Settings for the project (see Figure 2).
- Select Link-time optimizer (-flto) (see Figure 3).
Figure 3. Enabling Link-time Optimizer
- Click Apply, and then click OK.
- Before building the project, clean the project once. Right-click the project and click Clean Project.
- Now, build the project.