pool

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultSize = 16 * 0x400
View Source
const MaxLength = math.MaxInt32

MaxLength is the maximum length of an element that can be added to the Pool.

View Source
const MaxSegmentSize = math.MaxUint16
View Source
const MinRead = 512

MinRead is the minimum slice size passed to a Read call by Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond what is required to hold the contents of r, ReadFrom will not grow the underlying buffer.

Variables

This section is empty.

Functions

func Clone added in v0.3.6

func Clone(b []byte) []byte

func GetBufioReader added in v0.3.6

func GetBufioReader(r io.Reader, size int) *bufio.Reader

func GetBytes

func GetBytes[T constraints.Integer](size T) []byte

func PutBufioReader added in v0.3.6

func PutBufioReader(b *bufio.Reader)

func PutBytes

func PutBytes(b []byte)

Types

type Buffer added in v0.3.5

type Buffer struct {
	// Pool is the buffer pool to use. If nil, this Buffer will use the
	// global buffer pool.
	Pool Pool
	// contains filtered or unexported fields
}

Buffer is a buffer like bytes.Buffer that:

1. Uses a buffer pool. 2. Frees memory on read.

If you only have a few buffers and read/write at a steady rate, *don't* use this package, it'll be slower.

However:

  1. If you frequently create/destroy buffers, this implementation will be significantly nicer to the allocator.
  2. If you have many buffers with bursty traffic, this implementation will use significantly less memory.

func NewBUfferNoCopy added in v0.3.6

func NewBUfferNoCopy(buf []byte) *Buffer

NewBuffer constructs a new buffer initialized to `buf`.

func NewBuffer added in v0.3.5

func NewBuffer(buf []byte) *Buffer

NewBuffer constructs a new buffer initialized to `buf`. Unlike `bytes.Buffer`, we *copy* the buffer but don't reuse it (to ensure that we *only* use buffers from the pool).

func NewBufferSize added in v0.3.6

func NewBufferSize[T constraints.Integer](size T) *Buffer

NewBufferSize constructs a new buffer of the given size.

func NewBufferString added in v0.3.6

func NewBufferString(buf string) *Buffer

NewBufferString is identical to NewBuffer *except* that it allows one to initialize the buffer from a string (without having to allocate an intermediate bytes slice).

func (*Buffer) Advance added in v0.3.5

func (b *Buffer) Advance(n int)

func (*Buffer) Bytes added in v0.3.5

func (b *Buffer) Bytes() []byte

Bytes returns the slice of bytes currently buffered in the Buffer.

The buffer returned by Bytes is valid until the next call grow, truncate, read, or write. Really, just don't touch the Buffer until you're done with the return value of this function.

func (*Buffer) Cap added in v0.3.6

func (b *Buffer) Cap() int

Cap returns the current capacity of the buffer.

Note: Buffer *may* re-allocate when writing (or growing by) `n` bytes even if `Cap() < Len() + n` to avoid excessive copying.

func (*Buffer) Close added in v0.3.6

func (b *Buffer) Close() error

just for impl WriteCloser

func (*Buffer) Grow added in v0.3.6

func (b *Buffer) Grow(n int)

Grow grows the internal buffer such that `n` bytes can be written without reallocating.

func (*Buffer) Len added in v0.3.5

func (b *Buffer) Len() int

Len returns the number of bytes that can be read from this buffer.

func (*Buffer) Next added in v0.3.6

func (b *Buffer) Next(n int) []byte

Next is an alternative to `Read` that returns a byte slice instead of taking one.

The returned byte slice is valid until the next read, write, grow, or truncate.

func (*Buffer) Read added in v0.3.6

func (b *Buffer) Read(buf []byte) (int, error)

Read reads at most `len(buf)` bytes from the internal buffer into the given buffer.

func (*Buffer) ReadByte added in v0.3.6

func (b *Buffer) ReadByte() (byte, error)

ReadByte reads a single byte from the Buffer.

func (*Buffer) ReadFrom added in v0.3.5

func (b *Buffer) ReadFrom(r io.Reader) (int64, error)

ReadFrom reads from the given reader into the buffer.

func (*Buffer) Reset added in v0.3.6

func (b *Buffer) Reset()

Reset is equivalent to Truncate(0).

func (*Buffer) String added in v0.3.5

func (b *Buffer) String() string

String returns the string representation of the buffer.

It returns `<nil>` the buffer is a nil pointer.

func (*Buffer) Truncate added in v0.3.5

func (b *Buffer) Truncate(n int)

Truncate truncates the Buffer.

Panics if `n > b.Len()`.

This function may free memory by shrinking the internal buffer.

func (*Buffer) Write added in v0.3.5

func (b *Buffer) Write(buf []byte) (int, error)

Write writes the byte slice to the buffer.

func (*Buffer) WriteByte added in v0.3.5

func (b *Buffer) WriteByte(c byte) error

WriteByte writes a single byte to the Buffer.

func (*Buffer) WriteString added in v0.3.5

func (b *Buffer) WriteString(buf string) (int, error)

WriteString writes a string to the buffer.

This function is identical to Write except that it allows one to write a string directly without allocating an intermediate byte slice.

func (*Buffer) WriteTo added in v0.3.6

func (b *Buffer) WriteTo(w io.Writer) (int64, error)

WriteTo copies from the buffer into the given writer until the buffer is empty.

type Pool

type Pool interface {
	GetBytes(size int) []byte
	PutBytes(b []byte)
}
var DefaultPool Pool = &pool{}

type ReverseProxyBuffer

type ReverseProxyBuffer struct{}

func (ReverseProxyBuffer) Get

func (ReverseProxyBuffer) Get() []byte

func (ReverseProxyBuffer) Put

func (ReverseProxyBuffer) Put(b []byte)

Jump to

Keyboard shortcuts

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