-
Notifications
You must be signed in to change notification settings - Fork 5
/
mm_test.go
178 lines (149 loc) · 3.78 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
package mm_test
import (
"fmt"
"runtime"
"testing"
"github.com/joetifa2003/mm-go"
"github.com/joetifa2003/mm-go/allocator"
"github.com/joetifa2003/mm-go/batchallocator"
"github.com/joetifa2003/mm-go/typedarena"
)
type Node[T any] struct {
value T
next *Node[T]
prev *Node[T]
}
type LinkedList[T any] struct {
head *Node[T]
tail *Node[T]
}
func linkedListPushManaged[T any](list *LinkedList[T], value T) {
node := &Node[T]{value: value}
if list.head == nil {
list.head = node
list.tail = node
} else {
list.tail.next = node
node.prev = list.tail
list.tail = node
}
}
func linkedListPushAlloc[T any](alloc allocator.Allocator, list *LinkedList[T], value T) {
node := allocator.Alloc[Node[T]](alloc)
node.value = value
if list.head == nil {
list.head = node
list.tail = node
} else {
list.tail.next = node
node.prev = list.tail
list.tail = node
}
}
func linkedListPushArena[T any](arena *typedarena.TypedArena[Node[T]], list *LinkedList[T], value T) {
node := arena.Alloc()
node.value = value
if list.head == nil {
list.head = node
list.tail = node
} else {
list.tail.next = node
node.prev = list.tail
list.tail = node
}
}
func linkedListFree[T any](alloc allocator.Allocator, list *LinkedList[T]) {
currentNode := list.head
for currentNode != nil {
nextNode := currentNode.next
allocator.Free(alloc, currentNode)
currentNode = nextNode
}
}
const LINKED_LIST_SIZE = 10000
func BenchmarkLinkedListManaged(b *testing.B) {
for range b.N {
benchLinkedListManaged(b, LINKED_LIST_SIZE)
runtime.GC()
}
}
func BenchmarkLinkedListCAlloc(b *testing.B) {
for range b.N {
benchLinkedListCAlloc(b, LINKED_LIST_SIZE)
}
}
func BenchmarkLinkedListBatchAllocator(b *testing.B) {
for _, bucketSize := range []int{100, 200, 500, LINKED_LIST_SIZE} {
b.Run(fmt.Sprintf("bucket size %d", bucketSize), func(b *testing.B) {
for range b.N {
benchLinkedListBatchAllocator(b, LINKED_LIST_SIZE, bucketSize)
}
})
}
}
func BenchmarkLinkedListTypedArena(b *testing.B) {
for _, chunkSize := range []int{100, 200, 500, LINKED_LIST_SIZE} {
b.Run(fmt.Sprintf("chunk size %d", chunkSize), func(b *testing.B) {
for range b.N {
benchLinkedListTypedArena(b, LINKED_LIST_SIZE, chunkSize)
}
})
}
}
func benchLinkedListTypedArena(b *testing.B, size int, chunkSize int) {
alloc := allocator.NewC()
defer alloc.Destroy()
arena := typedarena.New[Node[int]](alloc, chunkSize)
defer arena.Free()
list := allocator.Alloc[LinkedList[int]](alloc)
defer allocator.Free(alloc, list)
for i := range size {
linkedListPushArena(arena, list, i)
}
assertLinkedList(b, list)
}
func benchLinkedListManaged(b *testing.B, size int) {
list := &LinkedList[int]{}
for i := range size {
linkedListPushManaged(list, i)
}
assertLinkedList(b, list)
}
func benchLinkedListCAlloc(b *testing.B, size int) {
alloc := allocator.NewC()
defer alloc.Destroy()
list := allocator.Alloc[LinkedList[int]](alloc)
defer linkedListFree(alloc, list)
for i := range size {
linkedListPushAlloc(alloc, list, i)
}
assertLinkedList(b, list)
}
func benchLinkedListBatchAllocator(b *testing.B, size int, bucketSize int) {
alloc := batchallocator.New(allocator.NewC(),
batchallocator.WithBucketSize(mm.SizeOf[Node[int]]()*bucketSize),
)
defer alloc.Destroy()
list := allocator.Alloc[LinkedList[int]](alloc)
for i := range size {
linkedListPushAlloc(alloc, list, i)
}
assertLinkedList(b, list)
}
func assertLinkedList(t *testing.B, list *LinkedList[int]) {
if list.head == nil {
t.Fatal("list head is nil")
}
if list.tail == nil {
t.Fatal("list tail is nil")
}
currentNode := list.head
i := 0
for currentNode != nil {
if currentNode.value != i {
t.Fatalf("list value at index %d is %d, expected %d", i, currentNode.value, i)
}
i++
currentNode = currentNode.next
}
}