[dm-devel] [PATCH 14/15] libmultipath: Micro-optimize snprint_size()

Bart Van Assche bart.vanassche at sandisk.com
Tue Oct 4 17:41:59 UTC 2016


Eliminate the stack array fmt[]. An interesting side effect of
this patch is that it makes it possible for the compiler to
verify whether the snprintf() arguments have a type that is
appropriate for the format string.

Signed-off-by: Bart Van Assche <bart.vanassche at sandisk.com>
---
 libmultipath/print.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/libmultipath/print.c b/libmultipath/print.c
index 94d6384..9aa41ad 100644
--- a/libmultipath/print.c
+++ b/libmultipath/print.c
@@ -73,7 +73,6 @@ static int
 snprint_size (char * buff, size_t len, unsigned long long size)
 {
 	float s = (float)(size >> 1); /* start with KB */
-	char fmt[6] = {};
 	char units[] = {'K','M','G','T','P'};
 	char *u = units;
 
@@ -81,12 +80,8 @@ snprint_size (char * buff, size_t len, unsigned long long size)
 		s = s / 1024;
 		u++;
 	}
-	if (s < 10)
-		snprintf(fmt, 6, "%%.1f%c", *u);
-	else
-		snprintf(fmt, 6, "%%.0f%c", *u);
 
-	return snprintf(buff, len, fmt, s);
+	return snprintf(buff, len, "%.*f%c", s < 10, s, *u);
 }
 
 /*
-- 
2.10.0




More information about the dm-devel mailing list