Repository navigation
libc/stdio: Optimize vsprintf stream output batching - #21339
Conversation
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]>
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]>
|
There was a problem hiding this comment.
🟢 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%swhen 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
casaroli
left a comment
There was a problem hiding this comment.
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]>
Summary
Optimize
lib_libvsprintf.cby 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%sformatting instead ofstrnlen(..., ~0).Changes
strlen()for%swhen no precision is specified%d,%u,%x,%X, and pointer formats%pS/%pssymbol name outputNaN/Infstring output%e/%EMotivation
vsprintfis used by both printf-style APIs and syslog paths, so avoiding repeatedputccallbacks in hot formatting paths helps reduce formatting overhead. The changes keep the existing formatting behavior while usingputs/repeat helpers where the output length is already known.Testing
sim/usbdev
qemu-armv8a/rpproxy
qemu-armv8a/rpserver