From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/14806 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] Use open_memstream(3) for more efficient asprintf Date: Mon, 14 Oct 2019 08:07:27 -0400 Message-ID: <20191014120727.GJ16318@brightrain.aerifal.cx> References: Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="232712"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-14822-gllmg-musl=m.gmane.org@lists.openwall.com Mon Oct 14 14:07:43 2019 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1iJz8Z-000yQe-BB for gllmg-musl@m.gmane.org; Mon, 14 Oct 2019 14:07:43 +0200 Original-Received: (qmail 25689 invoked by uid 550); 14 Oct 2019 12:07:40 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 25671 invoked from network); 14 Oct 2019 12:07:39 -0000 Content-Disposition: inline In-Reply-To: Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:14806 Archived-At: 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