lib/leveledbytebufferpool: pre-allocate byte slice with the given capacity if the pool is empty

This should reduce memory allocations and copying when the byte slice is growing.
This commit is contained in:
Aliaksandr Valialkin 2020-08-15 01:40:51 +03:00
parent 285665e93b
commit 6e863376f7

View file

@ -18,6 +18,9 @@ var pools [30]sync.Pool
// Get returns byte buffer with the given capacity. // Get returns byte buffer with the given capacity.
func Get(capacity int) *bytesutil.ByteBuffer { func Get(capacity int) *bytesutil.ByteBuffer {
if capacity <= 0 {
capacity = 1
}
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
v := getPool(capacity).Get() v := getPool(capacity).Get()
if v != nil { if v != nil {
@ -28,7 +31,9 @@ func Get(capacity int) *bytesutil.ByteBuffer {
} }
capacity *= 2 capacity *= 2
} }
return &bytesutil.ByteBuffer{} return &bytesutil.ByteBuffer{
B: make([]byte, 0, capacity),
}
} }
// Put returns bb to the pool. // Put returns bb to the pool.