2020-08-13 23:12:22 +03:00
|
|
|
package leveledbytebufferpool
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"math/bits"
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// pools contains pools for byte slices of various capacities.
|
|
|
|
|
//
|
2025-04-01 12:01:18 +02:00
|
|
|
// pools[0] is for capacities from 0 to 256
|
|
|
|
|
// pools[1] is for capacities from 257 to 512
|
|
|
|
|
// pools[2] is for capacities from 513 to 1024
|
2022-07-11 19:21:59 +03:00
|
|
|
// ...
|
2025-04-01 12:01:18 +02:00
|
|
|
// pools[n] is for capacities from 2^(n+7)+1 to 2^(n+8)
|
2020-08-13 23:12:22 +03:00
|
|
|
//
|
2021-03-14 22:56:23 +02:00
|
|
|
// Limit the maximum capacity to 2^18, since there are no performance benefits
|
|
|
|
|
// in caching byte slices with bigger capacities.
|
2025-04-01 12:01:18 +02:00
|
|
|
var pools [10]sync.Pool
|
2020-08-13 23:12:22 +03:00
|
|
|
|
2025-04-01 12:01:18 +02:00
|
|
|
// Get returns byte buffer, which is able to store at least dataLen bytes.
|
|
|
|
|
func Get(dataLen int) *bytesutil.ByteBuffer {
|
|
|
|
|
id, capacityNeeded := getPoolIDAndCapacity(dataLen)
|
2020-08-13 23:12:22 +03:00
|
|
|
for i := 0; i < 2; i++ {
|
2020-08-16 22:12:19 +03:00
|
|
|
if id < 0 || id >= len(pools) {
|
2020-08-13 23:12:22 +03:00
|
|
|
break
|
|
|
|
|
}
|
2020-08-16 22:12:19 +03:00
|
|
|
if v := pools[id].Get(); v != nil {
|
|
|
|
|
return v.(*bytesutil.ByteBuffer)
|
|
|
|
|
}
|
|
|
|
|
id++
|
2020-08-13 23:12:22 +03:00
|
|
|
}
|
2025-04-01 12:01:18 +02:00
|
|
|
|
2020-08-15 01:40:51 +03:00
|
|
|
return &bytesutil.ByteBuffer{
|
2020-08-16 22:12:19 +03:00
|
|
|
B: make([]byte, 0, capacityNeeded),
|
2020-08-15 01:40:51 +03:00
|
|
|
}
|
2020-08-13 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Put returns bb to the pool.
|
|
|
|
|
func Put(bb *bytesutil.ByteBuffer) {
|
|
|
|
|
capacity := cap(bb.B)
|
2021-03-14 22:56:23 +02:00
|
|
|
id, poolCapacity := getPoolIDAndCapacity(capacity)
|
|
|
|
|
if capacity <= poolCapacity {
|
|
|
|
|
bb.Reset()
|
|
|
|
|
pools[id].Put(bb)
|
|
|
|
|
}
|
2020-08-13 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
2020-08-28 09:44:08 +03:00
|
|
|
func getPoolIDAndCapacity(size int) (int, int) {
|
2020-08-16 22:12:19 +03:00
|
|
|
size--
|
2020-08-13 23:12:22 +03:00
|
|
|
if size < 0 {
|
|
|
|
|
size = 0
|
|
|
|
|
}
|
2025-04-01 12:01:18 +02:00
|
|
|
size >>= 8
|
2020-08-16 22:12:19 +03:00
|
|
|
id := bits.Len(uint(size))
|
2021-03-14 22:56:23 +02:00
|
|
|
if id >= len(pools) {
|
2020-08-16 22:12:19 +03:00
|
|
|
id = len(pools) - 1
|
2020-08-13 23:12:22 +03:00
|
|
|
}
|
2025-04-01 12:01:18 +02:00
|
|
|
return id, (1 << (id + 8))
|
2020-08-13 23:12:22 +03:00
|
|
|
}
|