mempool

package module
v0.0.0-...-a05549c Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 5 Imported by: 3

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, and a fast-access ring buffer to reduce main mutex contention.

func NewPool

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

func (*Pool) Check

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

func (*Pool) GC

func (p *Pool) GC()

func (*Pool[T]) Get

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

func (*Pool[T]) Put

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

func (*Pool[T]) Shard

func (p *Pool[T]) Shard() PoolShard[T]

Shard returns a new PoolShard[T] with a reference to this original pool. See type for more usage info.

func (*Pool) Size

func (p *Pool) Size() (sz int)

type PoolShard

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

PoolShard contains a reference to an original Pool, but with its own separate fast-access ring buffer to further reduce Pool's mutex contention.

func (*PoolShard[T]) Get

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

func (*PoolShard[T]) Original

func (p *PoolShard[T]) Original() *Pool[T]

Original returns a reference to the shard's origin pool.

func (*PoolShard[T]) Put

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

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, and a fast-access ring buffer to reduce main mutex contention.

func NewUnsafePool

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

func (*UnsafePool) Check

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

func (*UnsafePool) GC

func (p *UnsafePool) GC()

func (*UnsafePool) Get

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

func (*UnsafePool) Put

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

func (*UnsafePool) Shard

func (p *UnsafePool) Shard() UnsafePoolShard

Shard returns a new UnsafePoolShard with a reference to this original pool. See type for more usage info.

func (*UnsafePool) Size

func (p *UnsafePool) Size() (sz int)

type UnsafePoolShard

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

UnsafePoolShard contains a reference to an original UnsafePool, but with its own separate fast-access ring buffer to reduce the original UnsafePool's mutex contention.

func (*UnsafePoolShard) Get

func (s *UnsafePoolShard) Get() unsafe.Pointer

func (*UnsafePoolShard) Original

func (s *UnsafePoolShard) Original() *UnsafePool

Original returns a reference to the shard's origin pool.

func (*UnsafePoolShard) Put

func (s *UnsafePoolShard) Put(ptr unsafe.Pointer)

func (*UnsafePoolShard) Release

func (s *UnsafePoolShard) Release()

Release will release resources of this particular shard.

type UnsafeSimplePool

type UnsafeSimplePool struct {

	// Check determines how often to flush
	// internal pools based on underlying
	// current and victim pool sizes. It gets
	// called on every pool Put() operation.
	//
	// A flush will start a new current
	// pool, make victim the old current,
	// and drop the existing victim pool.
	Check 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 CheckGC().

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

func (*UnsafeSimplePool) GC

func (p *UnsafeSimplePool) GC()

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