mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 11:07:39 +08:00
feat: use the same buffer pool as MiraiGo (#1297)
This commit is contained in:
parent
15602e1daa
commit
bdf68ec694
@ -2,26 +2,16 @@ package global
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var bufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return new(bytes.Buffer)
|
||||
},
|
||||
}
|
||||
"github.com/Mrs4s/MiraiGo/binary" // 和 MiraiGo 共用同一 buffer 池
|
||||
)
|
||||
|
||||
// NewBuffer 从池中获取新 bytes.Buffer
|
||||
func NewBuffer() *bytes.Buffer {
|
||||
return bufferPool.Get().(*bytes.Buffer)
|
||||
return (*bytes.Buffer)(binary.SelectWriter())
|
||||
}
|
||||
|
||||
// PutBuffer 将 Buffer放入池中
|
||||
func PutBuffer(buf *bytes.Buffer) {
|
||||
// See https://golang.org/issue/23199
|
||||
const maxSize = 1 << 16
|
||||
if buf != nil && buf.Cap() < maxSize { // 对于大Buffer直接丢弃
|
||||
buf.Reset()
|
||||
bufferPool.Put(buf)
|
||||
}
|
||||
binary.PutWriter((*binary.Writer)(buf))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user