mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2024-11-27 12:05:23 +00:00
19 lines
555 B
Go
19 lines
555 B
Go
//+build !mobile
|
|
|
|
package util
|
|
|
|
import "sync"
|
|
|
|
// This is used to buffer recently used slices of bytes, to prevent allocations in the hot loops.
|
|
var byteStore = sync.Pool{New: func() interface{} { return []byte(nil) }}
|
|
|
|
// GetBytes returns a 0-length (possibly nil) slice of bytes from a free list, so it may have a larger capacity.
|
|
func GetBytes() []byte {
|
|
return byteStore.Get().([]byte)[:0]
|
|
}
|
|
|
|
// PutBytes stores a slice in a free list, where it can potentially be reused to prevent future allocations.
|
|
func PutBytes(bs []byte) {
|
|
byteStore.Put(bs)
|
|
}
|