-
Notifications
You must be signed in to change notification settings - Fork 1
/
arena_test.go
195 lines (175 loc) · 4.44 KB
/
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
package arena
import (
"fmt"
"testing"
"unsafe"
"github.com/alecthomas/assert/v2"
)
type Struct struct {
String string
Int int
Float32 float32
Float64 float64
Int32 int32
Int64 int64
Uint32 uint32
Uint64 uint64
}
func TestArenaObjectTooLarge(t *testing.T) {
arena := Create(4)
assert.Panics(t, func() { New[Struct](arena) })
}
func TestArenaLimit(t *testing.T) {
arena := Create(int(unsafe.Sizeof(Struct{})), WithLimit(2))
assert.Equal(t, 1, len(arena.chunks))
New[Struct](arena)
assert.Equal(t, 2, len(arena.chunks))
New[Struct](arena)
assert.Equal(t, 2, len(arena.chunks), "should not expand once limit is reached")
assert.Panics(t, func() { New[Struct](arena) })
assert.Equal(t, 2, len(arena.chunks), "should not expand once limit is reached")
}
func TestArenaReuse(t *testing.T) {
arena := Create(100)
a := Value[Struct](arena, Struct{Int: 42})
a.String = "hello"
arena.Reset()
b := New[Struct](arena)
assert.Equal(t, a, b)
assert.Equal(t, a, &Struct{})
assert.Equal(t, unsafe.Pointer(unsafe.StringData(b.String)), unsafe.Pointer(unsafe.StringData(a.String)))
}
func TestSlice(t *testing.T) {
arena := Create(1000)
s := Make[Struct](arena, 10, 10)
for i := range 10 {
s[i].Int = i
}
for i := range 10 {
assert.Equal(t, i, s[i].Int)
}
}
func TestAppend(t *testing.T) {
arena := Create(10000)
s := make([]Struct, 0, 10)
expected := unsafe.SliceData(s)
s = Append(arena, s, Struct{Int: 1})
actual := unsafe.SliceData(s)
assert.Equal(t, 1, s[0].Int)
// Ensure the slice was not reallocated.
assert.Equal(t, unsafe.Pointer(expected), unsafe.Pointer(actual))
for i := range 20 {
s = Append(arena, s, Struct{Int: i + 2})
}
actual = unsafe.SliceData(s)
// Ensure the slice was reallocated.
assert.NotEqual(t, unsafe.Pointer(expected), unsafe.Pointer(actual))
for i := range 20 {
assert.Equal(t, i+1, s[i].Int)
}
}
func TestAppendShort(t *testing.T) {
arena := Create(1024)
s := Make[Struct](arena, 0, 10)
s = Append(arena, s, Struct{Int: 1})
assert.Equal(t, 1, s[0].Int)
}
func TestNewString(t *testing.T) {
arena := Create(100)
s := String(arena, "hello")
assert.Equal(t, "hello", s)
}
func TestNewInPlace(t *testing.T) {
arena := Create(100)
a := Value[Struct](arena, Struct{Int: 42})
assert.Equal(t, 42, a.Int)
}
func TestSetField(t *testing.T) {
arena := Create(1024 * 1024)
array := make([]*Struct, 1000)
for i := range 1000 {
a := New[Struct](arena)
a.Int = i
array[i] = a
}
for i := range 1000 {
assert.Equal(t, i, array[i].Int)
}
}
func BenchmarkArena(b *testing.B) {
arena := Create(32 * 1024 * 1024) // 32Mb chunk size
for _, objectCount := range []int{100, 1_000, 10_000, 100_000, 1_000_000} {
b.Run(fmt.Sprintf("%d", objectCount), func(b *testing.B) {
array := make([]*Struct, objectCount)
arena.Reset()
b.ReportAllocs()
b.ResetTimer()
for range b.N {
for j := range objectCount {
array[j] = New[Struct](arena)
}
}
})
}
}
func BenchmarkGoRuntime(b *testing.B) {
for _, objectCount := range []int{100, 1_000, 10_000, 100_000, 1_000_000} {
array := make([]*Struct, objectCount)
b.Run(fmt.Sprintf("%d", objectCount), func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for range b.N {
for j := range objectCount {
array[j] = new(Struct)
}
}
})
}
}
func BenchmarkArenaAppend(b *testing.B) {
arena := Create(512 * 1024 * 1024) // 512MB chunk size
for _, objectCount := range []int{100, 1_000, 10_000} {
b.Run(fmt.Sprintf("%d", objectCount), func(b *testing.B) {
arena.Reset()
b.ReportAllocs()
b.ResetTimer()
for range b.N {
s := Make[Struct](arena, 0, 16)
for j := range objectCount {
s = Append(arena, s, Struct{Int: j})
}
}
})
}
}
func BenchmarkGoRuntimeAppend(b *testing.B) {
for _, objectCount := range []int{100, 1_000, 10_000} {
b.Run(fmt.Sprintf("%d", objectCount), func(b *testing.B) {
b.ReportAllocs()
for range b.N {
s := make([]Struct, 0, 16)
for j := range objectCount {
s = append(s, Struct{Int: j}) //nolint:staticcheck
}
}
})
}
}
func BenchmarkReset(b *testing.B) {
arena := Create(64 * 1024 * 1024) // 64MB chunk size
for _, objectCount := range []int{100, 1_000, 10_000} {
b.Run(fmt.Sprintf("%d", objectCount), func(b *testing.B) {
for range b.N {
s := Make[Struct](arena, 0, 16)
for j := range objectCount {
s = Append(arena, s, Struct{Int: j})
}
}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
arena.Reset()
}
})
}
}