[#3] signature: Add buffer pool

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-03-09 11:33:21 +03:00
parent 73fde0e37c
commit ec0d0274fa
5 changed files with 67 additions and 58 deletions

29
util/signature/buffer.go Normal file
View file

@ -0,0 +1,29 @@
package signature
import "sync"
const poolSliceMaxSize = 64 * 1024
var buffersPool = sync.Pool{
New: func() any {
return make([]byte, 0)
},
}
func newBufferFromPool(size int) []byte {
result := buffersPool.Get().([]byte)
if cap(result) < size {
result = make([]byte, size)
} else {
result = result[:size]
}
return result
}
func returnBufferToPool(buf []byte) {
if cap(buf) > poolSliceMaxSize {
return
}
buf = buf[:0]
buffersPool.Put(buf)
}