1 Star 0 Fork 0

simon/smallnest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
buffer_pool.go 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
simon 提交于 2021-09-04 14:58 +08:00 . 旧版
package util
import (
"math"
"sync"
)
type levelPool struct {
size int
pool sync.Pool
}
func newLevelPool(size int) *levelPool {
return &levelPool{
size: size,
pool: sync.Pool{
New: func() interface{} {
data := make([]byte, size)
return &data
},
},
}
}
type LimitedPool struct {
minSize int
maxSize int
pools []*levelPool
}
func NewLimitedPool(minSize, maxSize int) *LimitedPool {
if maxSize < minSize {
panic("maxSize can't be less than minSize")
}
const multiplier = 2
var pools []*levelPool
curSize := minSize
for curSize < maxSize {
pools = append(pools, newLevelPool(curSize))
curSize *= multiplier
}
pools = append(pools, newLevelPool(maxSize))
return &LimitedPool{
minSize: minSize,
maxSize: maxSize,
pools: pools,
}
}
func (p *LimitedPool) findPool(size int) *levelPool {
if size > p.maxSize {
return nil
}
idx := int(math.Ceil(math.Log2(float64(size) / float64(p.minSize))))
if idx < 0 {
idx = 0
}
if idx > len(p.pools)-1 {
return nil
}
return p.pools[idx]
}
func (p *LimitedPool) findPutPool(size int) *levelPool {
if size > p.maxSize {
return nil
}
if size < p.minSize {
return nil
}
idx := int(math.Floor(math.Log2(float64(size) / float64(p.minSize))))
if idx < 0 {
idx = 0
}
if idx > len(p.pools)-1 {
return nil
}
return p.pools[idx]
}
func (p *LimitedPool) Get(size int) *[]byte {
sp := p.findPool(size)
if sp == nil {
data := make([]byte, size)
return &data
}
buf := sp.pool.Get().(*[]byte)
*buf = (*buf)[:size]
return buf
}
func (p *LimitedPool) Put(b *[]byte) {
sp := p.findPutPool(cap(*b))
if sp == nil {
return
}
*b = (*b)[:cap(*b)]
sp.pool.Put(b)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/simon_git_code/smallnest.git
git@gitee.com:simon_git_code/smallnest.git
simon_git_code
smallnest
smallnest
e483c3e07d35

搜索帮助