pool

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcquireWriter

func AcquireWriter(w io.Writer) *bufio.Writer

AcquireWriter returns an empty Writer instance from writer pool.

func ReleaseBuffer

func ReleaseBuffer(buf *Buffer)

ReleaseBuffer returns buf acquired via AcquireBuffer/AcquireBufferSize to buffer pool.

It is forbidden accessing buf and/or its' members after returning it to buffer pool.

func ReleaseWriter

func ReleaseWriter(writer *bufio.Writer)

ReleaseWriter returns buf acquired via AcquireWriter to writer pool. It will flush and reset the writer before putting to writer pool.

Types

type Buffer

type Buffer struct {
	*bytes.Buffer
	// contains filtered or unexported fields
}

Buffer provides byte buffer, which can be used for minimizing memory allocations and implements interfaces: io.ReaderFrom, io.WriterTo, io.ReadWriteCloser.

The allocatedSize is the buffer's initial size.

func AcquireBuffer

func AcquireBuffer() *Buffer

AcquireBuffer returns an empty Buffer instance from buffer pool.

The returned Buffer instance may be passed to ReleaseBuffer when it is no longer needed. This allows Buffer recycling, reduces GC pressure and usually improves performance.

func AcquireBufferSize

func AcquireBufferSize(size int) *Buffer

AcquireBufferSize returns an empty Buffer instance from buffer pool, whose capacity is greater than or equal to the giving size.

The returned Buffer instance may be passed to ReleaseBuffer when it is no longer needed. This allows Buffer recycling, reduces GC pressure and usually improves performance.

This function is recommended when you know how much memory you need actually.

func NewBuffer

func NewBuffer(size int) *Buffer

NewBuffer creates a new Buffer which initialized by empty content.

func NewBufferString

func NewBufferString(s string) *Buffer

NewBufferString creates and initializes a new Buffer using string s as its content.

func (*Buffer) Close

func (b *Buffer) Close() error

type BufferPool

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

BufferPool stores several intervals of buffer's initial capacity, which can minimizing the allocation times by bytes.Buffer.grow(n int).

It groups the scenarios of using buffer, tries to avoid a large buffer not being recycling because it's used by callers which only need a small one.

func NewBufferPool

func NewBufferPool(count, base int) *BufferPool

func (*BufferPool) Get

func (bp *BufferPool) Get(size int) *Buffer

Get returns a buffer with a capacity from the buffer pool.

func (*BufferPool) Put

func (bp *BufferPool) Put(buf *Buffer)

Put puts the buf to the buffer pool.

Jump to

Keyboard shortcuts

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