mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
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:
parent
285665e93b
commit
6e863376f7
1 changed files with 6 additions and 1 deletions
|
@ -18,6 +18,9 @@ var pools [30]sync.Pool
|
|||
|
||||
// Get returns byte buffer with the given capacity.
|
||||
func Get(capacity int) *bytesutil.ByteBuffer {
|
||||
if capacity <= 0 {
|
||||
capacity = 1
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
v := getPool(capacity).Get()
|
||||
if v != nil {
|
||||
|
@ -28,7 +31,9 @@ func Get(capacity int) *bytesutil.ByteBuffer {
|
|||
}
|
||||
capacity *= 2
|
||||
}
|
||||
return &bytesutil.ByteBuffer{}
|
||||
return &bytesutil.ByteBuffer{
|
||||
B: make([]byte, 0, capacity),
|
||||
}
|
||||
}
|
||||
|
||||
// Put returns bb to the pool.
|
||||
|
|
Loading…
Reference in a new issue