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
Feb 27, 2024, 07:53 UTC
Message-ID
<20240227075350.GE3263678@coredump.intra.peff.net>
In-Reply-To
<1868c502-b915-42fd-8cb2-efe90429f9b7@web.de>
On Mon, Feb 26, 2024 at 07:17:00PM +0100, René Scharfe wrote:
Show 16 quoted lines
> > This is pulling heavily from strbuf_vaddf(). This might be a dumb idea,
> > but... would it be reasonable to instead push a global flag that causes
> > xmalloc() to use a memory pool instead of the regular heap?
> >
> > Then you could do something like:
> >
> >   push_mem_pool(pool);
> >   str = xstrfmt("%.*s~%d^%d", ...etc...);
> >   pop_mem_pool(pool);
> >
> > It's a little more involved at the caller, but it means that it now
> > works for all allocations, not just this one string helper.
> 
> That would allow to keep track of allocations that would otherwise leak.
> We can achieve that more easily by pushing the pointer to a global array
> and never freeing it.  Hmm.
I see two uses for memory pools:
  1. You want to optimize allocations (either locality or per-allocation
     overhead).
  2. You want to make a bunch of allocations with the same lifetime
     without worrying about their lifetime otherwise. And then you can
     free them all in one swoop at the end.

And my impression is that you were interested in (2) here. If what you're saying is that another way to do that is:

  str = xstrfmt(...whatever...);
  attach_to_pool(pool, str);
  ...much later...
  free_pool(pool);

then yeah, I'd agree. And that is a lot less tricky / invasive than what I suggested.

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

-Peff
Previous: René ScharfeNext: René Scharfe
Message 8 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.