feat: ProtobufParse.formatOpenMetricsFloat: improve float formatting …#15167
feat: ProtobufParse.formatOpenMetricsFloat: improve float formatting …#15167machine424 merged 1 commit intoprometheus:mainfrom
Conversation
beorn7
left a comment
There was a problem hiding this comment.
Interesting approach. I have some nits about doc comments and maybe a useful idea.
model/textparse/protobufparse.go
Outdated
| return "-Inf" | ||
| } | ||
| s := fmt.Sprint(f) | ||
| s := string(strconv.AppendFloat(b[:0], f, 'g', -1, 64)) |
There was a problem hiding this comment.
Interesting that this is really measurably better in performance.
Now that we are acting on a bytes buffer anyway, how about folding the operation down in line 641 into this? I.e. appending the ".0" string is much more efficient if using a mutable bytes buffer rather than string concatenation.
There was a problem hiding this comment.
Good idea, yes it should be beneficial (even though it doesn't change anything in BenchmarkParse case).
|
I have quickly dived into the However, with my idea in the comments above (i.e. also append |
|
Real life example (from Prometheus's own metrics): |
I had tried a global sync.Pool, seen no difference, but I wasn't aware a new parser was created on every scrape/iteration. Avoiding a requisite 24bytes per scrape will always be better, I'll go back to sync.Pool. |
beorn7
left a comment
There was a problem hiding this comment.
Looks good, just a question about maybe using defer.
How do the benchmark look like with the current state?
Let's maybe add the case where .0 has to be appended to the benchmarks to see how well it works?
model/textparse/protobufparse.go
Outdated
| *bp = strconv.AppendFloat((*bp)[:0], f, 'g', -1, 64) | ||
| if bytes.ContainsAny(*bp, "e.") { | ||
| s := string(*bp) | ||
| floatFormatBufPool.Put(bp) |
There was a problem hiding this comment.
Let's put the Put in a defer statement? I have vague memories that the performance overhead of that should be much smaller these days…
Being able to write return string (*bp) is totally worth it, IMHO. :)
There was a problem hiding this comment.
Yes, I wanted to test the version without defer but didn't restore.
I added it to the description.
Saw this coming, I realized I was asking for trouble when I pointed out that the tests didn’t cover that case :) Added one. Comparing it with the "alter in string" version |
|
Just make sure to get the DCO right when squashing… |
…by using strconv.AppendFloat instead of fmt.Sprint Signed-off-by: machine424 <ayoubmrini424@gmail.com>
…by using strconv.AppendFloat instead of fmt.Sprint
For
BenchmarkParseuse case (use "a lot" of summaries)v1.txtbeing the PR.Still cannot justify the
Parse/data=promtestdata.txt/parser=xpfmt-2 693.5µ ± 24% 744.2µ ± 18% +7.30% (p=0.041 n=6)asformatOpenMetricsFloatis only used bypromprotoRan with
sync.Pool