M*LIB Size Optimization Report
Describe the enhancement you're suggesting.
https://github.com/kalicyh/Momentum-Firmware-CN/commit/2d214b4e5ccb33f8c6da038aa250fb572f1ed337
M*LIB Size Optimization Report
Summary
This change reduces firmware.dfu size by replacing two mlib header-only generic container usages in the main firmware with smaller purpose-built implementations.
Commit:
2d214b4e5-Reduce firmware size in archive and cli containers
Result:
- Baseline
firmware.dfu:880593bytes - After archive container change:
880537bytes - After archive + CLI container changes:
880009bytes - Total reduction:
584bytes
Build command used for comparison:
MOMENTUM_UI_LANG=zh_CN MOMENTUM_DEVICE=clipper ./fbt updater_package COMPACT=1 DEBUG=0Motivation
mlib is a header-only generic container library. It is convenient, but in some places it also causes extra code generation because container logic is instantiated per use site and per type. In this firmware, replacing a few high-traffic generic containers with simpler domain-specific implementations produces a measurable size reduction without changing behavior.
This is not a proposal to update mlib itself. It is a targeted reduction of mlib usage in a few core firmware paths where the generic implementation is not necessary.
Change 1: Archive File List
Files:
applications/main/archive/helpers/archive_files.happlications/main/archive/helpers/archive_files.c
What changed:
- Removed the
mlib ARRAY_DEF-basedfiles_arrayimplementation. - Replaced it with a small dedicated dynamic array implementation tailored to
ArchiveFile_t. - Moved
ArchiveFile_t_init,ArchiveFile_t_init_set,ArchiveFile_t_set,ArchiveFile_t_clear, andArchiveFile_t_cmpout of the header and into the.cfile. - Kept the public helper function names used by archive code:
files_array_initfiles_array_resetfiles_array_clearfiles_array_sizefiles_array_getfiles_array_sortfiles_array_remove_vfiles_array_pop_atfiles_array_push_atfiles_array_swap_atfiles_array_push_back
Why:
- The archive file list is a specific container with fixed semantics.
- A custom implementation avoids carrying generic
mlibarray machinery for this path. - Moving
ArchiveFile_t_*helpers into a single translation unit also reduces header-level code duplication pressure.
Measured impact:
880593->880537- Reduction:
56bytes
Change 2: CLI Command Registry
Files:
lib/toolbox/cli/cli_registry_i.hlib/toolbox/cli/cli_registry.clib/toolbox/cli/shell/cli_shell.clib/toolbox/cli/shell/cli_shell_completions.c
What changed:
- Removed the
mlib DICT_DEF2-basedCliCommandDict. - Replaced it with a compact sequential registry based on
CliRegistryEntry[]. - Added a minimal internal iteration/access API:
cli_registry_get_countcli_registry_get_name_atcli_registry_get_command_at
- Updated CLI command listing, fuzzy suggestion, and completion code to iterate through the new registry interface instead of
mlibdictionary iterators.
Why:
- The number of CLI commands is limited and manageable with a sequential container.
- The generic dictionary implementation is more flexible than needed for this use case.
- A simpler data structure reduces linked code size while preserving behavior.
Measured impact:
880537->880009- Reduction from this step:
528bytes
Total Outcome
Final result:
880593->880009- Total reduction:
584bytes
This is a small but real improvement in a size-sensitive firmware image. The change is concentrated in two well-bounded areas and does not require modifying mlib itself.
Tradeoff
Pros:
- Smaller final firmware image
- No dependency on
mlibfor these two core container paths - Logic remains straightforward and maintainable
Cons:
- Slightly more handwritten container code
- Loses some genericity compared to the original
mlibimplementation
Anything else?
No response
Source: Next-Flip/Momentum-Firmware