mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [PATCH] Use open_memstream(3) for more efficient asprintf
Date: Mon, 14 Oct 2019 08:07:27 -0400	[thread overview]
Message-ID: <20191014120727.GJ16318@brightrain.aerifal.cx> (raw)
In-Reply-To: <CAOt2X9uNdorAtmWc6jDT+dVLf1neu5at+9LMrrfBAB20e9dzGQ@mail.gmail.com>

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
strategies:

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
  conversions

- 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:

6a25313c1122629b43b990ada70af1c209f03a54

Rich


  parent reply	other threads:[~2019-10-14 12:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-14  6:48 Alex Brachet-Mialot
2019-10-14  6:56 ` Alex Brachet-Mialot
2019-10-14 12:12   ` Rich Felker
2019-10-14 12:07 ` Rich Felker [this message]
2019-10-14 13:57   ` Micha Nelissen
2019-10-14 14:23     ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191014120727.GJ16318@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).