Skip to content

Commit

Permalink
store/postings_codec: return both if possible
Browse files Browse the repository at this point in the history
Signed-off-by: Giedrius Statkevičius <[email protected]>
  • Loading branch information
GiedriusS committed Apr 11, 2023
1 parent 6c67dc1 commit 9d05c09
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions pkg/store/postings_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,46 +91,53 @@ type closeablePostings interface {
close()
}

// alias returns true if given slices have the same both backing array.
// See: https://groups.google.com/g/golang-nuts/c/C6ufGl73Uzk.
func alias(x, y []byte) bool {
return cap(x) > 0 && cap(y) > 0 && &x[0:cap(x)][cap(x)-1] == &y[0:cap(y)][cap(y)-1]
}

func diffVarintSnappyDecode(input []byte) (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)
}

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

biggerSlice := raw
if cap(dstBuf) > cap(biggerSlice) {
biggerSlice = dstBuf
if !alias(raw, dstBuf) {
toFree = append(toFree, raw)
}

return newDiffVarintPostings(raw, biggerSlice), nil
return newDiffVarintPostings(raw, toFree), nil
}

func newDiffVarintPostings(input, freeSlice []byte) *diffVarintPostings {
return &diffVarintPostings{freeSlice: freeSlice, buf: &encoding.Decbuf{B: input}}
func newDiffVarintPostings(input []byte, freeSlices [][]byte) *diffVarintPostings {
return &diffVarintPostings{freeSlices: freeSlices, buf: &encoding.Decbuf{B: input}}
}

// diffVarintPostings is an implementation of index.Postings based on diff+varint encoded data.
type diffVarintPostings struct {
buf *encoding.Decbuf
cur storage.SeriesRef
freeSlice []byte
buf *encoding.Decbuf
cur storage.SeriesRef
freeSlices [][]byte
}

func (it *diffVarintPostings) close() {
if it.freeSlice == nil {
return
for i := range it.freeSlices {
snappyDecodePool.Put(&it.freeSlices[i])
}
snappyDecodePool.Put(&it.freeSlice)
}

func (it *diffVarintPostings) At() storage.SeriesRef {
Expand Down

0 comments on commit 9d05c09

Please sign in to comment.