The Golang Arena is here!

Metarsit Leenayongwut
4 min readFeb 12, 2023

--

Context

In Garbage Collector, we trust. Maybe…

Garbage Collection Overhead in Golang

The Go garbage collector can be CPU and Memory intensive, especially when collecting large numbers of objects or running multiple GC cycles concurrently. This may also result in periodic spikes in latency, which will impact real-time or high-performance systems.

Proposal

About

The Arena package is a memory allocation scheme allocated in bulk and managed as a series of small, fixed-size blocks. An arena is a fast, constant-time allocation strategy that can be useful in situations where many small objects need to be allocated and deallocated quickly. This implementation can manage memory allocation in a performance-critical part of a Go program, such as a server handling high volumes of incoming requests.

Arena Package

Methods

func NewArena() *Arena

Allocates the new arena memory onto Golang's unsafe pointer.

func (a *Arena) Free()

Frees the arena memory and all objects allocated from the arena. The exact address can be reused quickly without any garbage collector overhead. Arena memory and all objects allocated from the arena should not be called after the arena memory is freed.

func New[T any](a *Arena) *T

Creates a new pointer to the type provided to the generic function in arena memory.

func Clone[T any](s T) T

Makes a shallow copy of the input value to the heap. This means that the object is no longer bounded in arena memory and can persist after the arena memory is freed.

func MakeSlice[T any](a *Arena, len, cap int) []T

Creates a new slice with the provided type, capacity, and length. The slice must not be used after the arena is freed.

Examples

As of 13 February 2023, the Arena package is still an experimental feature in Go 1.20. To enable it, you need to turn it on by:

export GOEXPERIMENT=arenas

Allocation

Clone

Bad Sanitization

However, this will build and run fine, but the application may panic randomly. To prevent this, we can run with an ASAN flag, and it will throw an error at runtime.

root@docker-desktop:/medium# GOEXPERIMENT=arenas go run -asan main.go 
=================================================================
==3764==ERROR: AddressSanitizer: use-after-poison on address 0x4040007ff7f8 at pc 0x00000045cf44 bp 0x000000000000 sp 0x20400003ef30
WRITE of size 8 at 0x4040007ff7f8 thread T0
#0 0x45cf40 (/tmp/go-build73019719/b001/exe/main+0x45cf40)

Address 0x4040007ff7f8 is a wild pointer.
SUMMARY: AddressSanitizer: use-after-poison (/tmp/go-build73019719/b001/exe/main+0x45cf40)
Shadow bytes around the buggy address:
0x0818000ffea0: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
0x0818000ffeb0: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
0x0818000ffec0: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
0x0818000ffed0: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
0x0818000ffee0: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
=>0x0818000ffef0: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7[f7]
0x0818000fff00: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
0x0818000fff10: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
0x0818000fff20: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
0x0818000fff30: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
0x0818000fff40: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==3764==ABORTING
exit status 1

Conclusion

Although the Golang Arena Proposal was not ultimately integrated into the primary Go language, it remains an essential and widely-discussed proposal for improving the performance and memory usage of Go programs. It continues to be a topic of interest and research among the Go community. Its ideas and concepts have influenced the development of other language memory management strategies and techniques.

--

--