pool

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: 4 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 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

View Source
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.

func (Item[T]) IsNil

func (it Item[T]) IsNil() bool

IsNil reports whether it refers to nothing.

func (Item[T]) Value

func (it Item[T]) Value() *T

Value returns a Go pointer to the T, valid until the Item is put back.

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

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

New 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() error

Free releases every chunk, invalidating all Items. p must not be used afterwards.

func (*Pool[T]) Get

func (p *Pool[T]) Get() (Item[T], error)

Get returns a zeroed T.

func (*Pool[T]) Len

func (p *Pool[T]) Len() int

Len returns the number of Items handed out and not yet put back.

func (*Pool[T]) Put

func (p *Pool[T]) Put(it Item[T])

Put returns it to p, invalidating it.

Jump to

Keyboard shortcuts

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