Skip to content

Commit b77925d

Browse files
committed
fix: 恢复之前修改的 buffer 代码
1 parent de34474 commit b77925d

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

global/buffer.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@ package global
22

33
import (
44
"bytes"
5-
"sync"
5+
6+
"github.com/LagrangeDev/LagrangeGo/utils/binary"
7+
"github.com/RomiChan/syncx"
68
)
79

8-
var bufferPool = sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
10+
var bufferTable syncx.Map[*bytes.Buffer, *binary.Builder]
911

1012
// NewBuffer 从池中获取新 bytes.Buffer
1113
func NewBuffer() *bytes.Buffer {
12-
return bufferPool.Get().(*bytes.Buffer)
14+
builder := binary.NewBuilder()
15+
bufferTable.Store(builder.Buffer(), builder)
16+
return builder.Buffer()
1317
}
1418

1519
// PutBuffer 将 Buffer放入池中
1620
func PutBuffer(buf *bytes.Buffer) {
17-
buf.Reset()
18-
bufferPool.Put(buf)
21+
if v, ok := bufferTable.LoadAndDelete(buf); ok {
22+
binary.PutBuilder(v)
23+
}
1924
}

0 commit comments

Comments
 (0)