slab

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 0 Imported by: 0

README

slab

slab is a small Go package for arena-style allocators built around reusable slabs.

It provides primitives:

  • Slab[T] for reusable object slots.
  • SliceSlab[T] for reusable slice buffers.

Designed for hot paths where values are created in batches, used briefly, then discarded together, or reused in a cycles:

  • Preallocate once.
  • Allocate many times from the slab.
  • Reset between cycles.
  • When limits are reached, allocation falls back to the heap, so behavior stays simple and safe.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClearFunc

type ClearFunc[T any] func(*T)

type Slab

type Slab[T any] struct {
	// contains filtered or unexported fields
}

Slab is a simple slab allocator for objects of type T. It maintains an internal buffer of pre-allocated objects and allows for efficient allocation and resetting. If the internal buffer is exhausted, it falls back to heap allocation.

func NewSlab

func NewSlab[T any](size int, clear ClearFunc[T]) Slab[T]

NewSlab creates a new slab with the given capacity and clear function (a function that releases internal type resources; use nil if not needed).

func (*Slab[T]) Alloc

func (s *Slab[T]) Alloc() *T

Alloc returns a pointer to an object of type T. It first tries to allocate from the slab's internal buffer, and if that is exhausted, it falls back to heap allocation.

func (*Slab[T]) Reset

func (s *Slab[T]) Reset()

Reset clears the slab, making all previously allocated objects available for reuse. If a clear function was provided, it will be called on each object in the slab before resetting.

func (*Slab[T]) Stats added in v1.0.2

func (s *Slab[T]) Stats() Stats

Stats returns the current usage statistics of the slab, including the number of objects allocated from the internal buffer (Pool) and the number of objects allocated from the heap (Heap). Stats will be reset to zero after each call to Reset.

type SliceSlab

type SliceSlab[T any] struct {
	// contains filtered or unexported fields
}

SliceSlab is a simple slab allocator for slices. It preallocates a number of slices with a fixed capacity and returns them on demand. If the requested capacity exceeds the preallocated capacity or if all preallocated slices are used, it falls back to allocating a new slice on the heap.

func NewSliceSlab

func NewSliceSlab[T any](size int, cap int) SliceSlab[T]

NewSliceSlab creates a new SliceSlab with the given number of slices (size) and capacity for each slice (cap).

func (*SliceSlab[T]) Alloc

func (s *SliceSlab[T]) Alloc(cap int, sized bool) []T

Alloc returns a slice of type T with the requested minimum capacity. It first tries to allocate from the slab's internal buffer, and if that is exhausted or if the requested capacity exceeds the preallocated capacity, it falls back to heap allocation. If sized is true, the returned slice will have a length equal to the requested capacity; otherwise, it will have a length of 0 and the requested capacity.

func (*SliceSlab[T]) Reset

func (s *SliceSlab[T]) Reset()

Reset clears the slab, making all previously allocated slices available for reuse.

type Stats added in v1.0.2

type Stats struct {
	Pool int // number of objects used from pool
	Heap int // number of objects allocated on heap
}

Jump to

Keyboard shortcuts

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