@@ -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
7279func 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 {
139165func (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