mpool

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2015 License: MIT Imports: 2 Imported by: 0

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

View Source
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

func (p *Pool) Get(length uint32) interface{}

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.

func (*Pool) Put

func (p *Pool) Put(length uint32, val interface{})

Put adds x to the pool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL