mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-18 03:35:10 +08:00
优化字节缓冲区相关代码
This commit is contained in:
@@ -9,6 +9,10 @@ var BytePool4k = NewBytePool(4 << 10)
|
||||
var BytePool16k = NewBytePool(16 << 10)
|
||||
var BytePool32k = NewBytePool(32 << 10)
|
||||
|
||||
type BytesBuf struct {
|
||||
Bytes []byte
|
||||
}
|
||||
|
||||
// BytePool pool for get byte slice
|
||||
type BytePool struct {
|
||||
length int
|
||||
@@ -24,20 +28,22 @@ func NewBytePool(length int) *BytePool {
|
||||
length: length,
|
||||
rawPool: &sync.Pool{
|
||||
New: func() any {
|
||||
return make([]byte, length)
|
||||
return &BytesBuf{
|
||||
Bytes: make([]byte, length),
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Get 获取一个新的byte slice
|
||||
func (this *BytePool) Get() []byte {
|
||||
return this.rawPool.Get().([]byte)
|
||||
func (this *BytePool) Get() *BytesBuf {
|
||||
return this.rawPool.Get().(*BytesBuf)
|
||||
}
|
||||
|
||||
// Put 放回一个使用过的byte slice
|
||||
func (this *BytePool) Put(b []byte) {
|
||||
this.rawPool.Put(b)
|
||||
func (this *BytePool) Put(ptr *BytesBuf) {
|
||||
this.rawPool.Put(ptr)
|
||||
}
|
||||
|
||||
// Length 单个字节slice长度
|
||||
|
||||
Reference in New Issue
Block a user