Prometheus: Reduce allocations parsing exemplars (#58959)

* Prometheus: Reduce allocations parsing exemplars

This reduces allocations for parsing exemplars.

name            old time/op    new time/op    delta
ExemplarJson-8    24.7ms ±15%    17.6ms ± 2%  -28.72%  (p=0.008 n=5+5)

name            old alloc/op   new alloc/op   delta
ExemplarJson-8    4.70MB ± 0%    3.58MB ± 0%  -23.79%  (p=0.008 n=5+5)

name            old allocs/op  new allocs/op  delta
ExemplarJson-8     72.6k ± 0%     69.1k ± 0%   -4.81%  (p=0.008 n=5+5)

* Ensure pairs is reset
This commit is contained in:
Christian Simon 2023-01-18 15:42:22 +00:00 committed by GitHub
parent 1fdd3767f1
commit a1f2d0e205
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View File

@ -37,6 +37,7 @@ func BenchmarkExemplarJson(b *testing.B) {
tCtx, err := setup(true)
require.NoError(b, err)
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
res := http.Response{
StatusCode: 200,

View File

@ -227,8 +227,8 @@ func readArrayData(iter *jsoniter.Iterator) backend.DataResponse {
}
// For consistent ordering read values to an array not a map
func readLabelsAsPairs(iter *jsoniter.Iterator) [][2]string {
pairs := make([][2]string, 0, 10)
func readLabelsAsPairs(iter *jsoniter.Iterator, pairs [][2]string) [][2]string {
pairs = pairs[:0]
for k := iter.ReadObject(); k != ""; k = iter.ReadObject() {
pairs = append(pairs, [2]string{k, iter.ReadString()})
}
@ -269,7 +269,7 @@ func readLabelsOrExemplars(iter *jsoniter.Iterator) (*data.Frame, [][2]string) {
case "labels":
max := 0
for _, pair := range readLabelsAsPairs(iter) {
for _, pair := range readLabelsAsPairs(iter, pairs) {
k := pair[0]
v := pair[1]
f, ok := lookup[k]
@ -304,6 +304,7 @@ func readLabelsOrExemplars(iter *jsoniter.Iterator) (*data.Frame, [][2]string) {
}
default:
v := fmt.Sprintf("%v", iter.Read())
pairs = pairs[:0]
pairs = append(pairs, [2]string{l1Field, v})
}
}