Documentation
¶
Index ¶
- func Append[T any](ar *Arena, src []T, values ...T) []T
- func DeepCopy[T any](ar *Arena, v T) *T
- func MustValidate[T any]()
- func New[T any](ar *Arena) *T
- func NewSlice[T any](ar *Arena, length, capacity int) (result []T)
- func Validate[T any]() error
- type Arena
- func (ar *Arena) AuditPointers(obj any) []PointerViolation
- func (ar *Arena) Bytes(v []byte) (b []byte)
- func (ar *Arena) Free(ptrT any)
- func (ar *Arena) IsManagedPointer(ptr unsafe.Pointer) bool
- func (ar *Arena) Malloc(sz uintptr) unsafe.Pointer
- func (ar *Arena) NewBool(v bool) *bool
- func (ar *Arena) NewFloat32(v float32) *float32
- func (ar *Arena) NewFloat64(v float64) *float64
- func (ar *Arena) NewInt(v int) *int
- func (ar *Arena) NewInt8(v int8) *int8
- func (ar *Arena) NewInt16(v int16) *int16
- func (ar *Arena) NewInt32(v int32) *int32
- func (ar *Arena) NewInt64(v int64) *int64
- func (ar *Arena) NewString(v string) (s *string)
- func (ar *Arena) NewUint(v uint) *uint
- func (ar *Arena) NewUint8(v uint8) *uint8
- func (ar *Arena) NewUint16(v uint16) *uint16
- func (ar *Arena) NewUint32(v uint32) *uint32
- func (ar *Arena) NewUint64(v uint64) *uint64
- func (ar *Arena) Reset()
- func (ar *Arena) String(s string) string
- type Map
- func (m *Map[K, V]) AddIfAbsent(key K, value V) bool
- func (m *Map[K, V]) All() iter.Seq2[K, V]
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) Get(key K) (V, bool)
- func (m *Map[K, V]) Len() int
- func (m *Map[K, V]) MarshalJSON() ([]byte, error)
- func (m *Map[K, V]) Put(key K, value V)
- func (m *Map[K, V]) Remove(key K)
- func (m *Map[K, V]) UnmarshalJSON(data []byte) error
- type Memory
- type Option
- type PointerViolation
- type Vector
- func (v *Vector[T]) AddIfAbsent(value T) bool
- func (v *Vector[T]) All() iter.Seq2[int, T]
- func (v *Vector[T]) Append(values ...T) *Vector[T]
- func (v *Vector[T]) At(index int) T
- func (v *Vector[T]) Cap() int
- func (v *Vector[T]) Clear()
- func (v *Vector[T]) Equatable(equatable func(a, b T) bool) *Vector[T]
- func (v *Vector[T]) Index(value T) int
- func (v *Vector[T]) LastIndex(value T) int
- func (v *Vector[T]) Len() int
- func (v *Vector[T]) MarshalJSON() ([]byte, error)
- func (v *Vector[T]) Remove(value T) bool
- func (v *Vector[T]) RemoveBy(limit int, fn func(index int, v T) bool) int
- func (v *Vector[T]) RemoveIdx(idx int)
- func (v *Vector[T]) UnmarshalJSON(data []byte) error
- type ViolationKind
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Append ¶
Append extends a slice managed by the Arena, reallocating if necessary. The underlying memory is managed by the Arena.
func DeepCopy ¶
DeepCopy allocates an object of type T from the Arena and performs a deep copy of the source object v into it. The underlying memory is managed by the Arena.
func MustValidate ¶
func MustValidate[T any]()
func New ¶
New allocates memory for a type T and returns a pointer to it. The object's lifetime is tied to the Arena.
Types ¶
type Arena ¶
type Arena struct {
// contains filtered or unexported fields
}
Arena manages memory chunks and provides allocation services with reduced GC overhead. It maintains reusable memory blocks and handles alignment automatically.
Locking: the internal lock (if enabled via WithEnableLock) protects only the allocator's chunk/freelist/refcount state. Map and Vector containers are NOT protected by this lock — concurrent access to individual container instances requires external synchronization.
func NewArena ¶
NewArena creates a new Arena instance with customizable options. Options can configure chunk size, memory pool size, locking behavior, and memory source.
IMPORTANT: The caller must keep the Arena reachable by the GC (e.g., hold a reference in a non-arena variable) for the entire lifetime of any objects allocated from it. Arena-allocated objects store their *Arena reference inside arena memory (backed by []byte), which the GC does not scan. If the Arena becomes unreachable, the GC may collect it while arena objects are still in use, causing undefined behavior.
func (*Arena) AuditPointers ¶
func (ar *Arena) AuditPointers(obj any) []PointerViolation
AuditPointers recursively scans an arena-allocated object for pointers that do not belong to this arena's memory. Returns a list of violations describing each non-arena pointer found, including the field path and type information.
This is a debugging tool — it walks the object graph using reflect and checks every pointer, slice backing array, string data, and func closure pointer against the arena's managed address ranges.
func (*Arena) Bytes ¶
Bytes allocates a byte slice's backing array in the Arena and copies the input data. The slice header is returned by value (on stack), not allocated in arena. Only the backing data is arena-managed.
func (*Arena) Free ¶
Free releases a previously allocated memory block. The pointer must belong to this Arena.
func (*Arena) IsManagedPointer ¶
IsManagedPointer reports whether the given pointer points into memory managed by this Arena.
func (*Arena) Malloc ¶
Malloc allocates a memory block of the given size. Returned pointer is aligned. Panics if size is zero.
func (*Arena) NewBool ¶
NewBool allocates a boolean in the Arena and initializes it with the given value.
func (*Arena) NewFloat32 ¶
NewFloat32 allocates an float32 in the Arena and initializes it with the given value.
func (*Arena) NewFloat64 ¶
NewFloat64 allocates an float64 in the Arena and initializes it with the given value.
func (*Arena) NewInt ¶
NewInt allocates an integer in the Arena and initializes it with the given value.
func (*Arena) NewInt8 ¶
NewInt8 allocates an int8 in the Arena and initializes it with the given value.
func (*Arena) NewInt16 ¶
NewInt16 allocates an int16 in the Arena and initializes it with the given value.
func (*Arena) NewInt32 ¶
NewInt32 allocates an int32 in the Arena and initializes it with the given value.
func (*Arena) NewInt64 ¶
NewInt64 allocates an int64 in the Arena and initializes it with the given value.
func (*Arena) NewString ¶
NewString allocates a string header and data in the Arena, returning *string.
func (*Arena) NewUint ¶
NewUint allocates an uint in the Arena and initializes it with the given value.
func (*Arena) NewUint8 ¶
NewUint8 allocates an uint8 in the Arena and initializes it with the given value.
func (*Arena) NewUint16 ¶
NewUint16 allocates an uint16 in the Arena and initializes it with the given value.
func (*Arena) NewUint32 ¶
NewUint32 allocates an uint32 in the Arena and initializes it with the given value.
func (*Arena) NewUint64 ¶
NewUint64 allocates an uint64 in the Arena and initializes it with the given value.
func (*Arena) Reset ¶
func (ar *Arena) Reset()
Reset clears all allocated chunks and resets the Arena to its initial state. Existing pointers become invalid after this operation.
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is an arena-native hash map using linear probing. All data (ctrl, keys, values) is stored in arena-allocated slices.
Map is NOT thread-safe. Concurrent calls to Put/Get/Remove on the same Map instance require external synchronization (e.g., sync.Mutex). The Arena's internal lock (if enabled) only protects the allocator, not the Map's internal state.
func NewMap ¶
func NewMap[K comparable, V any](allocator *Arena, capacity int) *Map[K, V]
NewMap creates a new arena-native hash map with specified initial capacity. The returned Map is NOT thread-safe — use external synchronization (e.g., sync.Mutex) for concurrent access.
func (*Map[K, V]) AddIfAbsent ¶
AddIfAbsent stores a key-value pair only if the key doesn't exist. Returns true if added, false if the key already existed.
func (*Map[K, V]) Clear ¶
func (m *Map[K, V]) Clear()
Clear removes all entries and frees all value arena memory.
func (*Map[K, V]) Get ¶
Get retrieves the value for the given key. Returns the value and true if found, or the zero value and false otherwise.
func (*Map[K, V]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. It serializes the Map as a JSON object ({"key": value, ...}).
func (*Map[K, V]) Put ¶
func (m *Map[K, V]) Put(key K, value V)
Put stores a key-value pair. Existing values are freed and overwritten.
func (*Map[K, V]) Remove ¶
func (m *Map[K, V]) Remove(key K)
Remove deletes a key-value pair and frees the value's arena memory.
func (*Map[K, V]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface. It clears the Map and populates it from a JSON object. All data is deep-copied into arena memory.
type Option ¶
type Option func(*arenaOptions)
Option defines a function type for configuring Arena parameters
func WithChunkSize ¶
WithChunkSize sets the base allocation size for memory chunks. Larger values reduce allocation frequency but may increase waste. Minimum size is automatically aligned to system pointer size.
func WithEnableLock ¶
WithEnableLock enables thread-safe operation using a spinlock. Required when the Arena is accessed concurrently. When enabled, adds ~5-15ns overhead per allocation.
NOTE: This lock protects ONLY the Arena allocator's internal state (chunk management, freelist, refcounts). It does NOT protect the contents of Map or Vector containers allocated from this Arena. Concurrent access to individual Map/Vector instances requires external synchronization (e.g., sync.Mutex).
func WithMemory ¶
WithMemory specifies a custom memory allocator implementing the Memory interface. Allows integration with alternative memory sources (e.g., mmap, cgo, shm). Default: heapMemory (standard Go allocations).
func WithPoolSize ¶
WithPoolSize configures the maximum number of reusable chunks retained in the free list. Higher values improve reuse at the cost of increased memory retention.
type PointerViolation ¶
type PointerViolation struct {
Path string // Full field path, e.g. "GameState.Players.vec[3].Name"
Kind ViolationKind // Type of violation: pointer, slice, string, or func
Address uintptr // The offending pointer address
Type reflect.Type // The reflect.Type of the field
Hint string // Additional context to help diagnose the violation
}
PointerViolation describes a single pointer safety violation found in an arena object.
type Vector ¶
type Vector[T any] struct { // contains filtered or unexported fields }
Vector is an Arena-backed dynamic array providing type-safe operations. It reduces GC pressure by storing elements in contiguous Arena memory.
Vector is NOT thread-safe. Concurrent calls on the same Vector instance require external synchronization (e.g., sync.Mutex). The Arena's internal lock (if enabled) only protects the allocator, not the Vector's internal state.
func NewVector ¶
NewVector creates a new Vector with specified initial capacity. The vector's memory is managed by the provided Arena allocator. The returned Vector is NOT thread-safe — use external synchronization (e.g., sync.Mutex) for concurrent access.
func (*Vector[T]) AddIfAbsent ¶
AddIfAbsent adds an element only if it doesn't already exist in the vector.
func (*Vector[T]) Equatable ¶
Equatable sets a custom equality comparison function for element comparison.
func (*Vector[T]) Index ¶
Index finds the first occurrence of an element. Index of first match, or -1 if not found
func (*Vector[T]) LastIndex ¶
LastIndex finds the last occurrence of an element. Index of last match, or -1 if not found
func (*Vector[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. It serializes the Vector as a JSON array ([elem1, elem2, ...]).
func (*Vector[T]) RemoveBy ¶
RemoveBy removes elements matching a condition with quantity control. use limit param to control maximum number of elements to remove (0 = unlimited)
func (*Vector[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface. It clears the Vector and populates it from a JSON array. All data is deep-copied into arena memory.
type ViolationKind ¶
type ViolationKind string
ViolationKind describes the type of pointer violation found by AuditPointers.
const ( ViolationPointer ViolationKind = "pointer" ViolationSlice ViolationKind = "slice" ViolationString ViolationKind = "string" ViolationFunc ViolationKind = "func" )