diff --git a/region/compressor.go b/region/compressor.go index 6e6cb10b..a71b9ffa 100644 --- a/region/compressor.go +++ b/region/compressor.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "net" + "slices" "github.com/tsuna/gohbase/compression" ) @@ -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) @@ -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) @@ -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 diff --git a/region/compressor_test.go b/region/compressor_test.go index 3d802522..c8381793 100644 --- a/region/compressor_test.go +++ b/region/compressor_test.go @@ -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() }