lbpool

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 2 Imported by: 2

README

Leaky Buffer pool

A pool solution that implements leaky buffer template.

It's slowly than vanilla pool but implements release logic in other hand. sync/pool is a great pool solution but it has a big inconvenience

Any item stored in the Pool may be removed automatically at any time without notification. If the Pool holds the only reference when this happens, the item might be deallocated.

This pool was made special for object like cbyte that requires manual release.

Use it the same as vanilla pools.

Benchmarks

BenchmarkPool-8                 20000000        81.8 ns/op       0 B/op       0 allocs/op
BenchmarkPoolParallel-8         100000000       19.6 ns/op       0 B/op       0 allocs/op
BenchmarkPoolNative-8           50000000        25.6 ns/op       0 B/op       0 allocs/op
BenchmarkPoolNativeParallel-8   200000000       5.54 ns/op       0 B/op       0 allocs/op

LB pool is 4-5 slowest that vanilla since it based on channels, whereas native is based on system pins.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool struct {
	// Maximum size of the pool.
	Size uint
	// Release factor (RF) value and internal counter.
	// RF is a value that indicates how big part of items should be released even if pool may store them.
	// This feature need for gradual refresh of pool data and avoid to bloating objects stored in the pool.
	// RF should be in range [0.0, 1.0]. Note, that RF value around or equal 1.0 is senseless since in that case poll
	// will store only small piece of the data.
	// Usually RF <= 0.05 is enough.
	ReleaseFactor float32

	// Function to make new object if pool didn't deliver existing.
	New func() interface{}
	// contains filtered or unexported fields
}

A Pool is a set of temporary objects. Object must implement release logic.

func NewPool

func NewPool(size uint, releaseFactor float32) *Pool

NewPool inits new pool with given size.

func (*Pool) Get

func (p *Pool) Get() interface{}

Get selects an arbitrary item from the Pool, removes it from the Pool, and returns it to the caller.

func (*Pool) Put

func (p *Pool) Put(x Releaser) bool

Put adds x to the pool.

type Releaser

type Releaser interface {
	Release()
}

Releaser is the interface that wraps the basic Release method.

Jump to

Keyboard shortcuts

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