Documentation ¶
Overview ¶
Package mpool provides a sync.Pool equivalent that buckets incoming requests to one of 32 sub-pools, one for each power of 2, 0-32.
import "github.com/jbenet/go-msgio/mpool" var p mpool.Pool small := make([]byte, 1024) large := make([]byte, 4194304) p.Put(1024, small) p.Put(4194304, large) small2 := p.Get(1024).([]byte) large2 := p.Get(4194304).([]byte) fmt.Println("small2 len:", len(small2)) fmt.Println("large2 len:", len(large2)) // Output: // small2 len: 1024 // large2 len: 4194304
Index ¶
Examples ¶
Constants ¶
const MaxLength = 1 << 32
MaxLength is the maximum length of an element that can be added to the Pool.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pool ¶
type Pool struct { sync.Mutex // protecting list // New is a function that constructs a new element in the pool, with given len New func(len int) interface{} // contains filtered or unexported fields }
Pool is a pool to handle cases of reusing elements of varying sizes. It maintains up to 32 internal pools, for each power of 2 in 0-32.
Example ¶
var p Pool small := make([]byte, 1024) large := make([]byte, 4194304) p.Put(uint32(len(small)), small) p.Put(uint32(len(large)), large) small2 := p.Get(uint32(len(small))).([]byte) large2 := p.Get(uint32(len(large))).([]byte) fmt.Println("small2 len:", len(small2)) fmt.Println("large2 len:", len(large2))
Output: small2 len: 1024 large2 len: 4194304
var ByteSlicePool Pool
ByteSlicePool is a static Pool for reusing byteslices of various sizes.
func (*Pool) Get ¶
Get selects an arbitrary item from the Pool, removes it from the Pool, and returns it to the caller. Get may choose to ignore the pool and treat it as empty. Callers should not assume any relation between values passed to Put and the values returned by Get.
If Get would otherwise return nil and p.New is non-nil, Get returns the result of calling p.New.