Documentation
¶
Overview ¶
Package pool provides a free-list allocator for a single type whose values live off the Go heap and are recycled individually.
Get and Put never call the underlying allocator once a chunk has room, so they are the fastest way to allocate and free individual values in this module, and faster than new in a build with the memory_unchecked tag. With the guards compiled in, putting an Item twice, putting it into another pool, or reading it after Put panics.
Example ¶
package main
import (
"fmt"
memory "github.com/stubbedev/memory-go"
"github.com/stubbedev/memory-go/pool"
)
func main() {
var a memory.Local
defer a.Close()
type conn struct {
ID uint32
Bytes uint64
}
p, err := pool.New[conn](&a, 1024)
if err != nil {
panic(err)
}
defer p.Free()
c, err := p.Get()
if err != nil {
panic(err)
}
c.Value().ID = 7
fmt.Println(*c.Value(), p.Len())
p.Put(c)
reused, err := p.Get()
if err != nil {
panic(err)
}
fmt.Println(*reused.Value(), p.Len())
}
Output: {7 0} 1 {0 0} 1
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 Item ¶
type Item[T any] struct { // contains filtered or unexported fields }
Item is a T owned by a Pool. It may be stored off-heap. The zero Item is nil.
type Pool ¶
type Pool[T any] struct { // contains filtered or unexported fields }
Pool hands out zeroed Ts and takes them back for reuse. It is not safe for concurrent use.
func New ¶
New returns a Pool that grows by chunkSize values at a time. It allocates the first chunk immediately.
func (*Pool[T]) Free ¶
Free releases every chunk, invalidating all Items. p must not be used afterwards.