-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraypool.go
More file actions
54 lines (44 loc) · 1.09 KB
/
arraypool.go
File metadata and controls
54 lines (44 loc) · 1.09 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
package arraypool
import "sync"
var defaultBufferCapacity int = 128
type ArrayBuffer[T any] struct {
Buffer []T
}
type ArrayPool[T any] struct {
pool sync.Pool
}
// create a new instance of pool
func NewPool[T any]() *ArrayPool[T] {
ap := &ArrayPool[T]{}
ap.pool = sync.Pool{
New: func() any {
return &ArrayBuffer[T]{
Buffer: make([]T, 0, defaultBufferCapacity),
}
},
}
return ap
}
// get an existing instance from the pool. If it does not exist sync.pool creates one
func (ap *ArrayPool[T]) Get() *ArrayBuffer[T] {
return ap.pool.Get().(*ArrayBuffer[T])
}
// put the buffer back in the pool after use, an additional flag to resize the buffer is required
func (ap *ArrayPool[T]) Put(ab *ArrayBuffer[T], resize bool) {
if resize {
ab.Resize()
}
ap.pool.Put(ab)
}
// write to the Buffer
func (ab *ArrayBuffer[T]) Write(val T) {
ab.Buffer = append(ab.Buffer, val)
}
// clear the values of the buffer
func (ab *ArrayBuffer[T]) ClearAll() {
clear(ab.Buffer)
}
// resize the buffer back to zero, maintains the capacity
func (ab *ArrayBuffer[T]) Resize() {
ab.Buffer = ab.Buffer[:0]
}