bufferpool

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 27, 2019 License: MIT Imports: 1 Imported by: 0

README

GoDoc

bufferpool

a pool of byte slice, without the memory fragmentation

Byte slices in this pool are backed by a big array and all of them have a fixed, equal size so they will not grow as a result of append actions. They are safe for concurrent use because the ranges have not overlaps.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BufferPool

type BufferPool struct {
	// contains filtered or unexported fields
}

BufferPool provides a pool of byte slice, made from a single huge byte array.

Example
pool := New(10, 1000)

func() {
	buffer := pool.Take()
	defer pool.Put(buffer)
}()
Example (Concurrent)
// run with go test -race
pool := New(10, 1000)

start := make(chan struct{})
var done sync.WaitGroup
for i := 0; i < 1000; i++ {
	done.Add(1)
	go func() {
		defer done.Done()
		<-start
		buffer := pool.Take()
		defer pool.Put(buffer)
		for k := range buffer {
			buffer[k] = byte(k & 0xFF)
		}
	}()
}

close(start)
done.Wait()

func New

func New(bufferSize, bufferCount int) *BufferPool

New makes a new *BufferPool

func (*BufferPool) Expand

func (bf *BufferPool) Expand(bufferCount int)

Expand expands the pool bufferCount times, creating a new underlying array.

func (*BufferPool) Len

func (bf *BufferPool) Len() int

Len returns the length of the pool.

func (*BufferPool) Put

func (bf *BufferPool) Put(buffer []byte) bool

Put puts back a []byte into the pool unless the pool if full or the provided buffer has a different length than the initial buffer size.

func (*BufferPool) Take

func (bf *BufferPool) Take() (buffer []byte)

Take returns a byte slice from the pool or nil if the pool is depleted.

Jump to

Keyboard shortcuts

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