buffer

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package buffer provides a pooled *[]byte for zero-alloc formatting in hot paths. Consumers acquire a reusable buffer via Get or GetSize and return it via Put so steady-state allocations tend toward zero. A buffer is its caller's from Get until Put and must not be touched after Put. It is a thin byte-slice specialisation over internal/kernel/concur/recycler.CappedPool (ADR 0010): the recycling mechanism lives in recycler, the size classes and the *[]byte type live here.

Size classes (#810, ADR 0216): five pools, of 1, 4, 16, 64 and 256 KiB buffers. GetSize serves a request from the smallest class that fits, and Put routes a buffer by its capacity to the largest class it covers, which keeps it only while its capacity is at most twice the class's size — a buffer grown past that is dropped, so no class pins more than twice what it hands out, and a 1 KiB caller is never handed a 64 KiB buffer another returned.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get() *[]byte

Get borrows a reusable byte buffer with zero length and capacity >= initialCap.

func GetSize added in v0.28.0

func GetSize(n int) *[]byte

GetSize borrows a reusable byte buffer with zero length and capacity at least n, from the smallest class that fits: 1, 4, 16, 64 or 256 KiB. A buffer is never smaller than its class. Above 256 KiB the buffer is allocated to measure, and Put drops it unless its capacity is at most 512 KiB. An n of zero or less is the smallest class.

func Put

func Put(b *[]byte)

Put returns a buffer to the pool after use: routed by its capacity to the largest class it covers, its length truncated, and dropped when its capacity is past twice that class's size or below the smallest class. A nil argument is a safe no-op so call sites can always defer Put — the generic CappedPool cannot nil-check a pointer-like T, so the guard lives here.

Put ends the caller's ownership of b. Once it returns, neither *b nor the bytes it points at may be read or written — not even len(*b) — and b must not be Put again: the pool may already have handed b to another goroutine, which writes both as its own. Copy out whatever is still needed before Put.

Types

This section is empty.

Jump to

Keyboard shortcuts

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