-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.go
More file actions
74 lines (65 loc) · 1.51 KB
/
pool.go
File metadata and controls
74 lines (65 loc) · 1.51 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package narwhal
import (
"sync"
)
// ByteSlicePool provides a pool of reusable byte slices.
type ByteSlicePool struct {
pool sync.Pool
size int
}
// NewByteSlicePool creates a new byte slice pool with the specified default size.
func NewByteSlicePool(defaultSize int) *ByteSlicePool {
return &ByteSlicePool{
size: defaultSize,
pool: sync.Pool{
New: func() interface{} {
b := make([]byte, 0, defaultSize)
return &b
},
},
}
}
// Get retrieves a byte slice from the pool.
// The slice is reset to zero length but retains its capacity.
func (p *ByteSlicePool) Get() *[]byte {
b := p.pool.Get().(*[]byte)
*b = (*b)[:0]
return b
}
// Put returns a byte slice to the pool.
// The slice should not be used after calling Put.
func (p *ByteSlicePool) Put(b *[]byte) {
if cap(*b) >= p.size {
p.pool.Put(b)
}
}
// SlicePool provides a pool of reusable slices.
type SlicePool[T any] struct {
pool sync.Pool
initCap int
}
// NewSlicePool creates a new slice pool with the specified initial capacity.
func NewSlicePool[T any](initCap int) *SlicePool[T] {
return &SlicePool[T]{
initCap: initCap,
pool: sync.Pool{
New: func() interface{} {
s := make([]T, 0, initCap)
return &s
},
},
}
}
// Get retrieves a slice from the pool.
// The slice is reset to zero length but retains its capacity.
func (p *SlicePool[T]) Get() *[]T {
s := p.pool.Get().(*[]T)
*s = (*s)[:0]
return s
}
// Put returns a slice to the pool.
func (p *SlicePool[T]) Put(s *[]T) {
if cap(*s) >= p.initCap {
p.pool.Put(s)
}
}