Skip to content

Commit

Permalink
rpc_util: merge fallback bytespool functionality into bytepool
Browse files Browse the repository at this point in the history
  • Loading branch information
hueypark committed May 20, 2023
1 parent 96f5a27 commit fe1294f
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions shared_buffer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewSharedBufferPool() SharedBufferPool {
newBytesPool(level2PoolMaxSize),
newBytesPool(level3PoolMaxSize),
newBytesPool(level4PoolMaxSize),
newFallbackBytesPool(),
newBytesPool(0),
},
}
}
Expand Down Expand Up @@ -114,46 +114,33 @@ type simpleSharedBufferChildPool interface {

type bufferPool struct {
sync.Pool

defaultSize int
}

func (p *bufferPool) Get(size int) []byte {
bs := p.Pool.Get().(*[]byte)

return (*bs)[:size]
}

func newBytesPool(size int) simpleSharedBufferChildPool {
return &bufferPool{
sync.Pool{
New: func() interface{} {
bs := make([]byte, size)
return &bs
},
},
}
}

type fallbackBufferPool struct {
sync.Pool
}

func (p *fallbackBufferPool) Get(size int) []byte {
bs := p.Pool.Get().(*[]byte)
// If default size is 0. It means this pool is fallback pool.
// Therefore, we need to make a new one if the requested size is larger than the buffer.
if p.defaultSize == 0 {
if cap(*bs) < size {
*bs = make([]byte, size)
return *bs
}
}

return (*bs)[:size]
}

func newFallbackBytesPool() simpleSharedBufferChildPool {
return &fallbackBufferPool{
sync.Pool{
func newBytesPool(size int) simpleSharedBufferChildPool {
return &bufferPool{
Pool: sync.Pool{
New: func() interface{} {
return new([]byte)
bs := make([]byte, size)
return &bs
},
},
defaultSize: size,
}
}

Expand Down

0 comments on commit fe1294f

Please sign in to comment.