Skip to content

Commit

Permalink
region: Simplify buffer funcs
Browse files Browse the repository at this point in the history
Replace `growBuffer` with 'append' and `resizeBufferCap` with
`slices.Grow`. And remove redundant 'if' checks on capacity or size
that `append` will do as well. This is equivalent behavior 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 authored and dethi committed Jun 14, 2024
1 parent faec3bd commit c100991
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 26 deletions.
5 changes: 1 addition & 4 deletions region/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ func newBuffer(size int) []byte {
if v != nil {
b = v.([]byte)
}
if cap(b) < size {
return append(b[:0], make([]byte, size)...)[:size]
}
return b[:size]
return append(b[:0], make([]byte, size)...)
}

func freeBuffer(b []byte) {
Expand Down
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 c100991

Please sign in to comment.