This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 7505b8747228fc76188e2c1af5d43c443ad41ab1 Author: chao an <[email protected]> AuthorDate: Wed Oct 28 16:58:25 2026 +0800 libc/stdio: Batch vsprintf padding output 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]> --- libs/libc/stream/lib_libvsprintf.c | 77 +++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 30 deletions(-) diff --git a/libs/libc/stream/lib_libvsprintf.c b/libs/libc/stream/lib_libvsprintf.c index 7a166eebc49..cef1731b8c8 100644 --- a/libs/libc/stream/lib_libvsprintf.c +++ b/libs/libc/stream/lib_libvsprintf.c @@ -62,6 +62,10 @@ #define stream_putc(c,stream) (total_len++, lib_stream_putc(stream, c)) #define stream_puts(buf, len, stream) \ (total_len += len, lib_stream_puts(stream, buf, len)) +#define stream_repeat(c, len, stream) \ + (total_len += vsprintf_repeat(stream, c, len)) + +#define REPEAT_BUFLEN 16 /* Order is relevant here and matches order in format string */ @@ -133,6 +137,8 @@ struct arg_s ****************************************************************************/ static const char g_nullstring[] = "(null)"; +static const char g_spaces[REPEAT_BUFLEN] = " "; +static const char g_zeroes[REPEAT_BUFLEN] = "0000000000000000"; /**************************************************************************** * Private Function Prototypes @@ -147,6 +153,23 @@ static int vsprintf_internal(FAR struct lib_outstream_s *stream, * Private Functions ****************************************************************************/ +static int vsprintf_repeat(FAR struct lib_outstream_s *stream, int ch, + int len) +{ + FAR const char *buf = ch == '0' ? g_zeroes : g_spaces; + int total_len = len; + + while (len > 0) + { + int n = MIN(len, REPEAT_BUFLEN); + + lib_stream_puts(stream, buf, n); + len -= n; + } + + return total_len; +} + static int vsprintf_internal(FAR struct lib_outstream_s *stream, FAR struct arg_s *arglist, int numargs, FAR const IPTR char *fmt, va_list ap) @@ -683,11 +706,8 @@ flt_oper: width -= ndigs; if ((flags & FL_LPAD) == 0) { - do - { - stream_putc(' ', stream); - } - while (--width); + stream_repeat(' ', width, stream); + width = 0; } } else @@ -788,11 +808,8 @@ flt_oper: if ((flags & (FL_LPAD | FL_ZFILL)) == 0) { - while (width) - { - stream_putc(' ', stream); - width--; - } + stream_repeat(' ', width, stream); + width = 0; } if (sign != 0) @@ -802,11 +819,8 @@ flt_oper: if ((flags & FL_LPAD) == 0) { - while (width) - { - stream_putc('0', stream); - width--; - } + stream_repeat('0', width, stream); + width = 0; } if ((flags & FL_FLTFIX) != 0) @@ -1025,10 +1039,12 @@ flt_oper: if ((flags & FL_LPAD) == 0) { - while (size < width) + if (width > 0 && size < width) { - stream_putc(' ', stream); - width--; + int pad = width - size; + + stream_repeat(' ', pad, stream); + width -= pad; } } @@ -1052,10 +1068,12 @@ flt_oper: str_lpad: if ((flags & FL_LPAD) == 0) { - while (size < width) + if (width > 0 && size < width) { - stream_putc(' ', stream); - width--; + int pad = width - size; + + stream_repeat(' ', pad, stream); + width -= pad; } } @@ -1363,10 +1381,10 @@ str_lpad: } } - while (len < width) + if (len < width) { - stream_putc(' ', stream); - len++; + stream_repeat(' ', width - len, stream); + len = width; } } @@ -1397,10 +1415,10 @@ str_lpad: stream_putc(z, stream); } - while (prec > c) + if (prec > c) { - stream_putc('0', stream); - prec--; + stream_repeat('0', prec - c, stream); + prec = c; } while (c) @@ -1412,10 +1430,9 @@ tail: /* Tail is possible. */ - while (width) + if (width > 0) { - stream_putc(' ', stream); - width--; + stream_repeat(' ', width, stream); } }
