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 ¶
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 ¶
New returns an Arena whose chunks hold chunkSize elements. It allocates the first chunk immediately.
func (*Arena[T]) AllocSlice ¶
AllocSlice returns n new zeroed Ts. Requests larger than the chunk size get a chunk of their own, which Reset frees.