72 lines
1.2 KiB
Go
72 lines
1.2 KiB
Go
package fin
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// byteSlicePool helps reuse byte slices
|
|
var byteSlicePool = sync.Pool{
|
|
New: func() any {
|
|
b := make([]byte, 0, 64)
|
|
return &b
|
|
},
|
|
}
|
|
|
|
// GetByteSlice gets a byte slice from the pool
|
|
func GetByteSlice() *[]byte {
|
|
return byteSlicePool.Get().(*[]byte)
|
|
}
|
|
|
|
// PutByteSlice returns a byte slice to the pool
|
|
func PutByteSlice(b *[]byte) {
|
|
if b != nil {
|
|
*b = (*b)[:0] // Clear but keep capacity
|
|
byteSlicePool.Put(b)
|
|
}
|
|
}
|
|
|
|
// mapPool helps reuse maps for config objects
|
|
var mapPool = sync.Pool{
|
|
New: func() any {
|
|
m := make(map[string]any, 8)
|
|
return &m
|
|
},
|
|
}
|
|
|
|
// GetMap gets a map from the pool
|
|
func GetMap() *map[string]any {
|
|
return mapPool.Get().(*map[string]any)
|
|
}
|
|
|
|
// PutMap returns a map to the pool after clearing it
|
|
func PutMap(m *map[string]any) {
|
|
if m != nil {
|
|
// Clear the map
|
|
for k := range *m {
|
|
delete(*m, k)
|
|
}
|
|
mapPool.Put(m)
|
|
}
|
|
}
|
|
|
|
// arrayPool helps reuse slices
|
|
var arrayPool = sync.Pool{
|
|
New: func() any {
|
|
a := make([]any, 0, 4)
|
|
return &a
|
|
},
|
|
}
|
|
|
|
// GetArray gets a slice from the pool
|
|
func GetArray() *[]any {
|
|
return arrayPool.Get().(*[]any)
|
|
}
|
|
|
|
// PutArray returns a slice to the pool
|
|
func PutArray(a *[]any) {
|
|
if a != nil {
|
|
*a = (*a)[:0] // Clear but keep capacity
|
|
arrayPool.Put(a)
|
|
}
|
|
}
|