mempool

package module
v0.0.0-...-03e0c38 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: MIT Imports: 4 Imported by: 2

README

go-mempool

very simple memory pool implementation

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool[T any] struct {
	UnsafePool

	// New is an optionally provided
	// allocator used when no value
	// is available for use in pool.
	New func() T

	// Reset is an optionally provided
	// value resetting function called
	// on passed value to Put().
	Reset func(T) bool
}

Pool provides a form of SimplePool with the addition of concurrency safety.

func NewPool

func NewPool[T any](new func() T, reset func(T) bool, gc func(current, victim int) bool) Pool[T]

func (*Pool) Clear

func (p *Pool) Clear()

func (*Pool) GC

func (p *Pool) GC(fn func(current, victim int) bool) func(current, victim int) bool

func (*Pool[T]) Get

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

func (*Pool[T]) Put

func (p *Pool[T]) Put(t T)

func (*Pool) Size

func (p *Pool) Size() int

type SimplePool

type SimplePool[T any] struct {
	UnsafeSimplePool

	// New is an optionally provided
	// allocator used when no value
	// is available for use in pool.
	New func() T

	// Reset is an optionally provided
	// value resetting function called
	// on passed value to Put().
	Reset func(T) bool
}

SimplePool provides a type-safe form of UnsafePool using generics.

Note it is NOT safe for concurrent use, you must protect it yourself!

func (*SimplePool[T]) Get

func (p *SimplePool[T]) Get() T

func (*SimplePool[T]) Put

func (p *SimplePool[T]) Put(t T)

type UnsafePool

type UnsafePool struct {
	// contains filtered or unexported fields
}

UnsafePool provides a form of UnsafeSimplePool with the addition of concurrency safety.

func NewUnsafePool

func NewUnsafePool(gc func(current, victim int) bool) UnsafePool

func (*UnsafePool) Clear

func (p *UnsafePool) Clear()

func (*UnsafePool) GC

func (p *UnsafePool) GC(fn func(current, victim int) bool) func(current, victim int) bool

func (*UnsafePool) Get

func (p *UnsafePool) Get() unsafe.Pointer

func (*UnsafePool) Put

func (p *UnsafePool) Put(ptr unsafe.Pointer)

func (*UnsafePool) Size

func (p *UnsafePool) Size() int

type UnsafeSimplePool

type UnsafeSimplePool struct {

	// GC determines how often to flush
	// internal pools based on underlying
	// current and victim pool sizes.
	//
	// A flush will start a new current
	// pool, make victim the old current,
	// and drop the existing victim pool.
	GC func(current, victim int) bool
	// contains filtered or unexported fields
}

UnsafeSimplePool provides an incredibly simple memory pool implementation that stores ptrs to memory values, and regularly flushes internal pool structures according to GC function.

Note it is NOT safe for concurrent use, you must protect it yourself!

func (*UnsafeSimplePool) Clear

func (p *UnsafeSimplePool) Clear()

func (*UnsafeSimplePool) Get

func (p *UnsafeSimplePool) Get() unsafe.Pointer

func (*UnsafeSimplePool) Put

func (p *UnsafeSimplePool) Put(ptr unsafe.Pointer)

func (*UnsafeSimplePool) Size

func (p *UnsafeSimplePool) Size() int

Jump to

Keyboard shortcuts

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