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 1800aa8228fbad4782aea63324aa7461774f11d6
Author: chao an <[email protected]>
AuthorDate: Mon Nov 2 11:31:16 2026 +0800

    libc/stdio: Batch integer digit writes
    
    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]>
---
 libs/libc/stream/lib_libvsprintf.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/libs/libc/stream/lib_libvsprintf.c 
b/libs/libc/stream/lib_libvsprintf.c
index 4a8ee5c5f9a..b471197210e 100644
--- a/libs/libc/stream/lib_libvsprintf.c
+++ b/libs/libc/stream/lib_libvsprintf.c
@@ -170,6 +170,28 @@ static int vsprintf_repeat(FAR struct lib_outstream_s 
*stream, int ch,
   return total_len;
 }
 
+static int vsprintf_putnum(FAR struct lib_outstream_s *stream,
+                           FAR char *buf, int len)
+{
+  int i;
+  int j;
+
+  for (i = 0, j = len - 1; i < j; i++, j--)
+    {
+      char tmp = buf[i];
+
+      buf[i] = buf[j];
+      buf[j] = tmp;
+    }
+
+  if (len > 0)
+    {
+      lib_stream_puts(stream, buf, len);
+    }
+
+  return 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)
@@ -918,18 +940,14 @@ flt_oper:
                 }
 
               stream_putc(ndigs, stream);
-              c = __ultoa_invert(exp, buf, 10) - buf;
+              len = __ultoa_invert(exp, buf, 10) - buf;
 
               if (exp >= 0 && exp <= 9)
                 {
                   stream_putc('0', stream);
                 }
 
-              while (c > 0)
-                {
-                  stream_putc(buf[c - 1], stream);
-                  c--;
-                }
+              total_len += vsprintf_putnum(stream, buf, len);
             }
 
           goto tail;
@@ -1428,10 +1446,7 @@ str_lpad:
           prec = c;
         }
 
-      while (c)
-        {
-          stream_putc(buf[--c], stream);
-        }
+      total_len += vsprintf_putnum(stream, buf, c);
 
 tail:
 

Reply via email to