arena

package
v1.74.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package arena provides a typed arena allocator: a single Arena holds many objects of a single type in one slice. Each object is identified by its "index", which is 8 bytes (or 4, if the architecture cannot hold more bytes than that in a slice). This is the most compact way to hold complex objects.

There only way to release the objects is to drop the entire arena.

Index

Constants

View Source
const KeySizeBound = 32

Variables

This section is empty.

Functions

func NewBoundedKeyMap

func NewBoundedKeyMap[K ~string, V any]() *boundedArenaMap[K, V]

NewBoundedKeyMap returns a Map that uses string-like keys of bounded length. Keys are zero-padded, so must not end in zero bytes. This Map is not thread-safe.

It keep keys in an Arena. The map *panics* if it encounters a longer key.

func NewMap

func NewMap[K comparable, V any]() *arenaMap[K, V]

NewMap returns a Map backed by an arena. This Map is not thread-safe.

Types

type Arena

type Arena[T any] interface {
	// Append copies t into the arena and returns its index.  It invalidates results from all
	// previous Gets.  The returned Index is valid for the lifetime of the arena.
	Append(t T) Index
	// Get returns the T at index or nil.  The returned pointer is valid until the next time
	// the Arena is mutated (New or Append).
	Get(index Index) *T
	// Len returns the number of elements.
	Len() int
}

Arena holds Ts packed together for fast random access. It is an efficient way to store many Ts. If T is a pointer (or smaller), this does not save anything.

type Index

type Index struct {
	// contains filtered or unexported fields
}

type Map

type Map[K comparable, V any] interface {
	Put(k K, v V) *V
	Get(k K) *V
	Len() int
}

Map holds it values in an Arena. If V is a pointer (or smaller), this does not save anything.

type Optimizer

type Optimizer interface {
	Optimize()
}

Optimizer can be "optimized" after many writes and before many reads, to make them more efficient. Implementing this allows map keys to be compressed into an Arena.

type OptimizerMap

type OptimizerMap[K comparable, V any] interface {
	Map[K, V]
	Optimizer
}

type SliceArena

type SliceArena[T any] struct {
	// contains filtered or unexported fields
}

SliceArena is an Arena backed by a slice.

func New

func New[T any]() *SliceArena[T]

New returns an Arena. This Arena is not thread-safe.

func (*SliceArena[T]) Append

func (a *SliceArena[T]) Append(t T) Index

func (*SliceArena[T]) Get

func (a *SliceArena[T]) Get(index Index) *T

func (*SliceArena[T]) Len

func (a *SliceArena[T]) Len() int

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL