mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
efc730863b
Previously targets with big number of metrics and/or labels could generated too big buffers, which then could be re-used when scraping targets with small number of metrics. This resulted in memory waste. Now big buffers are used only for targets with big number of metrics / labels, while small buffers are used for targets with small number of metrics / labels.
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package leveledbytebufferpool
|
|
|
|
import (
|
|
"math/bits"
|
|
"sync"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
)
|
|
|
|
// pools contains pools for byte slices of various capacities.
|
|
//
|
|
// pools[0] is for capacities from 0 to 8
|
|
// pools[1] is for capacities from 9 to 16
|
|
// pools[2] is for capacities from 17 to 32
|
|
// ...
|
|
// pools[n] is for capacities from 2^(n+2)+1 to 2^(n+3)
|
|
//
|
|
var pools [30]sync.Pool
|
|
|
|
// Get returns byte buffer with the given capacity.
|
|
func Get(capacity int) *bytesutil.ByteBuffer {
|
|
id, capacityNeeded := getPoolIdAndCapacity(capacity)
|
|
for i := 0; i < 2; i++ {
|
|
if id < 0 || id >= len(pools) {
|
|
break
|
|
}
|
|
if v := pools[id].Get(); v != nil {
|
|
return v.(*bytesutil.ByteBuffer)
|
|
}
|
|
id++
|
|
}
|
|
return &bytesutil.ByteBuffer{
|
|
B: make([]byte, 0, capacityNeeded),
|
|
}
|
|
}
|
|
|
|
// Put returns bb to the pool.
|
|
func Put(bb *bytesutil.ByteBuffer) {
|
|
capacity := cap(bb.B)
|
|
id, _ := getPoolIdAndCapacity(capacity)
|
|
bb.Reset()
|
|
pools[id].Put(bb)
|
|
}
|
|
|
|
func getPoolIdAndCapacity(size int) (int, int) {
|
|
size--
|
|
if size < 0 {
|
|
size = 0
|
|
}
|
|
size >>= 3
|
|
id := bits.Len(uint(size))
|
|
if id > len(pools) {
|
|
id = len(pools) - 1
|
|
}
|
|
return id, (1 << (id + 3))
|
|
}
|