Skip to content

libs/libc/stdio: default to LIBC_PRINTF_MINIMAL on DEFAULT_SMALL - #22317

Merged
acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-printf-minimal-default-small
Nov 25, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-printf-minimal-default-small

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Addresses Issue #20414 ([HELP] Improve NuttX bloated size)

NuttX libc provides three printf profiles (Minimal, Default, and Full), documented in Documentation/os/libs/libc/printf.rst. Currently, the profile choice unconditionally defaults to LIBC_PRINTF_DEFAULT for all targets.

This patch changes the profile choice in libs/libc/stdio/Kconfig to default to LIBC_PRINTF_MINIMAL when CONFIG_DEFAULT_SMALL=y:

choice
	prompt "printf profile"
	default LIBC_PRINTF_MINIMAL if DEFAULT_SMALL
	default LIBC_PRINTF_DEFAULT

On memory-constrained targets, Minimal omits unnecessary conversions and flags that are rarely required in resource-constrained firmware:

  • Disables %lld / 64-bit integer division on 32-bit CPUs (unless SYSLOG_TIMESTAMP or FS_PROCFS is enabled).
  • Disables alternate form (# flag for %#x, %#o).
  • Disables integer precision (e.g. %.3d).
  • Automatically defaults CONFIG_STREAM_OUT_BUFFER_SIZE to 0 in libs/libc/stream/Kconfig, saving 64 bytes of stack frame on dprintf() and vdprintf() calls and omitting lib_bufferedoutstream.c.

Standard configurations (!DEFAULT_SMALL) continue to default to LIBC_PRINTF_DEFAULT with zero impact. Targets needing specific printf features can still enable them individually or select Default/Full in menuconfig.

Size Impact

Measured across multiple architectures with -Os:

Architecture Target Configuration Before (Default) After (Minimal) Flash Savings
ARM Cortex-M3 stm32f103-minimum:nsh 53,255 B 52,815 B -440 B
ARM Cortex-M0 nucleo-f072rb:nsh 46,792 B 46,460 B -332 B
32-bit RISC-V rv-virt:nsh 231,490 B 228,166 B -3,324 B (-3.3 KB)
ARM64 qemu-armv8a:nsh 329,564 B 325,844 B -3,720 B (-3.7 KB)

Impact

  • Is new feature added?: No.
  • Is existing feature changed?: Yes, defaults printf profile to LIBC_PRINTF_MINIMAL on DEFAULT_SMALL=y.
  • Impact on user: Memory-constrained targets save flash and stack. Format conversions remain safe (any omitted conversion safely prints ? and consumes arguments correctly).
  • Impact on build: None. No defconfigs set CONFIG_LIBC_PRINTF_*, so defconfig normalization is 100% clean.
  • Impact on hardware: Flash savings on all DEFAULT_SMALL boards.
  • Impact on documentation: Updated Documentation/os/libs/libc/printf.rst.

Testing

Build host: Linux container (nuttx-builder) with arm-none-eabi-gcc 10.3.1.
Target board: stm32f103-minimum:nsh.

1. Style & Verification

  • git diff --check: PASSED (0 whitespace errors).
  • Documentation updated and verified.

2. Build & Size Comparison Logs

Before (CONFIG_LIBC_PRINTF_DEFAULT=y):

CP: nuttx.bin
   text	   data	    bss	    dec	    hex	filename
  53255	    332	   3136	  56723	   dd93	nuttx

After (CONFIG_LIBC_PRINTF_MINIMAL=y via DEFAULT_SMALL):

CP: nuttx.bin
   text	   data	    bss	    dec	    hex	filename
  52815	    332	   3136	  56283	   dbdb	nuttx

Total flash reduction on stm32f103-minimum:nsh: -440 bytes.

NuttX provides three printf profiles: Minimal, Default, and Full.
Currently, the profile choice unconditionally defaults to
LIBC_PRINTF_DEFAULT across all targets, preventing memory-constrained
systems (CONFIG_DEFAULT_SMALL=y) from benefiting from the Minimal
profile by default.

This change sets the default printf profile to LIBC_PRINTF_MINIMAL
when DEFAULT_SMALL is enabled. This reduces text size by omitting
unneeded conversions and features:
- On stm32f103-minimum:nsh (Cortex-M3): -440 B Flash
  (53,255 B -> 52,815 B)
- On nucleo-f072rb:nsh (Cortex-M0): -332 B Flash
  (46,792 B -> 46,460 B)
- On rv-virt:nsh (32-bit RISC-V): -3,324 B Flash
  (231,490 B -> 228,166 B)
- On qemu-armv8a:nsh (arm64): -3,720 B Flash
  (329,564 B -> 325,844 B)

Additionally, with LIBC_PRINTF_MINIMAL, CONFIG_STREAM_OUT_BUFFER_SIZE
defaults to 0, saving 64 bytes of stack frame on dprintf() and
vdprintf() calls and omitting lib_bufferedoutstream.c from compilation.

Standard targets (!DEFAULT_SMALL) continue to default to
LIBC_PRINTF_DEFAULT.

Related to apache#20414

Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swatantra Yadav <[email protected]>
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: XS The size of the change in this PR is very small labels Nov 25, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text -778 B, .trampolines -12 B (-1.2%, 64,077 B / 262,144 B, total: 24% used)

hifive1-revb

  • flash: .text -200 B (-0.2%, 86,704 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text -4,160 B (-6.4%, 62,136 B / 131,072 B, total: 47% used)

stm32-nucleo-f103rb

  • flash: .text -1,164 B (-3.3%, 34,000 B / 131,072 B, total: 26% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@acassis
acassis merged commit 02e671a into apache:master Nov 25, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants