Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store: disable pooling for postings benchmarks #6473

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions pkg/store/postings_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
)

func decodePostings(input []byte) (closeablePostings, error) {
var df func([]byte) (closeablePostings, error)
var df func([]byte, bool) (closeablePostings, error)

switch {
case isDiffVarintSnappyEncodedPostings(input):
Expand All @@ -45,7 +45,7 @@ func decodePostings(input []byte) (closeablePostings, error) {
return nil, fmt.Errorf("unrecognize postings format")
}

return df(input)
return df(input, false)
}

// isDiffVarintSnappyEncodedPostings returns true, if input looks like it has been encoded by diff+varint+snappy codec.
Expand Down Expand Up @@ -129,12 +129,12 @@ func diffVarintSnappyStreamedEncode(p index.Postings, length int) ([]byte, error
return compressedBuf.Bytes(), nil
}

func diffVarintSnappyStreamedDecode(input []byte) (closeablePostings, error) {
func diffVarintSnappyStreamedDecode(input []byte, disablePooling bool) (closeablePostings, error) {
if !isDiffVarintSnappyStreamedEncodedPostings(input) {
return nil, errors.New("header not found")
}

return newStreamedDiffVarintPostings(input[len(codecHeaderStreamedSnappy):])
return newStreamedDiffVarintPostings(input[len(codecHeaderStreamedSnappy):], disablePooling)
}

type streamedDiffVarintPostings struct {
Expand All @@ -144,7 +144,10 @@ type streamedDiffVarintPostings struct {
err error
}

func newStreamedDiffVarintPostings(input []byte) (closeablePostings, error) {
func newStreamedDiffVarintPostings(input []byte, disablePooling bool) (closeablePostings, error) {
if disablePooling {
return &streamedDiffVarintPostings{sr: s2.NewReader(bytes.NewBuffer(input))}, nil
}
r, err := extsnappy.Compressor.DecompressByteReader(bytes.NewBuffer(input))
if err != nil {
return nil, fmt.Errorf("decompressing snappy postings: %w", err)
Expand Down Expand Up @@ -259,26 +262,28 @@ func alias(x, y []byte) bool {
}

// TODO(GiedriusS): remove for v1.0.
func diffVarintSnappyDecode(input []byte) (closeablePostings, error) {
func diffVarintSnappyDecode(input []byte, disablePooling bool) (closeablePostings, error) {
if !isDiffVarintSnappyEncodedPostings(input) {
return nil, errors.New("header not found")
}

toFree := make([][]byte, 0, 2)

var dstBuf []byte
decodeBuf := snappyDecodePool.Get()
if decodeBuf != nil {
dstBuf = *(decodeBuf.(*[]byte))
toFree = append(toFree, dstBuf)
if !disablePooling {
decodeBuf := snappyDecodePool.Get()
if decodeBuf != nil {
dstBuf = *(decodeBuf.(*[]byte))
toFree = append(toFree, dstBuf)
}
}

raw, err := s2.Decode(dstBuf, input[len(codecHeaderSnappy):])
if err != nil {
return nil, errors.Wrap(err, "snappy decode")
}

if !alias(raw, dstBuf) {
if !alias(raw, dstBuf) && !disablePooling {
toFree = append(toFree, raw)
}

Expand Down
25 changes: 16 additions & 9 deletions pkg/store/postings_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ func TestDiffVarintCodec(t *testing.T) {

codecs := map[string]struct {
codingFunction func(index.Postings, int) ([]byte, error)
decodingFunction func([]byte) (closeablePostings, error)
decodingFunction func([]byte, bool) (closeablePostings, error)
}{
"raw": {codingFunction: diffVarintEncodeNoHeader, decodingFunction: func(bytes []byte) (closeablePostings, error) { return newDiffVarintPostings(bytes, nil), nil }},
"raw": {codingFunction: diffVarintEncodeNoHeader, decodingFunction: func(bytes []byte, disablePooling bool) (closeablePostings, error) {
return newDiffVarintPostings(bytes, nil), nil
}},
"snappy": {codingFunction: diffVarintSnappyEncode, decodingFunction: diffVarintSnappyDecode},
"snappyStreamed": {codingFunction: diffVarintSnappyStreamedEncode, decodingFunction: diffVarintSnappyStreamedDecode},
}
Expand All @@ -81,7 +83,7 @@ func TestDiffVarintCodec(t *testing.T) {
t.Log("encoded size", len(data), "bytes")
t.Logf("ratio: %0.3f", float64(len(data))/float64(4*p.len()))

decodedPostings, err := codec.decodingFunction(data)
decodedPostings, err := codec.decodingFunction(data, false)
testutil.Ok(t, err)

p.reset()
Expand Down Expand Up @@ -212,21 +214,21 @@ func BenchmarkPostingsEncodingDecoding(b *testing.B) {

codecs := map[string]struct {
codingFunction func(index.Postings, int) ([]byte, error)
decodingFunction func([]byte) (closeablePostings, error)
decodingFunction func([]byte, bool) (closeablePostings, error)
}{
"raw": {codingFunction: diffVarintEncodeNoHeader, decodingFunction: func(bytes []byte) (closeablePostings, error) { return newDiffVarintPostings(bytes, nil), nil }},
"raw": {codingFunction: diffVarintEncodeNoHeader, decodingFunction: func(bytes []byte, disablePooling bool) (closeablePostings, error) {
return newDiffVarintPostings(bytes, nil), nil
}},
"snappy": {codingFunction: diffVarintSnappyEncode, decodingFunction: diffVarintSnappyDecode},
"snappyStreamed": {codingFunction: diffVarintSnappyStreamedEncode, decodingFunction: diffVarintSnappyStreamedDecode},
}

b.ReportAllocs()

for _, count := range []int{10000, 100000, 1000000} {
for codecName, codecFns := range codecs {
b.Run(strconv.Itoa(count), func(b *testing.B) {
b.Run(codecName, func(b *testing.B) {
b.Run("encode", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
ps := &uint64Postings{vals: p[:count]}

Expand All @@ -243,13 +245,18 @@ func BenchmarkPostingsEncodingDecoding(b *testing.B) {
if err != nil {
b.Fatal(err)
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := codecFns.decodingFunction(encoded)
decoded, err := codecFns.decodingFunction(encoded, true)
if err != nil {
b.Fatal(err)
}

for decoded.Next() {
var _ = decoded.At()
}
testutil.Ok(b, decoded.Err())
}
})

Expand Down