mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +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 (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
var bufferPool = sync.Pool{
|
"github.com/Mrs4s/MiraiGo/binary" // 和 MiraiGo 共用同一 buffer 池
|
||||||
New: func() interface{} {
|
)
|
||||||
return new(bytes.Buffer)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewBuffer 从池中获取新 bytes.Buffer
|
// NewBuffer 从池中获取新 bytes.Buffer
|
||||||
func NewBuffer() *bytes.Buffer {
|
func NewBuffer() *bytes.Buffer {
|
||||||
return bufferPool.Get().(*bytes.Buffer)
|
return (*bytes.Buffer)(binary.SelectWriter())
|
||||||
}
|
}
|
||||||
|
|
||||||
// PutBuffer 将 Buffer放入池中
|
// PutBuffer 将 Buffer放入池中
|
||||||
func PutBuffer(buf *bytes.Buffer) {
|
func PutBuffer(buf *bytes.Buffer) {
|
||||||
// See https://golang.org/issue/23199
|
binary.PutWriter((*binary.Writer)(buf))
|
||||||
const maxSize = 1 << 16
|
|
||||||
if buf != nil && buf.Cap() < maxSize { // 对于大Buffer直接丢弃
|
|
||||||
buf.Reset()
|
|
||||||
bufferPool.Put(buf)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user