git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/2] mem-pool: add mem_pool_strfmt()

From
Jeff King <peff@peff.net>
Date
Mar 7, 2024, 09:58 UTC
Message-ID
<20240307095841.GD2650063@coredump.intra.peff.net>
In-Reply-To
<3aed87d0-4789-4e96-8c6c-31cab2d996cc@web.de>
On Tue, Feb 27, 2024 at 06:58:26PM +0100, René Scharfe wrote:
Show 30 quoted lines
> >> It would not allow the shortcut of using the vast pool as a scratch
> >> space to format the string with a single vsnprintf call in most cases.
> >> Or am I missing something?
> >
> > So here it sounds like you do care about some of the performance
> > aspects. So no, it would not allow that. You'd be using the vast pool of
> > heap memory provided by malloc(), and trusting that a call to malloc()
> > is not that expensive in practice. I don't know how true that is, or how
> > much it matters for this case.
> 
> In the specific use case we can look at three cases:
> 
> 1. xstrfmt() [before 1c56fc2084]
> 2. size calculation + pre-size + strbuf_addf() [1c56fc2084]
> 3. mem_pool_strfmt() [this patch]
> 
> The performance of 2 and 3 is basically the same, 1 was worse.
> 
> xstrfmt() and strbuf_addf() both wrap strbuf_vaddf(), which uses
> malloc() and vsnprintf().  My conclusion is that malloc() is fast
> enough, but running vsnprintf() twice is slow (first time to determine
> the allocation size, second time to actually build the string).  With a
> memory pool we almost always only need to call it once per string, and
> that's why I use it here.
> 
> The benefit of this patch (to me) is better code readability (no more
> manual pre-sizing) without sacrificing performance.
> 
> The ability to clear (or UNLEAK) all these strings in one go is just a
> bonus.

Ah, OK. I admit I did not read the series all that carefully. Mostly I am just concerned about a world where there are parallel-universe versions of every allocating function that takes a mem-pool. If it's limited to this obvious string formatting variant that may not be too bad, though.

-Peff
Previous: René Scharfe
Message 10 of 10 in “name-rev: use memory pool for name strings”
  1. 0/2 name-rev: use memory pool for name stringsRené Scharfe, Feb 25, 2024
  2. 2/2 name-rev: use mem_pool_strfmt()René Scharfe, Feb 25, 2024
  3. Junio C HamanoFeb 26, 2024
  4. René ScharfeFeb 26, 2024
  5. 1/2 mem-pool: add mem_pool_strfmt()René Scharfe, Feb 25, 2024
  6. Jeff KingFeb 26, 2024
  7. René ScharfeFeb 26, 2024
  8. Jeff KingFeb 27, 2024
  9. René ScharfeFeb 27, 2024
  10. Jeff KingMar 7, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.