bytebufferpool

package
v0.0.0-...-d4cc26c Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2016 License: MIT, MIT Imports: 3 Imported by: 0

README

Build Status GoDoc Go Report

bytebufferpool

An implementation of a pool of byte buffers with anti-fragmentation protection.

The pool may waste limited amount of memory due to fragmentation. This amount equals to the maximum total size of the byte buffers in concurrent use.

bytebufferpool users

Documentation

Overview

Package bytebufferpool implements a pool of byte buffers with anti-fragmentation protection.

The pool may waste limited amount of memory due to fragmentation. This amount equals to the maximum total size of the byte buffers in concurrent use.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Release

func Release(b *ByteBuffer)

Release returns byte buffer to the pool.

ByteBuffer.B mustn't be touched after returning it to the pool. Otherwise data races will occur.

Types

type ByteBuffer

type ByteBuffer struct {

	// B is a byte buffer to use in append-like workloads.
	// See example code for details.
	B []byte
}

ByteBuffer provides byte buffer, which can be used for minimizing memory allocations.

ByteBuffer may be used with functions appending data to the given []byte slice. See example code for details.

Use Acquire for obtaining an empty byte buffer.

Example
package main

import (
	"fmt"

	"github.com/beyondblog/k8s-web-terminal/Godeps/_workspace/src/github.com/valyala/bytebufferpool"
)

func main() {
	bb := bytebufferpool.Acquire()

	bb.WriteString("first line\n")
	bb.Write([]byte("second line\n"))
	bb.B = append(bb.B, "third line\n"...)

	fmt.Printf("bytebuffer contents=%q", bb.B)

	// It is safe to release byte buffer now, since it is
	// no longer used.
	bytebufferpool.Release(bb)
}
Output:

func Acquire

func Acquire() *ByteBuffer

Acquire returns an empty byte buffer from the pool.

Acquired byte buffer may be returned to the pool via Release call. This reduces the number of memory allocations required for byte buffer management.

func (*ByteBuffer) Reset

func (b *ByteBuffer) Reset()

Reset makes ByteBuffer.B empty.

func (*ByteBuffer) Set

func (b *ByteBuffer) Set(p []byte)

Set sets ByteBuffer.B to p

func (*ByteBuffer) SetString

func (b *ByteBuffer) SetString(s string)

SetString sets ByteBuffer.B to s

func (*ByteBuffer) Write

func (b *ByteBuffer) Write(p []byte) (int, error)

Write implements io.Writer - it appends p to ByteBuffer.B

func (*ByteBuffer) WriteString

func (b *ByteBuffer) WriteString(s string) (int, error)

WriteString appends s to ByteBuffer.B

Jump to

Keyboard shortcuts

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