-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmonotonic_arena_test.go
256 lines (205 loc) · 6.13 KB
/
monotonic_arena_test.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// SPDX-License-Identifier: Apache-2.0
package nuke
import (
"fmt"
"runtime"
"testing"
"time"
"unsafe"
"github.com/stretchr/testify/require"
)
func TestMonotonicArenaAllocateObject(t *testing.T) {
arena := NewMonotonicArena(8182, 1) // 8KB
var refs []*int
for i := 0; i < 1_000; i++ {
refs = append(refs, New[int](arena))
}
for i := 0; i < 1_000; i++ {
require.True(t, isMonotonicArenaPtr(arena, unsafe.Pointer(refs[i])))
}
}
func TestMonotonicArenaAllocateSlice(t *testing.T) {
arena := NewMonotonicArena(1024*1024, 1) // 8KB
var refs [][]int
for i := 0; i < 1_000; i++ {
ss := MakeSlice[int](arena, 8, 8)
refs = append(refs, ss)
}
for i := 0; i < 1_000; i++ {
ptr := unsafe.Pointer(unsafe.SliceData(refs[i]))
require.True(t, isMonotonicArenaPtr(arena, ptr))
}
}
func TestMonotonicArenaSendObjectToHeap(t *testing.T) {
var x int
arena := NewMonotonicArena(2*int(unsafe.Sizeof(x)), 1) // 2 ints room
// Send the first two ints to the arena
require.True(t, isMonotonicArenaPtr(arena, unsafe.Pointer(New[int](arena))))
require.True(t, isMonotonicArenaPtr(arena, unsafe.Pointer(New[int](arena))))
// Send last one to the heap
require.False(t, isMonotonicArenaPtr(arena, unsafe.Pointer(New[int](arena))))
}
func TestMonotonicArenaReset(t *testing.T) {
arena := NewMonotonicArena(1024, 1).(*monotonicArena) // one monotonic buffer of 1KB
// Allocate monotonic buffer
_ = New[int](arena)
// Configure finalizer
gced := make(chan bool)
runtime.SetFinalizer((*byte)(arena.buffers[0].ptr), func(*byte) {
close(gced)
})
// Reset the arena (without releasing memory)
arena.Reset(false)
runtime.GC()
select {
case <-gced:
require.Fail(t, "finalizer should not have been called")
case <-time.NewTimer(time.Second).C:
break
}
// Do another allocation
_ = New[int](arena)
// Reset the arena (releasing memory)
arena.Reset(true)
runtime.GC()
select {
case <-gced:
break
case <-time.NewTimer(time.Second).C:
require.Fail(t, "finalizer should have been called")
}
// Add this extra allocation here to prevent the compiler from marking arena reference as unused
// before invoking runtime.GC().
_ = New[int](arena)
}
func TestMonotonicArenaMultipleTypes(t *testing.T) {
arena := NewMonotonicArena(8182, 1) // 8KB
var b = New[byte](arena)
var p = New[*int](arena)
require.Equal(t, *b, byte(0))
require.True(t, *p == nil)
}
func isMonotonicArenaPtr(a Arena, ptr unsafe.Pointer) bool {
ma := a.(*monotonicArena)
for _, s := range ma.buffers {
if s.ptr == nil {
break
}
beginPtr := uintptr(s.ptr)
endPtr := uintptr(s.ptr) + s.size
if uintptr(ptr) >= beginPtr && uintptr(ptr) < endPtr {
return true
}
}
return false
}
type noScanObject struct {
a byte
b int
c uint64
d complex128
}
func BenchmarkRuntimeNewObject(b *testing.B) {
for _, objectCount := range []int{100, 1_000, 10_000, 100_000, 1_000_000} {
b.Run(fmt.Sprintf("%d", objectCount), func(b *testing.B) {
a := newRuntimeAllocator[noScanObject]()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for j := 0; j < objectCount; j++ {
_ = a.new()
}
}
})
}
}
func BenchmarkMonotonicArenaNewObject(b *testing.B) {
monotonicArena := NewMonotonicArena(32*1024*1024, 6) // 32Mb buffer size (192Mb max size)
a := newArenaAllocator[noScanObject](monotonicArena)
for _, objectCount := range []int{100, 1_000, 10_000, 100_000, 1_000_000} {
b.Run(fmt.Sprintf("%d", objectCount), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for j := 0; j < objectCount; j++ {
_ = a.new()
}
monotonicArena.Reset(false)
}
})
}
}
func BenchmarkConcurrentMonotonicArenaNewObject(b *testing.B) {
monotonicArena := NewMonotonicArena(32*1024*1024, 6) // 32Mb buffer size (192Mb max size)
a := newArenaAllocator[noScanObject](NewConcurrentArena(monotonicArena))
for _, objectCount := range []int{100, 1_000, 10_000, 100_000, 1_000_000} {
b.Run(fmt.Sprintf("%d", objectCount), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for j := 0; j < objectCount; j++ {
_ = a.new()
}
monotonicArena.Reset(false)
}
})
}
}
func BenchmarkRuntimeMakeSlice(b *testing.B) {
a := newRuntimeAllocator[noScanObject]()
for _, objectCount := range []int{100, 1_000, 10_000, 100_000, 1_000_000} {
b.Run(fmt.Sprintf("%d", objectCount), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for j := 0; j < objectCount; j++ {
_ = a.makeSlice(0, 256)
}
}
})
}
}
func BenchmarkMonotonicArenaMakeSlice(b *testing.B) {
monotonicArena := NewMonotonicArena(32*1024*1024, 6) // 32Mb buffer size (192Mb max size)
a := newArenaAllocator[noScanObject](monotonicArena)
for _, objectCount := range []int{100, 1_000, 10_000, 100_000, 1_000_000} {
b.Run(fmt.Sprintf("%d", objectCount), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for j := 0; j < objectCount; j++ {
_ = a.makeSlice(0, 256)
}
monotonicArena.Reset(false)
}
})
}
}
func BenchmarkConcurrentMonotonicArenaMakeSlice(b *testing.B) {
monotonicArena := NewMonotonicArena(32*1024*1024, 6) // 32Mb buffer size (192Mb max size)
a := newArenaAllocator[noScanObject](NewConcurrentArena(monotonicArena))
for _, objectCount := range []int{100, 1_000, 10_000, 100_000, 1_000_000} {
b.Run(fmt.Sprintf("%d", objectCount), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for j := 0; j < objectCount; j++ {
_ = a.makeSlice(0, 256)
}
monotonicArena.Reset(false)
}
})
}
}
type allocator[T any] interface {
new() *T
makeSlice(len, cap int) []T
}
type runtimeAllocator[T any] struct{}
func newRuntimeAllocator[T any]() allocator[T] {
return &runtimeAllocator[T]{}
}
func (r *runtimeAllocator[T]) new() *T { return new(T) }
func (r *runtimeAllocator[T]) makeSlice(len, cap int) []T { return make([]T, len, cap) }
type arenaAllocator[T any] struct {
a Arena
}
func newArenaAllocator[T any](a Arena) allocator[T] {
return &arenaAllocator[T]{a: a}
}
func (r *arenaAllocator[T]) new() *T { return New[T](r.a) }
func (r *arenaAllocator[T]) makeSlice(len, cap int) []T { return MakeSlice[T](r.a, len, cap) }