heapslab

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 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 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

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

func NewArena[T any](chunkSize int) (*Arena[T], error)

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

func (*Arena[T]) Alloc

func (a *Arena[T]) Alloc() *T

Alloc returns a pointer to a new zeroed T.

func (*Arena[T]) AllocSlice

func (a *Arena[T]) AllocSlice(n int) []T

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.

func (*Arena[T]) Free

func (a *Arena[T]) Free()

Free zeroes everything a handed out and drops the chunks for the garbage collector. a must not be used afterwards.

func (*Arena[T]) Reset

func (a *Arena[T]) Reset()

Reset zeroes everything a handed out, so that nothing it referenced is kept alive by a, and makes the chunks available again. Pointers and slices handed out before stay valid memory but now alias new values.

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.

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 pointer to the T, valid until the Item is put back or its pool is reset.

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

func NewPool[T any](chunkSize int) (*Pool[T], error)

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.

func (*Pool[T]) Get

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

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 zeroes it, so that nothing it referenced is kept alive by p, and returns it to p, invalidating it.

func (*Pool[T]) Reset

func (p *Pool[T]) Reset()

Reset zeroes every value and invalidates every Item, keeping the chunks for reuse.

Jump to

Keyboard shortcuts

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