Skip to content

Commit

Permalink
region: Simplify buffer funcs in compressor.go
Browse files Browse the repository at this point in the history
Replace `growBuffer` with 'append' and `resizeBufferCap` with
`slices.Grow`. These are equivalent after undoing the unintentional
change from commit 1fee39f. Benchmarks show the performance is
equivalent to the code before commit 1fee39f.

Also replace some deprecated calls to rand funcs.
  • Loading branch information
aaronbee committed Jun 13, 2024
1 parent faec3bd commit 7d8e605
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
23 changes: 3 additions & 20 deletions region/compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"net"
"slices"

"github.com/tsuna/gohbase/compression"
)
Expand All @@ -25,24 +26,6 @@ func min(x, y uint32) int {
return int(y)
}

func growBuffer(b []byte, sz int) []byte {
l := len(b) + sz
if l <= cap(b) {
return b[:l]
}
return append(b, make([]byte, sz)...)
}

func resizeBufferCap(b []byte, capacity int) []byte {
if capacity <= cap(b) {
return b
}

b2 := make([]byte, capacity)
copy(b2, b)
return b2[:len(b)]
}

func (c *compressor) compressCellblocks(cbs net.Buffers, uncompressedLen uint32) []byte {
b := newBuffer(4)

Expand All @@ -62,7 +45,7 @@ func (c *compressor) compressCellblocks(cbs net.Buffers, uncompressedLen uint32)

// grow for chunk length
lenOffset = len(b)
b = growBuffer(b, 4)
b = append(b, make([]byte, 4)...)

b, chunkLen = c.Encode(uncompressedBuffer[:n], b)

Expand Down Expand Up @@ -122,7 +105,7 @@ func (c *compressor) decompressCellblocks(b []byte) ([]byte, error) {
return nil, fmt.Errorf("failed to read uncompressed block length: %w", err)
}

out = resizeBufferCap(out, len(out)+int(uncompressedBlockLen))
out = slices.Grow(out, int(uncompressedBlockLen))

// read and decompress encoded chunks until whole block is read
var uncompressedSoFar uint32
Expand Down
4 changes: 2 additions & 2 deletions region/compressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ func benchmarkDecompressCellblocks(b *testing.B, blockLen int, blocksCount int)
b.ReportAllocs()

data := make([]byte, blockLen)
rand.Seed(int64(b.N))
r := rand.New(rand.NewSource(42))

c := &compressor{Codec: mockCodec{}}

var compressedCellblocks []byte
for i := 0; i < blocksCount; i++ {
_, err := rand.Read(data)
_, err := r.Read(data)
if err != nil {
b.FailNow()
}
Expand Down

0 comments on commit 7d8e605

Please sign in to comment.