package pool
Import Path
github.com/gobwas/pool (on go.dev)
Dependency Relation
imports 2 packages, and imported by 3 packages
Involved Source Files
generic.go
option.go
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;
Package-Level Type Names (total 3)
Config describes generic pool configuration.
( Config) AddSize(n int)
( Config) SetSizeMapping(func(int) int)
Option configures pool.
func WithIdentitySizeMapping() Option
func WithLogSizeMapping() Option
func WithLogSizeRange(min, max int) Option
func WithSize(n int) Option
func WithSizeMapping(sz func(int) int) Option
func Custom(opts ...Option) *Pool
func github.com/gobwas/pool/pbufio.CustomReaderPool(opts ...Option) *pbufio.ReaderPool
func github.com/gobwas/pool/pbufio.CustomWriterPool(opts ...Option) *pbufio.WriterPool
func github.com/gobwas/pool/pbytes.Custom(opts ...Option) *pbytes.Pool
Pool contains logic of reusing objects distinguishable by size in generic
way.
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.
Put takes x and its size for future reuse.
func Custom(opts ...Option) *Pool
func New(min, max int) *Pool
var DefaultPool *Pool
Package-Level Functions (total 9)
Custom creates new Pool with given options.
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().
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.
Put takes x and its size for future reuse.
Put is a wrapper around DefaultPool.Put().
func WithIdentitySizeMapping() Option func WithLogSizeMapping() Option
WithSizeLogRange returns an Option that will add logarithmic range of
pooling sizes containing [min, max] values.
WithSize returns an Option that will add given pooling size to the pool.
func WithSizeMapping(sz func(int) int) Option
Package-Level Variables (only one)
var DefaultPool *Pool![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |