M*LIB Size Optimization Report

Author: kalicyhCreated May 6, 2026Updated Jul 11, 2026
Labelsneeds-triagediscussionlib

Describe the enhancement you're suggesting.

Image

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: 880593 bytes
  • After archive container change: 880537 bytes
  • After archive + CLI container changes: 880009 bytes
  • Total reduction: 584 bytes

Build command used for comparison:

bash
MOMENTUM_UI_LANG=zh_CN MOMENTUM_DEVICE=clipper ./fbt updater_package COMPACT=1 DEBUG=0

Motivation

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.h
  • applications/main/archive/helpers/archive_files.c

What changed:

  • Removed the mlib ARRAY_DEF-based files_array implementation.
  • 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, and ArchiveFile_t_cmp out of the header and into the .c file.
  • Kept the public helper function names used by archive code:
    • files_array_init
    • files_array_reset
    • files_array_clear
    • files_array_size
    • files_array_get
    • files_array_sort
    • files_array_remove_v
    • files_array_pop_at
    • files_array_push_at
    • files_array_swap_at
    • files_array_push_back

Why:

  • The archive file list is a specific container with fixed semantics.
  • A custom implementation avoids carrying generic mlib array 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: 56 bytes

Change 2: CLI Command Registry

Files:

  • lib/toolbox/cli/cli_registry_i.h
  • lib/toolbox/cli/cli_registry.c
  • lib/toolbox/cli/shell/cli_shell.c
  • lib/toolbox/cli/shell/cli_shell_completions.c

What changed:

  • Removed the mlib DICT_DEF2-based CliCommandDict.
  • Replaced it with a compact sequential registry based on CliRegistryEntry[].
  • Added a minimal internal iteration/access API:
    • cli_registry_get_count
    • cli_registry_get_name_at
    • cli_registry_get_command_at
  • Updated CLI command listing, fuzzy suggestion, and completion code to iterate through the new registry interface instead of mlib dictionary 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: 528 bytes

Total Outcome

Final result:

  • 880593 -> 880009
  • Total reduction: 584 bytes

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 mlib for these two core container paths
  • Logic remains straightforward and maintainable

Cons:

  • Slightly more handwritten container code
  • Loses some genericity compared to the original mlib implementation

Anything else?

No response

Source: Next-Flip/Momentum-Firmware