mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Re: [Patch 1/1] vfprintf: optimize vfprintf performance
@ 2023-06-12  7:56 =?gb18030?B?ODQ3NTY3MTYx?=
  0 siblings, 0 replies; only message in thread
From: =?gb18030?B?ODQ3NTY3MTYx?= @ 2023-06-12  7:56 UTC (permalink / raw)
  To: =?gb18030?B?bXVzbA==?=

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="gb18030", Size: 981 bytes --]

Hi all£¬ 

How about the patch sent in May£¿I can modify it if there are any problem.

the benchmark show there are about  30% benefits for commonly used format string.

before opt: 160ns
after opt: 110ns

static void BM_stdio_printf_s(benchmark::State& state) {                                                       
  while (state.KeepRunning()) {                                                                               
        char buf[BUFSIZ];                                                                                         
        snprintf(buf, sizeof(buf), "this is a more typical error message with detail: %s",                         
                "No such file or directory");
    }                                                                                                           
}                                                                                                             
MUSL_BENCHMARK(BM_stdio_printf_s);


Chuang Yin

[-- Attachment #2: optimize_printf.diff --]
[-- Type: application/octet-stream, Size: 1970 bytes --]

diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c
index 9b961e7f..4a01ca1e 100644
--- a/src/stdio/vfprintf.c
+++ b/src/stdio/vfprintf.c
@@ -427,7 +427,7 @@ static int getint(char **s) {
 	return i;
 }
 
-static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
+static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type, char nl_arg_filled)
 {
 	char *a, *z, *s=(char *)fmt;
 	unsigned l10n=0, fl;
@@ -437,7 +437,6 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg,
 	unsigned st, ps;
 	int cnt=0, l=0;
 	size_t i;
-	char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
 	const char *prefix;
 	int t, pl;
 	wchar_t wc[2], *ws;
@@ -462,6 +461,11 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg,
 		if (l) continue;
 
 		if (isdigit(s[1]) && s[2]=='$') {
+			if (!nl_arg_filled) {
+				if (printf_core(0, fmt, ap, nl_arg, nl_type, 1) < 0) {
+					return -1;
+				}
+			}
 			l10n=1;
 			argpos = s[1]-'0';
 			s+=3;
@@ -528,6 +532,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg,
 
 		if (!f) continue;
 
+		char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
 		z = buf + sizeof(buf);
 		prefix = "-+   0X0x";
 		pl = 0;
@@ -665,10 +670,6 @@ int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
 
 	/* the copy allows passing va_list* even if va_list is an array */
 	va_copy(ap2, ap);
-	if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
-		va_end(ap2);
-		return -1;
-	}
 
 	FLOCK(f);
 	olderr = f->flags & F_ERR;
@@ -680,7 +681,7 @@ int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
 		f->wpos = f->wbase = f->wend = 0;
 	}
 	if (!f->wend && __towrite(f)) ret = -1;
-	else ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
+	else ret = printf_core(f, fmt, &ap2, nl_arg, nl_type, 0);
 	if (saved_buf) {
 		f->write(f, 0, 0);
 		if (!f->wpos) ret = -1;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-06-12  7:56 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-12  7:56 [musl] Re: [Patch 1/1] vfprintf: optimize vfprintf performance =?gb18030?B?ODQ3NTY3MTYx?=

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).