Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 14 Oct 2019 08:07:27 -0400
From: Rich Felker <>
Subject: Re: [PATCH] Use open_memstream(3) for more efficient asprintf

On Mon, Oct 14, 2019 at 02:48:19AM -0400, Alex Brachet-Mialot wrote:
> Hi I wasn't able to search the lists from the online archive, so I'm not
> sure if it has been talked about yet, but the current vasprintf
> implementation could be made better if it didn't call vsnprintf twice. Let
> me know what you think!

> diff --git a/src/stdio/vasprintf.c b/src/stdio/vasprintf.c
> index 08251bc2..d55fe32f 100644
> --- a/src/stdio/vasprintf.c
> +++ b/src/stdio/vasprintf.c
> @@ -5,11 +5,16 @@
>  int vasprintf(char **s, const char *fmt, va_list ap)
>  {
> -	va_list ap2;
> -	va_copy(ap2, ap);
> -	int l = vsnprintf(0, 0, fmt, ap2);
> -	va_end(ap2);
> +	size_t l;
> +	*s = 0;
> +	FILE *f = open_memstream(s, &l);
> +	if (!f)
> +		return -1;
> -	if (l<0 || !(*s=malloc(l+1U))) return -1;
> -	return vsnprintf(*s, l+1U, fmt, ap);
> +	if ((l = vfprintf(f, fmt, ap)) == -1) {
> +		free(*s);
> +		*s = 0;
> +	}
> +	fclose(f);
> +	return l;
>  }

Hi. Unfortunately this isn't more efficient, or at least it's a
tradeoff between different types of efficiency opposite to the choice
made when implementing it.

In general for this type of operation, you have a choice of two

1. pre-computing the size needed or some upper bound on it, allocating
   that, and then writing the output, or

2. incrementally allocating/resizing storage as output is produced,
   with no need to precompute.

The tradeoffs in favor of approach 2 (your version) are:

- avoiding two passes over the data, which may be moderately expensive
  in the case of long floating point formats or wide character

- avoiding concerns about whether the second pass generates same
  output (this comes up with %m and LC_MESSAGES, and possibly %f with
  LC_NUMERIC in the future, but all such cases involve UB due to
  illegal concurrent locale change so they don't have to be handled)

and in favor of approach 1 are:

- avoiding quadratic-time worst-case (from the memcpy in realloc with
  linear buffer size growth at each step) or internal fragmentation
  (from geometric buffer growth at each step). Our current memstream
  implementation uses linear growth I think so it would be the former
  (quadratic time)

- avoiding linking realloc/free in static linked programs that don't
  free results.

- avoiding fragmentation produced by realloc

- minimizing memory synchronization with other cores. Everything
  vsnprintf does is local to the calling thread, but
  malloc/realloc/free have to synchronize, and there are fairly many
  if you use open_memstream. At least one malloc/free pair for the
  FILE object, and one realloc per size increase of output.

This is actually documented to some extent in the commit that produced
the current version of vasprintf.c:



Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.