Skip to content

libc/stdio: Batch vsprintf output - #21072

Merged
xiaoxiang781216 merged 2 commits into
apache:masterfrom
anchao:26102810
Oct 30, 2026
Merged

xiaoxiang781216 merged 2 commits into
apache:masterfrom
anchao:26102810

Conversation

@anchao

@anchao anchao commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR optimizes the hot vsprintf() formatting path used by printf/syslog-style output.

It contains two changes:

  • Batch consecutive literal format text into a single stream write when CONFIG_ARCH_ROMGETC is not required.
  • Batch repeated padding spaces/zeroes using fixed-size chunks instead of emitting one character at a time.

These reduce per-character output callback overhead in common formatting paths, especially for log/printf output with long literal text or wide field padding.

Motivation

vsprintf_internal() currently emits many parts of the formatted output one character at a time via lib_stream_putc(). For printf/syslog hot paths, this means repeated function-pointer-style output calls for:

  • plain literal text in the format string
  • left/right field padding
  • zero padding for numeric formats

The formatted result is unchanged, but batching these output sequences can reduce call overhead.

Implementation

Literal text batching

For non-CONFIG_ARCH_ROMGETC builds, consecutive literal characters in the format string are now accumulated and emitted with one stream_puts() call.

The CONFIG_ARCH_ROMGETC path keeps the original character-by-character behavior because format characters may need to be fetched through up_romgetc().

Padding batching

Repeated spaces and zeroes are emitted through a small helper that writes from static 16-byte buffers:

  • spaces: " "
  • zeroes: "0000000000000000"

Padding loops now output chunks rather than calling stream_putc() for each byte.

Testing

sim/nsh

Write consecutive literal format text with one stream call when ROMGETC is
not required, reducing per-character output overhead on printf hot paths.

Signed-off-by: chao an <[email protected]>
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Oct 28, 2026
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .data +32 B, .text +216 B (+0.4%, 65,380 B / 262,144 B, total: 25% used)
  • sram: .data +32 B (+1.2%, 2,722 B / 8,192 B, total: 33% used)

esp32-devkitc

  • ROM: .flash.rodata +32 B, .flash.text +16 B (+0.0%, 127,820 B / 4,194,272 B, total: 3% used)
  • drom0_0_seg: .flash.rodata +32 B (+0.2%, 13,828 B / 4,194,272 B, total: 0% used)
  • irom0_0_seg: .flash.text +16 B (+0.0%, 93,764 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +104 B (+0.1%, 84,636 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +468 B (+0.7%, 67,268 B / 131,072 B, total: 51% used)

qemu-intel64

  • Code: .text +256 B (+0.0%, 8,686,983 B)
  • Data: .rodata +32 B (+0.0%, 125,049 B)

rx65n-rsk2mb

  • ROM: .rodata +32 B, .text +96 B (+0.1%, 89,912 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +448 B (+0.1%, 382,624 B)

stm32-nucleo-f103rb

  • flash: .text +36 B (+0.1%, 34,156 B / 131,072 B, total: 26% used)

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

Comment thread libs/libc/stream/lib_libvsprintf.c Resolved
Comment thread libs/libc/stream/lib_libvsprintf.c Resolved
Emit repeated spaces and zeroes in small fixed-size chunks to avoid calling
the output callback once per padding character.

Signed-off-by: chao an <[email protected]>
@acassis

acassis commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

@anchao please analyze the CI issues

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: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants