2023-03-09 08:33:21 +00:00
|
|
|
package signature
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
2023-06-02 14:25:15 +00:00
|
|
|
const poolSliceMaxSize = 128 * 1024
|
2023-03-09 08:33:21 +00:00
|
|
|
|
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-10-06 12:05:30 +00:00
|
|
|
func tryGetNewBufferFromPool(size int) (*buffer, bool) {
|
|
|
|
if size > poolSliceMaxSize {
|
|
|
|
return &buffer{}, false
|
|
|
|
}
|
|
|
|
return newBufferFromPool(size), true
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|