bufferpool

package
v3.0.1-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 2 Imported by: 4

Documentation

Overview

Package bufferpool supports object pooling for byte buffers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool struct {
	MinAlloc, BucketCount int
	// contains filtered or unexported fields
}

Pool maintains a list of BucketCount buckets that contain buffers of exponentially-increasing capacity, 1 << 0 to 1 << BucketCount.

The MinAlloc field specifies the minimum capacity of new buffers allocated by Pool, which improves reuse of small buffers. For the avoidance of doubt: calls to Get() with size < MinAlloc return a buffer of len(buf) = size and cap(buf) >= MinAlloc. MinAlloc MUST NOT exceed 1 << BucketCount, or method calls to Pool will panic.

The zero-value Pool is ready to use, defaulting to BucketCount=20 and MinAlloc=1024 (max size = ~1MiB). Most applications will not benefit from tuning these parameters.

As a general rule, increasing MinAlloc reduces GC latency at the expense of increased memory usage. Increasing BucketCount can reduce GC latency in applications that frequently allocate large buffers.

var Default Pool

A default global pool.

func (*Pool) Get

func (p *Pool) Get(size int) []byte

Get a buffer of len(buf) == size and cap >= size.

func (*Pool) Put

func (p *Pool) Put(buf []byte)

Put returns the buffer to the pool. The first len(buf) bytes of the buffer are zeroed.

Jump to

Keyboard shortcuts

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