Skip to content

Commit fe1294f

Browse files
committed
rpc_util: merge fallback bytespool functionality into bytepool
1 parent 96f5a27 commit fe1294f

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

shared_buffer_pool.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func NewSharedBufferPool() SharedBufferPool {
5454
newBytesPool(level2PoolMaxSize),
5555
newBytesPool(level3PoolMaxSize),
5656
newBytesPool(level4PoolMaxSize),
57-
newFallbackBytesPool(),
57+
newBytesPool(0),
5858
},
5959
}
6060
}
@@ -114,46 +114,33 @@ type simpleSharedBufferChildPool interface {
114114

115115
type bufferPool struct {
116116
sync.Pool
117+
118+
defaultSize int
117119
}
118120

119121
func (p *bufferPool) Get(size int) []byte {
120122
bs := p.Pool.Get().(*[]byte)
121123

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 {
142127
if cap(*bs) < size {
143128
*bs = make([]byte, size)
144-
return *bs
129+
}
145130
}
146131

147132
return (*bs)[:size]
148133
}
149134

150-
func newFallbackBytesPool() simpleSharedBufferChildPool {
151-
return &fallbackBufferPool{
152-
sync.Pool{
135+
func newBytesPool(size int) simpleSharedBufferChildPool {
136+
return &bufferPool{
137+
Pool: sync.Pool{
153138
New: func() interface{} {
154-
return new([]byte)
139+
bs := make([]byte, size)
140+
return &bs
155141
},
156142
},
143+
defaultSize: size,
157144
}
158145
}
159146

0 commit comments

Comments
 (0)