2023-06-20 05:55:12 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
2024-05-12 14:33:29 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2024-05-20 02:08:30 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/slicesutil"
|
2023-06-20 05:55:12 +00:00
|
|
|
)
|
|
|
|
|
2024-05-12 14:33:29 +00:00
|
|
|
func getArena() *arena {
|
|
|
|
v := arenaPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &arena{}
|
|
|
|
}
|
|
|
|
return v.(*arena)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putArena(a *arena) {
|
|
|
|
a.reset()
|
|
|
|
arenaPool.Put(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
var arenaPool sync.Pool
|
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
type arena struct {
|
|
|
|
b []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *arena) reset() {
|
|
|
|
a.b = a.b[:0]
|
|
|
|
}
|
|
|
|
|
2024-05-20 02:08:30 +00:00
|
|
|
func (a *arena) preallocate(n int) {
|
|
|
|
a.b = slicesutil.ExtendCapacity(a.b, n)
|
|
|
|
}
|
|
|
|
|
2024-05-12 14:33:29 +00:00
|
|
|
func (a *arena) sizeBytes() int {
|
2024-05-20 02:08:30 +00:00
|
|
|
return cap(a.b)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
func (a *arena) copyBytes(b []byte) []byte {
|
2024-05-12 14:33:29 +00:00
|
|
|
if len(b) == 0 {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
ab := a.b
|
|
|
|
abLen := len(ab)
|
|
|
|
ab = append(ab, b...)
|
|
|
|
a.b = ab
|
2024-05-20 02:08:30 +00:00
|
|
|
return ab[abLen:]
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 14:33:29 +00:00
|
|
|
func (a *arena) copyBytesToString(b []byte) string {
|
|
|
|
bCopy := a.copyBytes(b)
|
|
|
|
return bytesutil.ToUnsafeString(bCopy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *arena) copyString(s string) string {
|
|
|
|
b := bytesutil.ToUnsafeBytes(s)
|
|
|
|
return a.copyBytesToString(b)
|
|
|
|
}
|
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
func (a *arena) newBytes(size int) []byte {
|
2024-05-12 14:33:29 +00:00
|
|
|
if size <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
ab := a.b
|
|
|
|
abLen := len(ab)
|
|
|
|
ab = bytesutil.ResizeWithCopyMayOverallocate(ab, abLen+size)
|
|
|
|
result := ab[abLen:]
|
|
|
|
a.b = ab
|
|
|
|
return result
|
|
|
|
}
|