bufferpool

package module
v0.0.0-...-7d6e165 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2015 License: BSD-2-Clause Imports: 2 Imported by: 17

README

bufferpool Build Status

The bufferpool package implements a thread-safe pool of reusable, equally sized byte.Buffers. If you're allocating byte.Buffers very frequently, you can use this to speed up your program and take strain off the garbage collector.

docs

GoDoc

Documentation

Overview

Package bufferpool implements a capacity-limited pool of reusable, equally-sized buffers.

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
}

A BufferPool is a capacity-limited pool of equally sized buffers.

func New

func New(poolSize, bufferSize int) *BufferPool

New returns a newly allocated BufferPool with the given maximum pool size and buffer size.

Example
package main

import (
	"fmt"

	"github.com/pushrax/bufferpool"
)

func main() {
	bp := bufferpool.New(10, 255)

	dogBuffer := bp.Take()
	dogBuffer.WriteString("Dog!")
	bp.Give(dogBuffer)

	catBuffer := bp.Take() // dogBuffer is reused and reset.
	catBuffer.WriteString("Cat!")

	fmt.Println(catBuffer)
}
Output:

Cat!

func (*BufferPool) Give

func (pool *BufferPool) Give(buf *bytes.Buffer) error

Give is used to attempt to return a buffer to the pool. It may not be added to the pool if it was already full.

func (*BufferPool) GiveSlice

func (pool *BufferPool) GiveSlice(slice []byte) error

GiveSlice is used to attempt to return a slice to the pool. It may not be added to the pool if it was already full.

func (*BufferPool) Take

func (pool *BufferPool) Take() *bytes.Buffer

Take is used to obtain a new zeroed buffer. This will allocate a new buffer if the pool was empty.

func (*BufferPool) TakeSlice

func (pool *BufferPool) TakeSlice() (slice []byte)

TakeSlice is used to obtain a new slice. This will allocate a new slice if the pool was empty.

Jump to

Keyboard shortcuts

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