python String formatting Debug message legibility vs. performance... avoid f-strings entirely? #1623
Replies: 8 comments 4 replies
so roughly a 1000 debug lines to change, at minimum. |
|
I like f-strings and have always tried to use them when writing new code. But I didn't even know the logger supported the C-style formatting with commas until that PR was created. In non-logger situations, I think f-strings should be the standard. The risk of a formatting error causing a crash that wouldn't be detected until If we decide to use the deferred formatting for logs, would we want to apply it to all logs or just debug? It seems annoying to have different standards for different log levels. |
|
fwiw... Gemini prepared a patch to implement this... testing... |
|
I also prefer f-strings for readability, and because I have a similar "use modern" bias to what @petersilva expressed. f-strings are demonstrably faster than The implementation change in Python 3.12 to base them on the PEG parser means, I think, that they will benefit from at least some of the ongoing interpreter performance improvements. I know that logging plays an important role in sarracenia. Has anyone profiled to get a feel for significant the impact of the eager evaluation operation of f-strings might be in the overall scheme of things? |
|
if you're looking for some numbers, I have some from when I put together #1613 :) The thing is, f-strings and % formatting are both fast at building strings.. that's not the issue. The issue is building strings that nobody reads. In production we run at INFO or WARNING, so every here: bench on a realistic message dict:
On its own each call is tiny, but in To 🧠Reid's point about different standards per level -- I think this only really matters for debug? At INFO/WARNING/ERROR you're logging maybe a handful of times per batch, not per message. The overhead is negligible there, so f-strings are totally fine for readability. It's really just the debug-level hot path stuff where lazy formatting pays off. So maybe the guideline is just: use lazy formatting for debug, f-strings are fine everywhere else? That keeps things simple and avoids the formatting-error-hiding concern Reid mentioned, since you'd only hit that at debug level anyway. |
|
so read through the link from Doug to realpython, and it mentions a tool called flynt, which automates the conversion of strings, as this is a very common problem. It even mentions about not converting debug logs... before I started: idefix% find sarracenia -name '*.py' | grep -v .pybuild | xargs grep -n logger'.*(' | grep -v logger.debug | grep -v f\" | grep -v f\' | grep % | wc -l
254
idefix% find sarracenia -name '*.py' | xargs flynt
idefix% find sarracenia -name '*.py' | grep -v .pybuild | xargs grep -n logger'.*(' | grep -v logger.debug | grep -v f\" | grep -v f\' | grep % | wc -l
76
So I looked at the result, and it looks like the stuff it did was good. I ran the flow tests on the resulting code. It seems to have left 76 logger statements, and looking them over, all the ones left are logger.info's and:
So doing a second pass looking at those manually. a day or so for a PR. |
|
See #1628. It should update the entire app to f-strings appropriately (except logger.debug) |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hey folks,
in #1613, and #1618, the AI @robjarawan is using reminds of a really interesting fact:
This means that a lot of cpu is consumed in the first two cases, calculating strings that will never be used
in, say debug messages.
Until now:
I had always been opportunistically converting old-style string formatting, which is generally
discouraged, by the more moden f strings which are considered easier to read. So this raises the question
of should we make this change in a comprehensive way in the the code, and document the reasoning in
the developer guide.
All reactions