2024-08-30 09:09:14 +00:00
|
|
|
package target
|
2022-12-06 10:25:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2023-06-02 14:33:51 +00:00
|
|
|
const (
|
|
|
|
defaultAllocSize = 1024
|
|
|
|
poolSliceMaxSize = 128 * 1024
|
|
|
|
)
|
2022-12-06 10:25:19 +00:00
|
|
|
|
2023-04-03 10:09:44 +00:00
|
|
|
type payload struct {
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2022-12-06 10:25:19 +00:00
|
|
|
var putBytesPool = &sync.Pool{
|
2023-04-03 10:09:44 +00:00
|
|
|
New: func() any { return &payload{Data: make([]byte, 0, defaultAllocSize)} },
|
2022-12-06 10:25:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 10:09:44 +00:00
|
|
|
func getPayload() *payload {
|
|
|
|
return putBytesPool.Get().(*payload)
|
2022-12-06 10:25:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 10:09:44 +00:00
|
|
|
func putPayload(p *payload) {
|
2023-06-02 14:33:51 +00:00
|
|
|
if cap(p.Data) > poolSliceMaxSize {
|
|
|
|
return
|
|
|
|
}
|
2023-04-03 10:09:44 +00:00
|
|
|
p.Data = p.Data[:0]
|
|
|
|
putBytesPool.Put(p)
|
2022-12-06 10:25:19 +00:00
|
|
|
}
|