2020-02-18 00:25:39 +00:00
|
|
|
package memorypool
|
|
|
|
|
2020-02-19 06:29:49 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
|
|
|
)
|
2020-02-18 00:25:39 +00:00
|
|
|
|
|
|
|
// DoubleByteSlicePool represents the memory pool
|
|
|
|
// for 2d byte slices.
|
|
|
|
var DoubleByteSlicePool = new(sync.Pool)
|
|
|
|
|
|
|
|
// GetDoubleByteSlice retrieves the 2d byte slice of
|
|
|
|
// the desired size from the memory pool.
|
|
|
|
func GetDoubleByteSlice(size int) [][]byte {
|
2020-02-19 06:29:49 +00:00
|
|
|
if !featureconfig.Get().EnableByteMempool {
|
|
|
|
return make([][]byte, size)
|
|
|
|
}
|
|
|
|
|
2020-02-18 00:25:39 +00:00
|
|
|
rawObj := DoubleByteSlicePool.Get()
|
|
|
|
if rawObj == nil {
|
|
|
|
return make([][]byte, size)
|
|
|
|
}
|
|
|
|
byteSlice := rawObj.([][]byte)
|
|
|
|
if len(byteSlice) >= size {
|
|
|
|
return byteSlice[:size]
|
|
|
|
}
|
|
|
|
return append(byteSlice, make([][]byte, size-len(byteSlice))...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutDoubleByteSlice places the provided 2d byte slice
|
|
|
|
// in the memory pool.
|
|
|
|
func PutDoubleByteSlice(data [][]byte) {
|
2020-02-19 06:29:49 +00:00
|
|
|
if featureconfig.Get().EnableByteMempool {
|
|
|
|
DoubleByteSlicePool.Put(data)
|
|
|
|
}
|
2020-02-18 00:25:39 +00:00
|
|
|
}
|