Documentation
¶
Overview ¶
Package heapslab provides a free-list Pool and a bump Arena for one type on the Go heap, for types that hold Go pointers and so cannot live off-heap.
Values are carved from chunks, ordinary []T slices, so the garbage collector still scans them, but it tracks one object per chunk instead of one per value, neighbouring values share cache lines, and a batch is freed or reused in one step. Pool.Put, Pool.Reset and Arena.Reset zero what they take back, so whatever those values referenced becomes collectable at once.
A pointer into a chunk keeps the whole chunk alive, so nothing handed out ever dangles: using a value after it was put back or reset reads or overwrites whatever the slot holds now, which is the one hazard. With the guards compiled in, a Pool catches putting an Item twice, putting it into another pool, and reading it after Put or Reset. An Arena checks nothing.
Example ¶
package main
import (
"fmt"
"github.com/stubbedev/memory-go/heapslab"
)
func main() {
type entry struct {
Name string
Tags []string
Next *entry
}
a, err := heapslab.NewArena[entry](1024)
if err != nil {
panic(err)
}
defer a.Free()
var head *entry
for _, name := range []string{"a", "b", "c"} {
e := a.Alloc()
*e = entry{Name: name, Tags: []string{"tag-" + name}, Next: head}
head = e
}
for e := head; e != nil; e = e.Next {
fmt.Print(e.Name, ":", e.Tags[0], ";")
}
fmt.Println()
a.Reset()
p, err := heapslab.NewPool[entry](1024)
if err != nil {
panic(err)
}
defer p.Free()
it := p.Get()
it.Value().Name = "conn"
fmt.Println(it.Value().Name, p.Len())
p.Put(it)
fmt.Printf("%q %d\n", p.Get().Value().Name, p.Len())
}
Output: c:tag-c;b:tag-b;a:tag-a; conn 1 "" 1
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrChunkSize reports a chunk size that is not positive. ErrChunkSize = chunks.ErrSize // ErrZeroSize reports a type with no bytes to pool. ErrZeroSize = chunks.ErrZeroSize )
Functions ¶
This section is empty.
Types ¶
type Arena ¶
type Arena[T any] struct { // contains filtered or unexported fields }
Arena hands out zeroed Ts from Go-heap chunks of chunkSize elements and takes them all back at once. It is not safe for concurrent use. Everything it hands out stays valid until Reset or Free.
func NewArena ¶
NewArena returns an Arena whose chunks hold chunkSize elements. It allocates the first chunk immediately.
func (*Arena[T]) AllocSlice ¶
AllocSlice returns n new zeroed Ts, with capacity n so that appending to it never overwrites a neighbour. Requests larger than the chunk size get a slice of their own, which Reset zeroes and drops. It panics if n is negative.
type Item ¶
type Item[T any] struct { // contains filtered or unexported fields }
Item is a T owned by a Pool. The zero Item is nil.
type Pool ¶
type Pool[T any] struct { // contains filtered or unexported fields }
Pool hands out zeroed Ts from Go-heap chunks and takes them back for reuse. It is not safe for concurrent use.
func NewPool ¶
NewPool returns a Pool that grows by chunkSize values at a time. It allocates the first chunk immediately.
func (*Pool[T]) Free ¶
func (p *Pool[T]) Free()
Free zeroes every value, invalidates every Item and drops the chunks for the garbage collector. p must not be used afterwards.