Documentation ¶
Overview ¶
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 ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultPool = New(128, 65536)
Functions ¶
func Get ¶
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().
Types ¶
type Option ¶
type Option func(Config)
Option configures pool.
func WithIdentitySizeMapping ¶
func WithIdentitySizeMapping() Option
func WithLogSizeMapping ¶
func WithLogSizeMapping() Option
func WithLogSizeRange ¶
WithSizeLogRange returns an Option that will add logarithmic range of pooling sizes containing [min, max] values.
func WithSizeMapping ¶
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool contains logic of reusing objects distinguishable by size in generic way.
func New ¶
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.
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
Package pbufio contains tools for pooling bufio.Reader and bufio.Writers.
|
Package pbufio contains tools for pooling bufio.Reader and bufio.Writers. |
Package pbytes contains tools for pooling byte pool.
|
Package pbytes contains tools for pooling byte pool. |