Skip to content

Commit

Permalink
feat: use the same buffer pool as MiraiGo (#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama authored May 30, 2022
1 parent 15602e1 commit bdf68ec
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions global/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit bdf68ec

Please sign in to comment.