-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferpool.go
More file actions
36 lines (30 loc) · 807 Bytes
/
Copy pathbufferpool.go
File metadata and controls
36 lines (30 loc) · 807 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"sync"
)
var (
payloadPool = sync.Pool{
New: func() interface{} {
// Pre-allocate with 10MB capacity for typical block size
return make([]byte, 0, 10*1024*1024)
},
}
)
// GetPayload copies data into a pooled buffer and returns it.
// The returned buffer is owned by the caller until PutPayload is called.
func GetPayload(data []byte) []byte {
buf := payloadPool.Get().([]byte)
// If capacity is insufficient, allocate new buffer
if cap(buf) < len(data) {
buf = make([]byte, len(data))
} else {
buf = buf[:len(data)]
}
copy(buf, data)
return buf
}
// PutPayload returns a buffer to the pool for reuse.
// The buffer length is reset to 0 but capacity is preserved.
func PutPayload(buf []byte) {
payloadPool.Put(buf[:0]) // Reset length but keep capacity
}