chore(util): using newest bytespool

@formatter:off

Signed-off-by: slasher <shenjie1@oppo.com>
This commit is contained in:
slasher 2025-04-03 11:57:27 +08:00 committed by zhumingze1108
parent d936fb90cc
commit 74a7c62171
3 changed files with 22 additions and 95 deletions

View File

@ -14,10 +14,7 @@
package bytespool
import (
"bytes"
"sync"
)
import "sync"
const (
zeroSize = 1 << 14 // 16 KB
@ -125,17 +122,3 @@ func msb(size int) byte {
v |= v >> 16
return debruijinPosition[(v*0x07C4ACDD)>>27]
}
func AllocWithZeroLengthBuffer(size int) *bytes.Buffer {
byteSlice := AllocWithZeroLength(size)
buffer := bytes.NewBuffer(byteSlice)
return buffer
}
func AllocWithZeroLength(size int) []byte {
if pool := GetPool(size); pool != nil {
b := pool.Get().([]byte)
return b[:0]
}
return make([]byte, 0, size)
}

View File

@ -26,9 +26,9 @@ import (
"syscall"
"time"
"github.com/cubefs/cubefs/blobstore/util/bytespool"
"github.com/cubefs/cubefs/proto"
"github.com/cubefs/cubefs/util/buf"
"github.com/cubefs/cubefs/util/bytespool"
)
const (
@ -54,7 +54,7 @@ type PutCacheRequest struct {
}
func (req *PutCacheRequest) Marshal() (result []byte, err error) {
buff := bytespool.AllocWithZeroLengthBuffer(4 + len(req.CacheKey) + 4 + len(req.Data) + 4 + len(req.VolName))
buff := bytespool.NewBuffer(4 + len(req.CacheKey) + 4 + len(req.Data) + 4 + len(req.VolName))
// cache key
err = binary.Write(buff, binary.BigEndian, uint32(len(req.CacheKey)))
if err != nil {
@ -139,7 +139,7 @@ type GetCacheRequest struct {
}
func (req *GetCacheRequest) Marshal() (result []byte, err error) {
buff := bytespool.AllocWithZeroLengthBuffer(4 + len(req.CacheKey) + 8 + 4)
buff := bytespool.NewBuffer(4 + len(req.CacheKey) + 8 + 4)
// cache key
err = binary.Write(buff, binary.BigEndian, uint32(len(req.CacheKey)))
if err != nil {
@ -194,7 +194,7 @@ type GetCachePathResponse struct {
}
func (req *GetCachePathResponse) Marshal() (result []byte, err error) {
buff := bytespool.AllocWithZeroLengthBuffer(4 + len(req.CachePath))
buff := bytespool.NewBuffer(4 + len(req.CachePath))
// cache key
err = binary.Write(buff, binary.BigEndian, uint32(len(req.CachePath)))
if err != nil {
@ -231,7 +231,7 @@ type DelCacheRequest struct {
}
func (req *DelCacheRequest) Marshal() (result []byte, err error) {
buff := bytespool.AllocWithZeroLengthBuffer(4 + len(req.CacheKey))
buff := bytespool.NewBuffer(4 + len(req.CacheKey))
// cache key
err = binary.Write(buff, binary.BigEndian, uint32(len(req.CacheKey)))
if err != nil {

View File

@ -1,83 +1,27 @@
package bytespool
import "sync"
import (
"bytes"
func newBytes(size int) func() interface{} {
return func() interface{} {
return make([]byte, size)
}
}
const (
zeroSize int = 1 << 14 // 16K
// 1K - 2K - 4K - 8K - 16K - 32K - 64K - 128K - 256K - 512K - 1M
numPools = 11
sizeStep = 2
startSize int = 1 << 10 // 1K
maxSize int = 1 << 20 // 1M
"github.com/cubefs/cubefs/blobstore/util/bytespool"
)
var (
zero = make([]byte, zeroSize)
pools [numPools]sync.Pool
poolSize [numPools]int
GetPool = bytespool.GetPool
Alloc = bytespool.Alloc
Free = bytespool.Free
Zero = bytespool.Zero
)
func init() {
size := startSize
for ii := 0; ii < numPools; ii++ {
pools[ii] = sync.Pool{
New: newBytes(size),
}
poolSize[ii] = size
size *= sizeStep
}
// NewBuffer returns empty buffer with sized capacity.
func NewBuffer(size int) *bytes.Buffer {
b := bytes.NewBuffer(Alloc(size))
b.Reset()
return b
}
// GetPool returns a sync.Pool that generates bytes slice with the size.
// Return nil if no such pool exists.
func GetPool(size int) *sync.Pool {
for idx, psize := range poolSize {
if size <= psize {
return &pools[idx]
}
}
return nil
}
// Alloc returns a bytes slice with the size.
// Make a new bytes slice if oversize.
func Alloc(size int) []byte {
if pool := GetPool(size); pool != nil {
b := pool.Get().([]byte)
return b[:size]
}
return make([]byte, size)
}
// Free puts the bytes slice into suitable pool.
// Discard the bytes slice if oversize.
func Free(b []byte) {
size := cap(b)
if size > maxSize {
return
}
b = b[0:size]
for ii := numPools - 1; ii >= 0; ii-- {
if size >= poolSize[ii] {
pools[ii].Put(b) // nolint: staticcheck
return
}
}
}
// Zero clean up the bytes slice b to zero.
func Zero(b []byte) {
for len(b) > 0 {
n := copy(b, zero)
b = b[n:]
}
// FreeBuffer free the underlying bytes.
func FreeBuffer(b *bytes.Buffer) {
b.Reset()
Free(b.Bytes())
}