Skip to content

libc/stdio: Optimize vsprintf stream output batching - #21339

Merged
xiaoxiang781216 merged 7 commits into
apache:masterfrom
anchao:26110201
Nov 3, 2026
Merged

xiaoxiang781216 merged 7 commits into
apache:masterfrom
anchao:26110201

Conversation

@anchao

@anchao anchao commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Optimize lib_libvsprintf.c by reducing per-character stream callbacks in common formatting paths.

This change builds on the existing literal and padding batching support, and further batches output for integer digits, symbol names, NaN/Inf strings, float exponent digits, and fixed-point zero runs. It also uses strlen() for unbounded %s formatting instead of strnlen(..., ~0).

Changes

  • Use strlen() for %s when no precision is specified
  • Batch integer digit output for %d, %u, %x, %X, and pointer formats
  • Batch %pS / %ps symbol name output
  • Batch fixed NaN / Inf string output
  • Batch exponent digits for %e / %E
  • Batch consecutive zero output in fixed-point float formatting

Motivation

vsprintf is used by both printf-style APIs and syslog paths, so avoiding repeated putc callbacks in hot formatting paths helps reduce formatting overhead. The changes keep the existing formatting behavior while using puts/repeat helpers where the output length is already known.

Testing

sim/usbdev
qemu-armv8a/rpproxy
qemu-armv8a/rpserver

Plain %s formats do not need the bounded scan used for precision
handling.  Use strlen() when no precision is specified so the common
string path can use the simpler libc helper.

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 Nov 2, 2026
Comment thread libs/libc/stream/lib_libvsprintf.c Resolved
The integer conversion path stores digits in reverse order and then
emits them one byte at a time.  Reverse the small conversion buffer in
place and write the digits with a single stream operation to reduce
putc callbacks for %d, %u, %x and pointer formats.

Signed-off-by: chao an <[email protected]>
When printing %pS/%ps symbols, emit the resolved symbol name with one
stream write instead of calling putc for every character.  This keeps
the debug symbol path consistent with the other batched string output
paths.

Signed-off-by: chao an <[email protected]>
NaN and Inf formatting only emits fixed three-byte strings.  Select the
pre-cased spelling up front and write it in one stream operation instead
of converting and outputting each character separately.

Signed-off-by: chao an <[email protected]>
Fixed-point formatting can generate long runs of zeroes for large
precisions or values far from the decimal point.  Detect consecutive
zero output and use the existing repeat helper so those runs are written
in chunks instead of one byte at a time.

Signed-off-by: chao an <[email protected]>
The exponent-style floating point path writes the mantissa fraction one
byte at a time.  Write the available dtoa digits with one stream call and
use the repeat helper for any precision padding zeroes to avoid per-digit
putc callbacks.

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

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +68 B (+0.1%, 66,353 B / 262,144 B, total: 25% used)

esp32-devkitc

  • ROM: .flash.rodata +8 B, .flash.text +64 B (+0.1%, 129,052 B / 4,194,272 B, total: 3% used)
  • drom0_0_seg: .flash.rodata +8 B (+0.1%, 13,924 B / 4,194,272 B, total: 0% used)
  • irom0_0_seg: .flash.text +64 B (+0.1%, 94,900 B / 3,342,304 B, total: 3% used)

mirtoo

  • kseg0_progmem: .text +180 B (+0.3%, 68,376 B / 131,072 B, total: 52% used)

qemu-armv8a

  • Code: .rodata +8 B, .text.vsprintf_internal.constprop.0 -72 B (+0.0%, 356,128 B)

qemu-intel64

  • Code: .text +96 B (+0.0%, 8,689,801 B)

s698pm-dkit

  • Code: .text +192 B (+0.0%, 388,496 B)

stm32-nucleo-f103rb

  • flash: .text +56 B (+0.2%, 34,792 B / 131,072 B, total: 27% used)

No memory changes detected for:


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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block approval of the formatted-output batching changes.

No open issues 🎉

What changed in this PR

Optimizes libc’s formatted output by batching writes to stream callbacks, reducing per-character overhead in printf-style APIs and syslog paths while aiming to preserve formatting behavior.

Changes:

  • Batches integer digits, symbol names, and NaN/Inf strings.
  • Batches floating-point fractional and exponent digits, plus consecutive zero runs.
  • Uses strlen() for %s when no precision is specified.
File Description
libs/​libc/​stream/​lib_libvsprintf.c Adds numeric-output batching and expands bulk stream writes across integer, string, and floating-point formatting.

Review effort: Balanced

xiaoxiang781216
xiaoxiang781216 previously approved these changes Nov 2, 2026

@casaroli casaroli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes lib_libvsprintf.o 80 bytes larger (arm-none-eabi-gcc 13.2: 2453 -> 2533 bytes of text). verdin-mx8mp:rpmsg had 8 bytes of flash free on master, so the arm-imx CI build now fails: "region `flash' overflowed by 72 bytes".

Fix: make room in boards/arm/mx8mp/verdin-mx8mp/configs/rpmsg/defconfig in this PR, for example disable CONFIG_TESTING_RAMTEST (the image then has 2440 bytes free).

Disable RAM test in the rpmsg defconfig to keep the image within the tight flash limit after the vsprintf refactor.

Signed-off-by: chao an <[email protected]>
@xiaoxiang781216
xiaoxiang781216 merged commit c27da7e into apache:master Nov 3, 2026
58 of 71 checks passed
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 Board: arm 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.

5 participants