@@ -54,7 +54,7 @@ func NewSharedBufferPool() SharedBufferPool {
54
54
newBytesPool (level2PoolMaxSize ),
55
55
newBytesPool (level3PoolMaxSize ),
56
56
newBytesPool (level4PoolMaxSize ),
57
- newFallbackBytesPool ( ),
57
+ newBytesPool ( 0 ),
58
58
},
59
59
}
60
60
}
@@ -114,46 +114,33 @@ type simpleSharedBufferChildPool interface {
114
114
115
115
type bufferPool struct {
116
116
sync.Pool
117
+
118
+ defaultSize int
117
119
}
118
120
119
121
func (p * bufferPool ) Get (size int ) []byte {
120
122
bs := p .Pool .Get ().(* []byte )
121
123
122
- return (* bs )[:size ]
123
- }
124
-
125
- func newBytesPool (size int ) simpleSharedBufferChildPool {
126
- return & bufferPool {
127
- sync.Pool {
128
- New : func () interface {} {
129
- bs := make ([]byte , size )
130
- return & bs
131
- },
132
- },
133
- }
134
- }
135
-
136
- type fallbackBufferPool struct {
137
- sync.Pool
138
- }
139
-
140
- func (p * fallbackBufferPool ) Get (size int ) []byte {
141
- bs := p .Pool .Get ().(* []byte )
124
+ // If default size is 0. It means this pool is fallback pool.
125
+ // Therefore, we need to make a new one if the requested size is larger than the buffer.
126
+ if p .defaultSize == 0 {
142
127
if cap (* bs ) < size {
143
128
* bs = make ([]byte , size )
144
- return * bs
129
+ }
145
130
}
146
131
147
132
return (* bs )[:size ]
148
133
}
149
134
150
- func newFallbackBytesPool ( ) simpleSharedBufferChildPool {
151
- return & fallbackBufferPool {
152
- sync.Pool {
135
+ func newBytesPool ( size int ) simpleSharedBufferChildPool {
136
+ return & bufferPool {
137
+ Pool : sync.Pool {
153
138
New : func () interface {} {
154
- return new ([]byte )
139
+ bs := make ([]byte , size )
140
+ return & bs
155
141
},
156
142
},
143
+ defaultSize : size ,
157
144
}
158
145
}
159
146
0 commit comments