xref: /aosp_15_r20/external/pigweed/pw_string/code_size.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1.. _module-pw_string-size-reports:
2
3==================
4Code Size Analysis
5==================
6.. pigweed-module-subpage::
7   :name: pw_string
8
9Save code space by replacing ``snprintf``
10=========================================
11The C standard library function ``snprintf`` is commonly used for string
12formatting. However, it isn't optimized for embedded systems, and using it will
13bring in a lot of other standard library code that will inflate your binary
14size.
15
16Size comparison: snprintf versus pw::StringBuilder
17--------------------------------------------------
18The fixed code size cost of :cpp:type:`pw::StringBuilder` is smaller than
19that of ``std::snprintf``. Using only :cpp:type:`pw::StringBuilder`'s ``<<`` and
20``append`` methods instead of ``snprintf`` leads to significant code size
21reductions.
22
23However, there are cases when the incremental code size cost of
24:cpp:type:`pw::StringBuilder` is similar to that of ``snprintf``. For example,
25each argument to :cpp:type:`pw::StringBuilder`'s ``<<`` method expands to a
26function call, but one or two :cpp:type:`pw::StringBuilder` appends may still
27have a smaller code size impact than a single ``snprintf`` call. Using
28:cpp:type:`pw::StringBuilder` error handling will also impact code size in a
29way that is comparable to ``snprintf``.
30
31.. include:: string_builder_size_report
32
33Size comparison: snprintf versus pw::string::Format
34---------------------------------------------------
35The ``pw::string::Format`` functions have a small, fixed code size
36cost. However, relative to equivalent ``std::snprintf`` calls, there is no
37incremental code size cost to using ``pw::string::Format``.
38
39.. include:: format_size_report
40