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

Re: fprintf_ln() is slow

From
Jeff King <peff@peff.net>
Date
Jun 27, 2019, 21:10 UTC
Message-ID
<20190627210959.GA20250@sigill.intra.peff.net>
In-Reply-To
<nycvar.QRO.7.76.6.1906271358260.44@tvgsbejvaqbjf.bet>
On Thu, Jun 27, 2019 at 02:00:54PM +0200, Johannes Schindelin wrote:
Show 5 quoted lines
> > I can get similar speedups by formatting into a buffer:
> [..]
> > But we shouldn't have to resort to that.
> 
> Why not?

My thinking was that it was introducing an extra copy of the data. But on further reflection, it probably doesn't, at least for the case of an unbuffered stream (we might want to predicate it on "fp == stderr", though).

> It would make for a perfectly fine excuse to finally get work going in
> direction of a initially heap-backed strbuf. Which we have wanted for ages
> now.
I assume you initially stack-backed strbuf?

Perhaps. I'm not sure that it's better for the fallback when we need more than the stack buffer to be allocating more memory as opposed to just calling fprintf() and sending it out in multiple writes.

Show 6 quoted lines
> > We can use setvbuf() to toggle buffering back and forth, but I'm not
> > sure if there's a way to query the current buffering scheme for a stdio
> > stream.
> 
> It also is not very safe, especially when we want to have this work in a
> multi-threaded fashion.

I considered that, too, but I think it is safe. stdio has its own locking, so every individual call is atomic. The potentially problematic case would be where we switch back from line buffering to no-buffering, and somebody else has written some content into our stack-based buffer (that is about to go out of scope!). But I'd assume that as part of the switch to no-buffering that any stdio implementation would flush out the buffer that it's detaching from (while under lock). Nothing else makes sense.

That said...
> I'd be much more comfortable with rendering the string into a buffer and
> then sending that buffer wholesale to stderr.

It's sufficiently complex that I think I prefer to just use our own buffer, too.

It also makes it more likely for the newline and the message to end up in an atomic write(), so if multiple threads _are_ writing to stderr they'd be more likely to stay together.

It does sound like people in the other part of the thread are OK with just getting rid of the "_ln" functions altogether.

-Peff
Previous: Johannes SchindelinNext: Phillip Wood
Message 11 of 13 in “fprintf_ln() is slow”
  1. Jeff KingJun 27, 2019
  2. Jeff KingJun 27, 2019
  3. Duy NguyenJun 27, 2019
  4. Ævar Arnfjörð BjarmasonJun 27, 2019
  5. Duy NguyenJun 27, 2019
  6. Junio C HamanoJun 27, 2019
  7. Jeff KingJun 27, 2019
  8. Jeff KingJun 27, 2019
  9. Junio C HamanoJun 27, 2019
  10. Johannes SchindelinJun 27, 2019
  11. Jeff KingJun 27, 2019
  12. Phillip WoodJun 28, 2019
  13. Jeff KingJun 28, 2019

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.