2023-03-09 08:33:21 +00:00
|
|
|
package signature
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
const poolSliceMaxSize = 64 * 1024
|
|
|
|
|
2023-05-03 11:09:17 +00:00
|
|
|
type buffer struct {
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
2023-03-09 08:33:21 +00:00
|
|
|
var buffersPool = sync.Pool{
|
|
|
|
New: func() any {
|
2023-05-03 11:09:17 +00:00
|
|
|
return new(buffer)
|
2023-03-09 08:33:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-03 11:09:17 +00:00
|
|
|
func newBufferFromPool(size int) *buffer {
|
|
|
|
result := buffersPool.Get().(*buffer)
|
|
|
|
if cap(result.data) < size {
|
|
|
|
result.data = make([]byte, size)
|
2023-03-09 08:33:21 +00:00
|
|
|
} else {
|
2023-05-03 11:09:17 +00:00
|
|
|
result.data = result.data[:size]
|
2023-03-09 08:33:21 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-05-03 11:09:17 +00:00
|
|
|
func returnBufferToPool(buf *buffer) {
|
|
|
|
if cap(buf.data) > poolSliceMaxSize {
|
2023-03-09 08:33:21 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-03 11:09:17 +00:00
|
|
|
buf.data = buf.data[:0]
|
2023-03-09 08:33:21 +00:00
|
|
|
buffersPool.Put(buf)
|
|
|
|
}
|