changeset 21338:bc57c62167fc

lib: Optimize printf_format_fix_noalloc() Using strchr() is faster than looping through the characters manually. Since this function is being called a lot, it's worth optimizing.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Tue, 29 Nov 2016 23:21:17 +0200
parents 1dd0c6ab307e
children 8d49b6ed7bab
files src/lib/printf-format-fix.c
diffstat 1 files changed, 16 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/printf-format-fix.c	Sat Dec 17 20:03:46 2016 +0100
+++ b/src/lib/printf-format-fix.c	Tue Nov 29 23:21:17 2016 +0200
@@ -35,28 +35,27 @@
 static const char *
 printf_format_fix_noalloc(const char *format, size_t *len_r)
 {
-	const char *p;
-	const char *ret = format;
+	const char *ret, *p, *p2;
 
-	for (p = format; *p != '\0'; ) {
-		if (*p++ == '%') {
-			switch (*p) {
-			case 'n':
-				i_panic("%%n modifier used");
-			case 'm':
-				if (ret != format)
-					i_panic("%%m used twice");
-				ret = fix_format_real(format, p-1, len_r);
-				break;
-			case '\0':
-				i_panic("%% modifier missing in '%s'", format);
-			}
-			p++;
+	p = ret = format;
+	while ((p2 = strchr(p, '%')) != NULL) {
+		p = p2+1;
+		switch (*p) {
+		case 'n':
+			i_panic("%%n modifier used");
+		case 'm':
+			if (ret != format)
+				i_panic("%%m used twice");
+			ret = fix_format_real(format, p-1, len_r);
+			break;
+		case '\0':
+			i_panic("%% modifier missing in '%s'", format);
 		}
+		p++;
 	}
 
 	if (ret == format)
-		*len_r = p - format;
+		*len_r = p - format + strlen(p);
 	return ret;
 }