Skip to content

libs/libc/stdio: disable stdio buffering by default on DEFAULT_SMALL - #22251

Open
Swatantra-66 wants to merge 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-stdio-buffering-default-small
Open

Swatantra-66 wants to merge 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-stdio-buffering-default-small

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

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

Change CONFIG_STDIO_DISABLE_BUFFERING default from n to DEFAULT_SMALL in libs/libc/stdio/Kconfig.

Tiny and memory-constrained embedded profiles (DEFAULT_SMALL=y) benefit significantly from disabling stdio stream buffering:

  1. Heap RAM Savings (64+ bytes per task): In sched/tls/task_initinfo.c, every task startup allocates CONFIG_STDIO_BUFFER_SIZE (default 64 bytes) for stdout buffering (info->ta_buf). When buffering is disabled, this allocation is completely eliminated, saving 64 bytes of heap RAM for every thread/task in the system.
  2. Flash Code Savings (-456 bytes): In libs/libc/stdio, disabling buffering allows the compiler to strip stream buffer allocation, deallocation, flushing, and ring-buffer maintenance routines (setbuf, setvbuf, setbuffer, lib_libfflush, lib_rdflush_unlocked, lib_wrflush_unlocked, and stream flush logic).

Standard configurations (DEFAULT_SMALL=n) retain the default of n (full buffering enabled).

Code Size Impact

Measured on stm32f103-minimum:nsh (arm-none-eabi-gcc 10.3.1 -Os):

Metric Before (STDIO_DISABLE_BUFFERING=n) After (STDIO_DISABLE_BUFFERING=y) Delta
Flash (text) 53,711 B 53,255 B -456 B
RAM (data) 336 B 332 B -4 B
RAM (bss) 3,136 B 3,136 B 0 B
Total Size 57,183 B 56,723 B -460 B
Heap per Task 64 B allocated 0 B allocated -64 B / task

Impact

  • Is new feature added?: No.
  • Is existing feature changed?: Yes, defaults CONFIG_STDIO_DISABLE_BUFFERING to DEFAULT_SMALL.
  • Impact on user: On DEFAULT_SMALL=y targets, saves 456 bytes of Flash and 64 bytes of heap RAM per task automatically without manual defconfig tuning. Over 40 defconfigs already manually set CONFIG_STDIO_DISABLE_BUFFERING=y. Standard builds (DEFAULT_SMALL=n) are unaffected.
  • Impact on build: None.
  • Impact on hardware: Flash and RAM savings across all memory-constrained targets.
  • Impact on documentation: None.

Testing

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

1. Kconfig Default Verification

  • Under DEFAULT_SMALL=n: CONFIG_STDIO_DISABLE_BUFFERING=n.
  • Under DEFAULT_SMALL=y: CONFIG_STDIO_DISABLE_BUFFERING=y.

2. Build & Size Comparison Logs

Before (CONFIG_STDIO_DISABLE_BUFFERING=n):

LD: nuttx
CP: nuttx.bin
   text	   data	    bss	    dec	    hex	filename
  53711	    336	   3136	  57183	   df5f	nuttx

After (CONFIG_STDIO_DISABLE_BUFFERING=y):

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

Tiny and memory-constrained embedded systems (DEFAULT_SMALL=y) benefit
greatly from disabling standard I/O buffering to minimize both Flash and
runtime SRAM footprints.

When CONFIG_STDIO_DISABLE_BUFFERING=y:
1. Sched/TLS eliminates the per-task stdout buffer allocation
   (CONFIG_STDIO_BUFFER_SIZE = 64+ bytes heap RAM saved per active task).
2. Libc strips stream buffer allocation, deallocation, flushing, and
   ring-buffer maintenance routines (lib_setbuf, lib_setvbuf, lib_libfflush,
   lib_rdflush_unlocked, lib_wrflush_unlocked, etc.).

Change CONFIG_STDIO_DISABLE_BUFFERING to default to DEFAULT_SMALL instead
of n. Standard configurations (DEFAULT_SMALL=n) retain full buffering.

Size reduction measured on stm32f103-minimum:nsh (arm-none-eabi-gcc 10.3.1 -Os):
- Flash (text): 53,711 -> 53,255 B (-456 B)
- RAM (data):   336 -> 332 B (-4 B)
- Total Image:  57,183 -> 56,723 B (-460 B)
- Heap RAM:     64 bytes saved per created task

Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swatantra Yadav <maverickswatantra@gmail.com>
@xiaoxiang781216 xiaoxiang781216 linked an issue Nov 24, 2026 that may be closed by this pull request
1 task done
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: XS The size of the change in this PR is very small labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

hifive1-revb

  • flash: .text -604 B (-0.7%, 85,760 B / 4,194,304 B, total: 2% used)
  • sram: .bss -256 B (-6.4%, 3,764 B / 16,384 B, total: 23% used)

No memory changes detected for:


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues 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.

[HELP] Improve NuttX bloated size

3 participants