forked from joetifa2003/mm-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm_test.go
219 lines (180 loc) · 4.56 KB
/
mm_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
package mm_test
import (
"fmt"
"runtime"
"testing"
"github.com/joetifa2003/mm-go"
"github.com/joetifa2003/mm-go/typedarena"
"github.com/stretchr/testify/assert"
)
type Node struct {
Value int
Prev *Node
Next *Node
}
func heapManaged(nodes int) {
allocated := make([]*Node, nodes)
for j := 0; j < nodes; j++ {
var prev *Node
var next *Node
if j != 0 {
prev = allocated[j-1]
}
if j != nodes-1 {
next = allocated[j+1]
}
allocated[j] = &Node{
Value: j,
Prev: prev,
Next: next,
}
}
runtime.GC()
}
func BenchmarkHeapManaged(b *testing.B) {
benchMarkSuit(b, heapManaged)
}
func manual(nodes int) {
allocatedNodes := mm.AllocMany[Node](nodes)
for j := 0; j < nodes; j++ {
var prev *Node
var next *Node
if j != 0 {
prev = &allocatedNodes[j-1]
}
if j != nodes-1 {
next = &allocatedNodes[j+1]
}
allocatedNodes[j] = Node{
Value: j,
Prev: prev,
Next: next,
}
}
mm.FreeMany(allocatedNodes)
runtime.GC()
}
func BenchmarkManual(b *testing.B) {
benchMarkSuit(b, manual)
}
func arenaManual(nodes int) {
arena := typedarena.New[Node](nodes)
allocatedNodes := arena.AllocMany(nodes)
for j := 0; j < nodes; j++ {
var prev *Node
var next *Node
if j != 0 {
prev = &allocatedNodes[j-1]
}
if j != nodes-1 {
next = &allocatedNodes[j+1]
}
allocatedNodes[j] = Node{
Value: j,
Prev: prev,
Next: next,
}
}
arena.Free()
runtime.GC()
}
func BenchmarkArenaManual(b *testing.B) {
benchMarkSuit(b, arenaManual)
}
func benchMarkSuit(b *testing.B, f func(int)) {
nodeCounts := []int{10000, 100000, 10000000, 100000000}
for _, nc := range nodeCounts {
b.Run(fmt.Sprintf("node count %d", nc), func(b *testing.B) {
for i := 0; i < b.N; i++ {
f(nc)
}
})
}
}
type TreeNode struct {
value int
left, right *TreeNode
}
func createTreeManaged(depth int) *TreeNode {
if depth != 0 {
return &TreeNode{
value: depth,
left: createTreeManaged(depth - 1),
right: createTreeManaged(depth - 1),
}
}
return nil
}
func createTreeManual(depth int, arena *typedarena.TypedArena[TreeNode]) *TreeNode {
if depth != 0 {
node := arena.Alloc()
node.left = createTreeManual(depth-1, arena)
node.right = createTreeManual(depth-1, arena)
return node
}
return nil
}
func sumBinaryTree(tree *TreeNode) int {
if tree.left == nil && tree.right == nil {
return tree.value
}
return sumBinaryTree(tree.left) + sumBinaryTree(tree.right)
}
const TREE_DEPTH = 26
func BenchmarkBinaryTreeManaged(b *testing.B) {
for n := 0; n < b.N; n++ {
tree := createTreeManaged(TREE_DEPTH)
runtime.GC()
sumBinaryTree(tree)
}
}
func BenchmarkBinaryTreeArena(b *testing.B) {
for _, chunkSize := range []int{50, 100, 150, 250, 500} {
b.Run(fmt.Sprintf("chunk size %d", chunkSize), func(b *testing.B) {
for n := 0; n < b.N; n++ {
arena := typedarena.New[TreeNode](chunkSize)
tree := createTreeManual(TREE_DEPTH, arena)
runtime.GC()
sumBinaryTree(tree)
arena.Free()
}
})
}
}
func TestAllocMany(t *testing.T) {
assert := assert.New(t)
allocated := mm.AllocMany[int](2) // allocates 2 ints and returns it as a slice of ints with length 2
defer mm.FreeMany(allocated) // it's recommended to make sure the data gets deallocated (defer recommended to prevent leaks)
assert.Equal(2, len(allocated))
allocated[0] = 15 // changes the data in the slice (aka the heap)
ptr := &allocated[0] // takes a pointer to the data in the heap
*ptr = 45 // changes the value from 15 to 45
assert.Equal(45, allocated[0])
}
func TestAlloc(t *testing.T) {
assert := assert.New(t)
ptr := mm.Alloc[int]() // allocates a single int and returns a ptr to it
defer mm.Free(ptr) // frees the int (defer recommended to prevent leaks)
*ptr = 15
assert.Equal(15, *ptr)
ptr2 := mm.Alloc[[1e3]int]() // creates large array to make malloc mmap new chunk
defer mm.Free(ptr2)
}
func TestReallocate(t *testing.T) {
assert := assert.New(t)
allocated := mm.AllocMany[int](2) // allocates 2 int and returns it as a slice of ints with length 2
allocated[0] = 15
assert.Equal(2, len(allocated))
allocated = mm.Reallocate(allocated, 3)
assert.Equal(3, len(allocated))
assert.Equal(15, allocated[0]) // data after reallocation stays the same
mm.FreeMany(allocated) // didn't use defer here because i'm doing a reallocation and changing the value of allocated variable (otherwise can segfault)
}
func TestUnmapChunk(t *testing.T) {
data1 := mm.AllocMany[int](1e6)
data2 := mm.AllocMany[int](1e6)
data3 := mm.AllocMany[int](1e6)
mm.FreeMany(data2)
mm.FreeMany(data1)
mm.FreeMany(data3)
}