typedarena

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package typedarena provides a chunked bump allocator for a single type whose values live off the Go heap and are freed all at once.

Allocation is a bounds check and an increment. Reset rewinds the arena so the same warm memory serves the next batch, which is where it outruns the Go allocator.

Example
package main

import (
	"fmt"

	memory "github.com/stubbedev/memory-go"
	"github.com/stubbedev/memory-go/typedarena"
)

func main() {
	var a memory.Local
	defer a.Close()

	type node struct {
		Value int
		Next  memory.Ref[node]
	}

	ta, err := typedarena.New[node](&a, 1024)
	if err != nil {
		panic(err)
	}
	defer ta.Free()

	var head memory.Ref[node]
	for i := range 3 {
		n, err := ta.Alloc()
		if err != nil {
			panic(err)
		}
		*n.Value() = node{Value: i, Next: head}
		head = n
	}
	for n := head; !n.IsNil(); n = n.Value().Next {
		fmt.Print(n.Value().Value, " ")
	}
	fmt.Println()
}
Output:
2 1 0

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrChunkSize = chunks.ErrSize

ErrChunkSize reports a chunk size that is not positive.

Functions

This section is empty.

Types

type Arena

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

Arena hands out zeroed Ts from chunks of chunkSize elements. It is not safe for concurrent use. Everything it hands out stays valid until Reset or Free.

func New

func New[T any](a memory.Allocator, chunkSize int) (*Arena[T], error)

New returns an Arena whose chunks hold chunkSize elements. It allocates the first chunk immediately.

func (*Arena[T]) Alloc

func (ta *Arena[T]) Alloc() (memory.Ref[T], error)

Alloc returns a reference to a new zeroed T.

func (*Arena[T]) AllocSlice

func (ta *Arena[T]) AllocSlice(n int) ([]T, error)

AllocSlice returns n new zeroed Ts. Requests larger than the chunk size get a chunk of their own, which Reset frees.

func (*Arena[T]) Free

func (ta *Arena[T]) Free() error

Free releases every chunk, invalidating everything ta handed out. ta must not be used afterwards.

func (*Arena[T]) Reset added in v0.2.0

func (ta *Arena[T]) Reset() error

Reset invalidates everything ta handed out and makes its chunks available again, zeroed, without returning them to the allocator.

Jump to

Keyboard shortcuts

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