Prometheus: Improve native histogram response parsing performance (#102024)

* introduce tests

* improve allocation by %61

* ~%27 improvement

goos: darwin
goarch: arm64
pkg: github.com/grafana/grafana/pkg/promlib/converter
cpu: Apple M1 Pro
                                                            │  pmem.0.txt  │             pmem.2.txt             │
                                                            │    sec/op    │   sec/op     vs base               │
ReadPrometheusStyleResult_FromFile/prom-query-range.json-10   117.72µ ± 6%   82.90µ ± 4%  -29.57% (p=0.002 n=6)

                                                            │  pmem.0.txt   │             pmem.2.txt              │
                                                            │     B/op      │     B/op      vs base               │
ReadPrometheusStyleResult_FromFile/prom-query-range.json-10   104.34Ki ± 0%   76.09Ki ± 0%  -27.08% (p=0.002 n=6)

                                                            │ pmem.0.txt  │            pmem.2.txt             │
                                                            │  allocs/op  │ allocs/op   vs base               │
ReadPrometheusStyleResult_FromFile/prom-query-range.json-10   2463.0 ± 0%   899.0 ± 0%  -63.50% (p=0.002 n=6)

* add more tests

* remove comment lines

* read string as slice to prevent type conversion

* golang lint

* revert appendValueFromString improvement

* fix merging issues

* improve native histogram response parsing
pull/102032/head
ismail simsek 9 months ago committed by GitHub
parent a484c2cb6f
commit dcba71a0b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 14
      pkg/promlib/converter/prom.go

@ -6,6 +6,7 @@ import (
"slices"
"strconv"
"time"
"unsafe"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
@ -858,14 +859,17 @@ func readHistogram(iter *sdkjsoniter.Iterator, hist *histogramInfo) error {
}
func appendValueFromString(iter *sdkjsoniter.Iterator, field *data.Field) error {
var err error
var s string
if s, err = iter.ReadString(); err != nil {
// Read the string directly into our buffer
buf, err := iter.ReadStringAsSlice()
if err != nil {
return err
}
var v float64
if v, err = strconv.ParseFloat(s, 64); err != nil {
// #nosec G103
// Convert string to float64 without allocation
// https://github.com/search?q=org%3Agrafana+yoloString&type=code
v, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&buf)), 64)
if err != nil {
return err
}

Loading…
Cancel
Save