Skip to content

Commit 57f16a0

Browse files
authored
perf(blooms): Replace JSON lib for encoding/decoding metas (#14767)
Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
1 parent cc496c6 commit 57f16a0

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

‎pkg/storage/stores/shipper/bloomshipper/fetcher.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package bloomshipper
22

33
import (
44
"context"
5-
"encoding/json"
65
"os"
76
"path/filepath"
87
"sync"
@@ -11,6 +10,7 @@ import (
1110
"github.com/dolthub/swiss"
1211
"github.com/go-kit/log"
1312
"github.com/go-kit/log/level"
13+
jsoniter "github.com/json-iterator/go"
1414
"github.com/pkg/errors"
1515
"github.com/prometheus/client_golang/prometheus"
1616
"k8s.io/utils/keymutex"
@@ -189,6 +189,8 @@ func (f *Fetcher) processMetasCacheResponse(_ context.Context, refs []MetaRef, k
189189

190190
var lastErr error
191191
var size int64
192+
193+
json := jsoniter.ConfigFastest
192194
for i, ref := range refs {
193195
if raw, ok := found[f.client.Meta(ref).Addr()]; ok {
194196
meta := Meta{
@@ -209,6 +211,8 @@ func (f *Fetcher) writeBackMetas(ctx context.Context, metas []Meta) error {
209211
var err error
210212
keys := make([]string, len(metas))
211213
data := make([][]byte, len(metas))
214+
215+
json := jsoniter.ConfigFastest
212216
for i := range metas {
213217
keys[i] = f.client.Meta(metas[i].MetaRef).Addr()
214218
data[i], err = json.Marshal(metas[i])

‎pkg/storage/stores/shipper/bloomshipper/fetcher_test.go

+46-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"math"
78
"os"
89
"path/filepath"
910
"testing"
@@ -436,16 +437,54 @@ func TestFetcher_IsBlockDir(t *testing.T) {
436437
})
437438
}
438439

439-
func metasFromCache(data map[string][]byte) []Meta {
440-
metas := make([]Meta, 0, len(data))
441-
for _, v := range data {
440+
func Benchmark_Fetcher_processMetasCacheResponse(b *testing.B) {
441+
b.Log(b.N)
442+
443+
cfg := bloomStoreConfig{
444+
numWorkers: 1,
445+
workingDirs: []string{b.TempDir()},
446+
}
447+
448+
c, err := NewBloomClient(cfg, nil, log.NewNopLogger())
449+
require.NoError(b, err)
450+
f, err := NewFetcher(cfg, c, nil, nil, nil, log.NewNopLogger(), v1.NewMetrics(nil))
451+
require.NoError(b, err)
452+
453+
step := math.MaxUint64 / uint64(b.N)
454+
455+
refs := make([]MetaRef, 0, b.N)
456+
keys := make([]string, 0, b.N)
457+
bufs := make([][]byte, 0, b.N)
458+
459+
for i := 0; i < b.N; i++ {
460+
minFp := model.Fingerprint(uint64(i) * step)
461+
maxFp := model.Fingerprint(uint64(i)*step + step)
462+
463+
ref := Ref{
464+
TenantID: "fake",
465+
TableName: "tsdb_index_20000",
466+
Bounds: v1.NewBounds(minFp, maxFp),
467+
StartTimestamp: model.Earliest,
468+
EndTimestamp: model.Latest,
469+
}
470+
metaRef := MetaRef{Ref: ref}
471+
refs = append(refs, metaRef)
472+
473+
keys = append(keys, f.client.Meta(metaRef).Addr())
474+
442475
meta := Meta{
443-
MetaRef: MetaRef{},
476+
MetaRef: metaRef,
477+
Blocks: []BlockRef{{Ref: ref}},
444478
}
445-
_ = json.Unmarshal(v, &meta)
446-
metas = append(metas, meta)
479+
480+
buf, _ := json.Marshal(meta)
481+
bufs = append(bufs, buf)
447482
}
448-
return metas
483+
484+
b.ReportAllocs()
485+
b.ResetTimer()
486+
487+
_, _, _ = f.processMetasCacheResponse(context.TODO(), refs, keys, bufs)
449488
}
450489

451490
func metaRefs(metas []Meta) []MetaRef {

0 commit comments

Comments
 (0)