[PATCH] talloc: Tune talloc_vasprintf

Volker Lendecke Volker.Lendecke at SerNet.DE
Thu May 15 01:56:04 MDT 2014


Hi!

A patch from my attic: In some profiles vsnprintf showed up
high from talloc_asprintf. This cuts vsnprintf use in
talloc_asprintf by half in most cases.

Review would be appreciated!

Thanks,

Volker

-- 
SerNet GmbH, Bahnhofsallee 1b, 37081 Göttingen
phone: +49-551-370000-0, fax: +49-551-370000-9
AG Göttingen, HRB 2816, GF: Dr. Johannes Loxen
http://www.sernet.de, mailto:kontakt at sernet.de
-------------- next part --------------
From a3546632aa52d5299991f4f5960187723298c1ab Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Fri, 10 Jan 2014 10:45:22 +0100
Subject: [PATCH] talloc: Tune talloc_vasprintf

vsnprintf is significantly more expensive than memcpy. For the
common case where the string we print is less than a kilobyte, avoid
the second vsnprintf.

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/talloc/talloc.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/lib/talloc/talloc.c b/lib/talloc/talloc.c
index 1cb4d7d..2a5406e 100644
--- a/lib/talloc/talloc.c
+++ b/lib/talloc/talloc.c
@@ -2356,11 +2356,11 @@ _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
 	int len;
 	char *ret;
 	va_list ap2;
-	char c;
+	char buf[1024];
 
 	/* this call looks strange, but it makes it work on older solaris boxes */
 	va_copy(ap2, ap);
-	len = vsnprintf(&c, 1, fmt, ap2);
+	len = vsnprintf(buf, sizeof(buf), fmt, ap2);
 	va_end(ap2);
 	if (unlikely(len < 0)) {
 		return NULL;
@@ -2369,9 +2369,13 @@ _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
 	ret = (char *)__talloc(t, len+1);
 	if (unlikely(!ret)) return NULL;
 
-	va_copy(ap2, ap);
-	vsnprintf(ret, len+1, fmt, ap2);
-	va_end(ap2);
+	if (len < sizeof(buf)) {
+		memcpy(ret, buf, len+1);
+	} else {
+		va_copy(ap2, ap);
+		vsnprintf(ret, len+1, fmt, ap2);
+		va_end(ap2);
+	}
 
 	_talloc_set_name_const(ret, ret);
 	return ret;
-- 
1.7.9.5



More information about the samba-technical mailing list