diff options
author | Volker Lendecke <vl@samba.org> | 2014-01-10 10:45:22 +0100 |
---|---|---|
committer | Karolin Seeger <kseeger@samba.org> | 2015-03-15 22:14:08 +0100 |
commit | 4419cad8952ce90febb2fdffd5bc1f74623475c6 (patch) | |
tree | 2f34bd697512c7eaeb445150d34156e70957aae0 | |
parent | d9e00c66d4573fc3ed85ceb709786356703d4615 (diff) | |
download | samba-4419cad8952ce90febb2fdffd5bc1f74623475c6.tar.gz |
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@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
Autobuild-User(master): Jeremy Allison <jra@samba.org>
Autobuild-Date(master): Thu May 15 12:49:14 CEST 2014 on sn-devel-104
(cherry picked from commit 593c8103af5a5ed6b3c915369fed5b90efb42c25)
-rw-r--r-- | lib/talloc/talloc.c | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/lib/talloc/talloc.c b/lib/talloc/talloc.c index 1cb4d7dead7..2a5406e11d0 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; |