Skip to content

Commit 1dba7f2

Browse files
Fix possible bugs in BytesSlicePool.
1 parent 1442d5c commit 1dba7f2

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

custom_float16.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ type Float16 float64
3131
//
3232
// float16SlicePool provides a thread-safe buffer pool for Float16 operations
3333
// Uses BytesSlicePool to manage shared byte slices, reducing memory allocations
34-
var float16SlicePool = &BytesSlicePool{
35-
bytes: make([]byte, 4096),
36-
offset: 0,
37-
}
34+
var float16SlicePool = NewBytesSlicePool(0)
3835

3936
// Pack 将 Float16 值序列化为16位二进制格式
4037
// 二进制格式遵循 IEEE 754-2008 binary16 规范

fields.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ import (
1515
//
1616
// unpackBasicTypeSlicePool is a globally shared byte block instance
1717
// Used for sharing byte slices within the unpackBasicType method to reduce memory allocations
18-
var unpackBasicTypeSlicePool = &BytesSlicePool{
19-
bytes: make([]byte, 4096),
20-
offset: 0,
21-
}
18+
var unpackBasicTypeSlicePool = NewBytesSlicePool(0)
2219

2320
// Fields 是字段切片类型,用于管理结构体的字段集合
2421
// 它提供了字段的序列化、反序列化和大小计算等功能

pool.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ import (
77
"sync"
88
)
99

10-
// MaxCapSize 定义了缓冲区的最大容量限制
10+
// MaxBufferCapSize 定义了缓冲区的最大容量限制
1111
// 超过此限制的缓冲区不会被放入对象池
1212
//
13-
// MaxCapSize defines the maximum capacity limit for buffers
13+
// MaxBufferCapSize defines the maximum capacity limit for buffers
1414
// Buffers exceeding this limit will not be put into the object pool
15-
const MaxCapSize = 1 << 20
15+
const MaxBufferCapSize = 1 << 20
16+
17+
// MaxBytesSliceSize 定义了字节切片的最大容量限制
18+
// 超过此限制的字节切片不会被放入对象池
19+
//
20+
// MaxBytesSliceSize defines the maximum capacity limit for byte slices
21+
// Byte slices exceeding this limit will not be put into the object pool
22+
const MaxBytesSliceSize = 4096
1623

1724
// bufferPool 用于减少打包/解包时的内存分配
1825
// bufferPool is used to reduce allocations when packing/unpacking
@@ -70,7 +77,7 @@ func acquireBuffer() *bytes.Buffer {
7077
// releaseBuffer 将缓冲区放回对象池
7178
// releaseBuffer returns a buffer to the pool
7279
func releaseBuffer(buf *bytes.Buffer) {
73-
if buf == nil || buf.Cap() > MaxCapSize {
80+
if buf == nil || buf.Cap() > MaxBufferCapSize {
7481
return
7582
}
7683

@@ -131,6 +138,25 @@ type BytesSlicePool struct {
131138
mu sync.Mutex // 互斥锁用于保护并发访问 / mutex for protecting concurrent access
132139
}
133140

141+
// NewBytesSlicePool 创建一个新的 BytesSlicePool 实例
142+
// 初始化时,会分配一个 4096 字节的字节数组
143+
//
144+
// NewBytesSlicePool creates a new BytesSlicePool instance
145+
// Initializes with a 4096-byte byte array
146+
func NewBytesSlicePool(size int) *BytesSlicePool {
147+
// 如果 size 小于等于 0 或者大于 MaxBytesSliceSize,则使用 MaxBytesSliceSize
148+
// If size is less than or equal to 0 or greater than MaxBytesSliceSize, use MaxBytesSliceSize
149+
if size > MaxBytesSliceSize || size <= 0 {
150+
size = MaxBytesSliceSize
151+
}
152+
153+
return &BytesSlicePool{
154+
bytes: make([]byte, size),
155+
offset: 0,
156+
mu: sync.Mutex{},
157+
}
158+
}
159+
134160
// GetSlice 返回指定大小的字节切片
135161
// 如果当前块空间不足,会分配新的块并重置偏移量
136162
//
@@ -139,12 +165,19 @@ type BytesSlicePool struct {
139165
func (b *BytesSlicePool) GetSlice(size int) []byte {
140166
b.mu.Lock()
141167

168+
// 如果请求的大小超过了最大限制,直接分配新的切片
169+
// If the requested size exceeds the maximum limit, allocate a new slice directly
170+
if size > MaxBytesSliceSize {
171+
b.mu.Unlock()
172+
return make([]byte, size)
173+
}
174+
142175
// 检查剩余空间是否足够
143176
// Check if remaining space is sufficient
144177
if int(b.offset)+size > len(b.bytes) {
145178
// 分配新的固定大小块(4096字节)并重置偏移量
146179
// Allocate new fixed-size block (4096 bytes) and reset offset
147-
b.bytes = make([]byte, 4096)
180+
b.bytes = make([]byte, MaxBytesSliceSize)
148181
b.offset = 0
149182
}
150183

0 commit comments

Comments
 (0)