Skip to content

Commit

Permalink
feat: add typed arena to benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
joetifa2003 committed Sep 8, 2024
1 parent ea9dfd7 commit de16911
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion mm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"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 {
Expand Down Expand Up @@ -47,14 +48,27 @@ func linkedListPushAlloc[T any](alloc allocator.Allocator, list *LinkedList[T],
}
}

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
}
allocator.Free(alloc, list)
}

const LINKED_LIST_SIZE = 10000
Expand Down Expand Up @@ -82,6 +96,33 @@ func BenchmarkLinkedListBatchAllocator(b *testing.B) {
}
}

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.NewCallocator()
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 {
Expand Down

0 comments on commit de16911

Please sign in to comment.