From: Andre Noll Date: Mon, 25 Mar 2013 19:05:05 +0000 (+0000) Subject: string: Speed up xvasprintf(). X-Git-Tag: v0.4.13~33^2 X-Git-Url: http://git.tue.mpg.de/?a=commitdiff_plain;h=bf1831886b93258ffcec63c47d6737fffff53aae;p=paraslash.git string: Speed up xvasprintf(). This avoids to call vsnprintf() twice in the common case where the result is small. The running time of a simple test case that called this function in a loop improved from 13s to 8s due to this change. --- diff --git a/string.c b/string.c index ba744bff..4d8b8b74 100644 --- a/string.c +++ b/string.c @@ -136,15 +136,18 @@ __must_check __malloc char *para_strdup(const char *s) __printf_2_0 unsigned xvasprintf(char **result, const char *fmt, va_list ap) { int ret; - size_t size; + size_t size = 150; va_list aq; + *result = para_malloc(size + 1); va_copy(aq, ap); - ret = vsnprintf(NULL, 0, fmt, aq); + ret = vsnprintf(*result, size, fmt, aq); va_end(aq); assert(ret >= 0); + if (ret < size) /* OK */ + return ret; size = ret + 1; - *result = para_malloc(size); + *result = para_realloc(*result, size); va_copy(aq, ap); ret = vsnprintf(*result, size, fmt, aq); va_end(aq);