package poolimport ()varDefaultPool = New(128, 65536)// 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 ( int) (interface{}, int) { returnDefaultPool.Get() }// Put takes x and its size for future reuse.// Put is a wrapper around DefaultPool.Put().func ( interface{}, int) { DefaultPool.Put(, ) }// Pool contains logic of reusing objects distinguishable by size in generic// way.typePoolstruct { pool map[int]*sync.Pool size func(int) int}// 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 (, int) *Pool {returnCustom(WithLogSizeMapping(),WithLogSizeRange(, ), )}// Custom creates new Pool with given options.func ( ...Option) *Pool { := &Pool{pool: make(map[int]*sync.Pool),size: pmath.Identity, } := (*poolConfig)()for , := range { () }return}// 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) ( int) (interface{}, int) { := .size()if := .pool[]; != nil {return .Get(), }returnnil, }// Put takes x and its size for future reuse.func ( *Pool) ( interface{}, int) {if := .pool[]; != nil { .Put() }}type poolConfig Pool// AddSize adds size n to the map.func ( *poolConfig) ( int) { .pool[] = new(sync.Pool)}// SetSizeMapping sets up incoming size mapping function.func ( *poolConfig) ( func(int) int) { .size = }
The pages are generated with Goldsv0.8.7. (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.