mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] Use open_memstream(3) for more efficient asprintf
@ 2019-10-14  6:48 Alex Brachet-Mialot
  2019-10-14  6:56 ` Alex Brachet-Mialot
  2019-10-14 12:07 ` Rich Felker
  0 siblings, 2 replies; 6+ messages in thread
From: Alex Brachet-Mialot @ 2019-10-14  6:48 UTC (permalink / raw)
  To: musl


[-- Attachment #1.1: Type: text/plain, Size: 236 bytes --]

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!

[-- Attachment #1.2: Type: text/html, Size: 281 bytes --]

[-- Attachment #2: asprintf.patch --]
[-- Type: application/octet-stream, Size: 575 bytes --]

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;
 }

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Use open_memstream(3) for more efficient asprintf
  2019-10-14  6:48 [PATCH] Use open_memstream(3) for more efficient asprintf 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
  1 sibling, 1 reply; 6+ messages in thread
From: Alex Brachet-Mialot @ 2019-10-14  6:56 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 1222 bytes --]

That's strange, I looked on the archives and it looks like my
attachment was in a weird format, something my email client did
perhaps. Here it is copied I'll also try again with .txt extension so
my mail client is less confused hopefully. Sorry about that.

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;
 }


On Mon, Oct 14, 2019 at 2:48 AM Alex Brachet-Mialot
<alexbrachetmialot@gmail.com> 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!

[-- Attachment #2: asprintf.txt --]
[-- Type: text/plain, Size: 575 bytes --]

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;
 }

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Use open_memstream(3) for more efficient asprintf
  2019-10-14  6:48 [PATCH] Use open_memstream(3) for more efficient asprintf Alex Brachet-Mialot
  2019-10-14  6:56 ` Alex Brachet-Mialot
@ 2019-10-14 12:07 ` Rich Felker
  2019-10-14 13:57   ` Micha Nelissen
  1 sibling, 1 reply; 6+ messages in thread
From: Rich Felker @ 2019-10-14 12:07 UTC (permalink / raw)
  To: musl

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Re: [PATCH] Use open_memstream(3) for more efficient asprintf
  2019-10-14  6:56 ` Alex Brachet-Mialot
@ 2019-10-14 12:12   ` Rich Felker
  0 siblings, 0 replies; 6+ messages in thread
From: Rich Felker @ 2019-10-14 12:12 UTC (permalink / raw)
  To: musl

On Mon, Oct 14, 2019 at 02:56:36AM -0400, Alex Brachet-Mialot wrote:
> That's strange, I looked on the archives and it looks like my
> attachment was in a weird format, something my email client did
> perhaps. Here it is copied I'll also try again with .txt extension so
> my mail client is less confused hopefully. Sorry about that.

It came through ok -- the attachment was just marked as a binary file
rather than text. If your mailer can send text attachments without
mangling the content, it's preferable to send as text so that
archive/recipients' mail software will show them, but either is okay
and binary (application/octet-streaam) can be better if you're at all
worried that your mailer will mess things up, e.g. replacing tabs with
spaces.

Rich


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Use open_memstream(3) for more efficient asprintf
  2019-10-14 12:07 ` Rich Felker
@ 2019-10-14 13:57   ` Micha Nelissen
  2019-10-14 14:23     ` Rich Felker
  0 siblings, 1 reply; 6+ messages in thread
From: Micha Nelissen @ 2019-10-14 13:57 UTC (permalink / raw)
  To: musl

On 14-10-2019 14:07, Rich Felker wrote:
> - 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)

This seems to imply linear buffer size growth doesn't cause 
fragmentation? In general it does I would assume.

> - avoiding fragmentation produced by realloc

I guess the idea behind the original malloc(GUESS) (and also this 
memstream implementation?) is that it allocates from end of heap, and 
therefore realloc is very cheap: just move end of heap pointer. Although 
in a multi-threaded world this becomes less likely depending on the 
intensity of malloc/realloc usage by other threads.

Micha


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Use open_memstream(3) for more efficient asprintf
  2019-10-14 13:57   ` Micha Nelissen
@ 2019-10-14 14:23     ` Rich Felker
  0 siblings, 0 replies; 6+ messages in thread
From: Rich Felker @ 2019-10-14 14:23 UTC (permalink / raw)
  To: musl

On Mon, Oct 14, 2019 at 03:57:31PM +0200, Micha Nelissen wrote:
> On 14-10-2019 14:07, Rich Felker wrote:
> >- 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)
> 
> This seems to imply linear buffer size growth doesn't cause
> fragmentation? In general it does I would assume.

It doesn't cause internal fragmentation (wasted space inside the
allocated block because too much was allocated). It still causes
external fragmentation.

> >- avoiding fragmentation produced by realloc
> 
> I guess the idea behind the original malloc(GUESS) (and also this
> memstream implementation?) is that it allocates from end of heap,
> and therefore realloc is very cheap: just move end of heap pointer.
> Although in a multi-threaded world this becomes less likely
> depending on the intensity of malloc/realloc usage by other threads.

The idea behind malloc(GUESS) was just to avoid two passes of
vsnprintf in the common case where the output is short; no
consideration of fragmentation was made. In general this kind of
pattern will result in breaking up of larger free areas rather than
using a suitable smaller one that's available. The malloc
implementation can mitigate this (and the future one will) by using
realloc as an opportunity to defragment, but that costs memcpy.

Note that one thing we could do, that would be better in almost all
ways, is make the first snprintf call write to an automatic (stack)
buffer of some reasonable size rather than null. Then, if the result
fit, we can just return strdup(buf) as the result rather than doing a
second pass.

Rich


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-10-14 14:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-14  6:48 [PATCH] Use open_memstream(3) for more efficient asprintf 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
2019-10-14 13:57   ` Micha Nelissen
2019-10-14 14:23     ` Rich Felker

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).