pool: github.com/gobwas/pool Index | Files | Directories

package pool

import "github.com/gobwas/pool"

Package pool contains helpers for pooling structures distinguishable by size.

Quick example:

import "github.com/gobwas/pool"

func main() {
   // Reuse objects in logarithmic range from 0 to 64 (0,1,2,4,6,8,16,32,64).
   p := pool.New(0, 64)

   buf, n := p.Get(10) // Returns buffer with 16 capacity.
   if buf == nil {
       buf = bytes.NewBuffer(make([]byte, n))
   }
   defer p.Put(buf, n)

   // Work with buf.
}

There are non-generic implementations for pooling: - pool/pbytes for []byte reuse; - pool/pbufio for *bufio.Reader and *bufio.Writer reuse;

Index

Package Files

generic.go option.go pool.go

Variables

var DefaultPool = New(128, 65536)

func Get Uses

func Get(size int) (interface{}, int)

Get pulls object whose generic size is at least of given size. It also returns a real size of x for further pass to Put(). It returns -1 as real size for nil x. Size >-1 does not mean that x is non-nil, so checks must be done.

Note that size could be ceiled to the next power of two.

Get is a wrapper around DefaultPool.Get().

func Put Uses

func Put(x interface{}, size int)

Put takes x and its size for future reuse. Put is a wrapper around DefaultPool.Put().

type Config Uses

type Config interface {
    AddSize(n int)
    SetSizeMapping(func(int) int)
}

Config describes generic pool configuration.

type Option Uses

type Option func(Config)

Option configures pool.

func WithIdentitySizeMapping Uses

func WithIdentitySizeMapping() Option

func WithLogSizeMapping Uses

func WithLogSizeMapping() Option

func WithLogSizeRange Uses

func WithLogSizeRange(min, max int) Option

WithSizeLogRange returns an Option that will add logarithmic range of pooling sizes containing [min, max] values.

func WithSize Uses

func WithSize(n int) Option

WithSize returns an Option that will add given pooling size to the pool.

func WithSizeMapping Uses

func WithSizeMapping(sz func(int) int) Option

type Pool Uses

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

Pool contains logic of reusing objects distinguishable by size in generic way.

func Custom Uses

func Custom(opts ...Option) *Pool

Custom creates new Pool with given options.

func New Uses

func New(min, max int) *Pool

New creates new Pool that reuses objects which size is in logarithmic range [min, max].

Note that it is a shortcut for Custom() constructor with Options provided by WithLogSizeMapping() and WithLogSizeRange(min, max) calls.

func (*Pool) Get Uses

func (p *Pool) Get(size int) (interface{}, int)

Get pulls object whose generic size is at least of given size. It also returns a real size of x for further pass to Put() even if x is nil. Note that size could be ceiled to the next power of two.

func (*Pool) Put Uses

func (p *Pool) Put(x interface{}, size int)

Put takes x and its size for future reuse.

Directories

PathSynopsis
internal/pmath
pbufioPackage pbufio contains tools for pooling bufio.Reader and bufio.Writers.
pbytesPackage pbytes contains tools for pooling byte pool.

Package pool imports 2 packages (graph) and is imported by 3 packages. Updated 2019-04-22. Refresh now. Tools for package owners.