-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
mem.go
209 lines (179 loc) · 4.19 KB
/
mem.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package mem
import (
"math/bits"
"honnef.co/go/gotraceui/mysync"
"gioui.org/op"
)
const allocatorBucketSize = 64
// BucketSlice is like a slice, but grows one bucket at a time, instead of growing exponentially. This allows
// for overall lower memory usage when the total capacity isn't known ahead of time, at the cost of more
// overall allocations.
type BucketSlice[T any] struct {
n int
buckets [][]T
}
// Grow grows the slice by one and returns a pointer to the new element, without overwriting it.
func (l *BucketSlice[T]) Grow() *T {
a, _ := l.index(l.n)
if a >= len(l.buckets) {
l.buckets = append(l.buckets, make([]T, 0, allocatorBucketSize))
}
l.buckets[a] = l.buckets[a][:len(l.buckets[a])+1]
ptr := &l.buckets[a][len(l.buckets[a])-1]
l.n++
return ptr
}
// GrowN grows the slice by n elements.
func (l *BucketSlice[T]) GrowN(n int) {
for i := 0; i < n; i++ {
l.Grow()
}
}
// Append appends v to the slice and returns a pointer to the new element.
func (l *BucketSlice[T]) Append(v T) *T {
ptr := l.Grow()
*ptr = v
return ptr
}
func (l *BucketSlice[T]) index(i int) (int, int) {
return i / allocatorBucketSize, i % allocatorBucketSize
}
func (l *BucketSlice[T]) Ptr(i int) *T {
a, b := l.index(i)
return &l.buckets[a][b]
}
func (l *BucketSlice[T]) Get(i int) T {
a, b := l.index(i)
return l.buckets[a][b]
}
func (l *BucketSlice[T]) Set(i int, v T) {
a, b := l.index(i)
l.buckets[a][b] = v
}
func (l *BucketSlice[T]) Len() int {
return l.n
}
func (l *BucketSlice[T]) Reset() {
for i := range l.buckets {
l.buckets[i] = l.buckets[i][:0]
}
l.n = 0
}
func (l *BucketSlice[T]) Truncate(n int) {
if n >= l.n {
return
}
a, b := l.index(n)
l.buckets[a] = l.buckets[a][:b]
for i := a + 1; i < len(l.buckets); i++ {
l.buckets[i] = l.buckets[i][:0]
}
l.n = n
}
type ReusableOps struct {
ops op.Ops
}
// get resets and returns an op.Ops
func (rops *ReusableOps) Get() *op.Ops {
rops.ops.Reset()
return &rops.ops
}
// Mapping from log2 to size that is to be used
//
// TODO(dh): remove some of the steps
var poolSizes = [...]int{
0: 1,
1: 2,
2: 4,
3: 8,
4: 16,
5: 32,
6: 64,
7: 128,
8: 256,
9: 512,
10: 1024,
11: 2048,
12: 4096,
13: 8192,
14: 16384,
15: 32768,
16: 65536,
17: 131072,
18: 262144,
19: 524288,
20: 1048576,
21: 2097152,
22: 4194304,
23: 8388608,
24: 16777216,
25: 33554432,
26: 67108864,
}
// ConcurrentSliceCache caches slices, grouped by capacities. When requesting a slice, the requested capacity
// is rounded up to the next group and then fetched from a sync.Pool dedicated to that group.
type ConcurrentSliceCache[E any, T ~[]E] struct {
pools [len(poolSizes)]*mysync.Pool[T]
}
func NewConcurrentSliceCache[E any, T ~[]E]() *ConcurrentSliceCache[E, T] {
var pools [len(poolSizes)]*mysync.Pool[T]
for i, v := range poolSizes {
i := i
v := v
if i == 0 {
pools[i] = mysync.NewPool(func() T { return make(T, 0, 1) })
continue
}
if v == poolSizes[i-1] {
pools[i] = pools[i-1]
} else {
pools[i] = mysync.NewPool(func() T { return make(T, 0, v) })
}
}
return &ConcurrentSliceCache[E, T]{
pools: pools,
}
}
func (c *ConcurrentSliceCache[E, T]) Put(s T) {
log2 := 64 - bits.LeadingZeros64(uint64(cap(s))-1)
if log2 >= len(poolSizes) {
return
}
c.pools[log2].Put(s[:0])
}
func (c *ConcurrentSliceCache[E, T]) Get(minCap int) T {
if minCap < 0 {
panic("invalid capacity")
}
if minCap == 0 {
return T{}
}
// log₂ of next power of two
log2 := 64 - bits.LeadingZeros64(uint64(minCap)-1)
if log2 >= len(poolSizes) {
// This is larger than any slices we cache
return make(T, 0, minCap)
}
return c.pools[log2].Get()[:0]
}
// AllocationCache is a trivial cache of allocations. Put appends a value to a slice and Get pops a value from the
// slice, or allocates a new value.
type AllocationCache[T any] struct {
items []*T
}
func (c *AllocationCache[T]) Put(x *T) {
c.items = append(c.items, x)
}
func (c *AllocationCache[T]) Get() *T {
if len(c.items) == 0 {
return new(T)
} else {
item := c.items[len(c.items)-1]
c.items = c.items[:len(c.items)-1]
return item
}
}
// GrowLen increases the slice's length by n elements.
func GrowLen[S ~[]E, E any](s S, n int) S {
return append(s, make([]E, n)...)
}