Skip to content
This repository was archived by the owner on Oct 21, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions monotonic_arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (s *monotonicBuffer) alloc(size, alignment uintptr) (unsafe.Pointer, bool)
s.ptr = unsafe.Pointer(unsafe.SliceData(buf))
}
alignOffset := uintptr(0)
for alignedPtr := uintptr(s.ptr) + s.offset; alignedPtr%alignment != 0; alignedPtr++ {
alignOffset++
if delta := (uintptr(s.ptr) + s.offset) % alignment; delta != 0 {
alignOffset = delta
}
allocSize := size + alignOffset

Expand All @@ -46,10 +46,10 @@ func (s *monotonicBuffer) reset(release bool) {
}
s.offset = 0

s.zeroOutBuffer()

if release {
s.ptr = nil
} else {
s.zeroOutBuffer()
}
}

Expand All @@ -71,7 +71,7 @@ func (s *monotonicBuffer) availableBytes() uintptr {

// NewMonotonicArena creates a new monotonic arena with a specified number of buffers and a buffer size.
func NewMonotonicArena(bufferSize, bufferCount int) Arena {
a := &monotonicArena{}
a := &monotonicArena{buffers: make([]*monotonicBuffer, 0, bufferCount)}
for i := 0; i < bufferCount; i++ {
a.buffers = append(a.buffers, newMonotonicBuffer(bufferSize))
}
Expand All @@ -80,7 +80,7 @@ func NewMonotonicArena(bufferSize, bufferCount int) Arena {

// Alloc satisfies the Arena interface.
func (a *monotonicArena) Alloc(size, alignment uintptr) unsafe.Pointer {
for i := 0; i < len(a.buffers); i++ {
for i := range a.buffers {
ptr, ok := a.buffers[i].alloc(size, alignment)
if ok {
return ptr
Expand All @@ -91,7 +91,7 @@ func (a *monotonicArena) Alloc(size, alignment uintptr) unsafe.Pointer {

// Reset satisfies the Arena interface.
func (a *monotonicArena) Reset(release bool) {
for _, s := range a.buffers {
s.reset(release)
for i := range a.buffers {
a.buffers[i].reset(release)
}
}
12 changes: 6 additions & 6 deletions monotonic_arena_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func isMonotonicArenaPtr(a Arena, ptr unsafe.Pointer) bool {

func BenchmarkRuntimeNewObject(b *testing.B) {
a := newRuntimeAllocator[int]()
for _, objectCount := range []int{100, 1_000, 10_000, 100_000} {
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++ {
Expand All @@ -127,7 +127,7 @@ func BenchmarkMonotonicArenaNewObject(b *testing.B) {
monotonicArena := NewMonotonicArena(2*1024*1024, 32) // 2Mb buffer size (64Mb max size)

a := newArenaAllocator[int](monotonicArena)
for _, objectCount := range []int{100, 1_000, 10_000, 100_000} {
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++ {
Expand All @@ -144,7 +144,7 @@ func BenchmarkConcurrentMonotonicArenaNewObject(b *testing.B) {
monotonicArena := NewMonotonicArena(2*1024*1024, 32) // 2Mb buffer size (64Mb max size)

a := newArenaAllocator[int](NewConcurrentArena(monotonicArena))
for _, objectCount := range []int{100, 1_000, 10_000, 100_000} {
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++ {
Expand All @@ -159,7 +159,7 @@ func BenchmarkConcurrentMonotonicArenaNewObject(b *testing.B) {

func BenchmarkRuntimeMakeSlice(b *testing.B) {
a := newRuntimeAllocator[int]()
for _, objectCount := range []int{100, 1_000, 10_000, 100_000} {
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++ {
Expand All @@ -175,7 +175,7 @@ func BenchmarkMonotonicArenaMakeSlice(b *testing.B) {
monotonicArena := NewMonotonicArena(2*1024*1024, 32) // 2Mb buffer size (64Mb max size)

a := newArenaAllocator[int](monotonicArena)
for _, objectCount := range []int{100, 1_000, 10_000, 100_000} {
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++ {
Expand All @@ -192,7 +192,7 @@ func BenchmarkConcurrentMonotonicArenaMakeSlice(b *testing.B) {
monotonicArena := NewMonotonicArena(2*1024*1024, 32) // 2Mb buffer size (64Mb max size)

a := newArenaAllocator[int](NewConcurrentArena(monotonicArena))
for _, objectCount := range []int{100, 1_000, 10_000, 100_000} {
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++ {
Expand Down