← Back to the chapter · Next detail →
Technical detail · Software Optimization Techniques
Compiler optimization modes
18.7 Compiler Optimization Modes
Optimization flags express a preference to a particular toolchain; they do not guarantee a fixed list of transformations. In common GCC- and Clang-style workflows, a speed-focused -O3 build may inline more aggressively, unroll or vectorize loops, reorder instructions, and create separate fast paths for alignment or aliasing cases. A size-focused -Os build usually discourages growth, favors shorter encodings, and may keep branches or calls that a speed build removes. Inspect the compiler version, generated code, link map, and measurements rather than assigning universal behavior to the flag name.
Use a four-result comparison for the production workload:
| Build | Flash bytes | Hot-path time | Whole-cycle energy | Why it may win |
|---|---|---|---|---|
| Balanced optimization | Measure | Measure | Measure | Stable baseline with moderate code growth |
Speed-focused (-O3-style) | Often larger | Often shorter | Depends on active-time reduction and fetch/cache effects | A deadline-bound hot loop dominates energy |
Size-focused (-Os-style) | Often smaller | May be longer | Depends on flash fetch, cache fit, and added active time | Flash/update slot or instruction-fetch pressure dominates |
| Custom pass set | Toolchain-specific | Toolchain-specific | Must be measured | One known transformation helps while another causes regression |
A speed build can reduce energy by returning to sleep sooner, yet lose that gain if code growth causes more flash wait states or instruction-cache misses. A size build can reduce fetch energy, yet keep the core awake longer. The winning flag is the one that lowers measured charge per useful task while preserving timing, stack, numerical, and update-slot requirements.