Involved Source Filesbuffer.go Package pool provides a sync.Pool equivalent that buckets incoming
requests to one of 32 sub-pools, one for each power of 2, 0-32.
import (pool "github.com/libp2p/go-buffer-pool")
var p pool.BufferPool
small := make([]byte, 1024)
large := make([]byte, 4194304)
p.Put(small)
p.Put(large)
small2 := p.Get(1024)
large2 := p.Get(4194304)
fmt.Println("small2 len:", len(small2))
fmt.Println("large2 len:", len(large2))
// Output:
// small2 len: 1024
// large2 len: 4194304writer.go
Package-Level Type Names (total 3)
/* sort by: | */
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. Pool is the buffer pool to use. If nil, this Buffer will use the
global buffer pool. 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. 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. Grow grows the internal buffer such that `n` bytes can be written without
reallocating. Len returns the number of bytes that can be read from this buffer. 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. Read reads at most `len(buf)` bytes from the internal buffer into the given
buffer. ReadByte reads a single byte from the Buffer. ReadFrom reads from the given reader into the buffer. Reset is equivalent to Truncate(0). String returns the string representation of the buffer.
It returns `<nil>` the buffer is a nil pointer. Truncate truncates the Buffer.
Panics if `n > b.Len()`.
This function may free memory by shrinking the internal buffer. Write writes the byte slice to the buffer. WriteByte writes a single byte to the Buffer. 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. WriteTo copies from the buffer into the given writer until the buffer is
empty.
*Buffer : github.com/apache/arrow-go/v18/internal/hashing.ByteSlice
*Buffer : github.com/gobwas/ws.HandshakeHeader
*Buffer : github.com/klauspost/compress/flate.Reader
*Buffer : github.com/miekg/dns.Writer
*Buffer : github.com/quic-go/quic-go/quicvarint.Reader
*Buffer : github.com/quic-go/quic-go/quicvarint.Writer
*Buffer : compress/flate.Reader
*Buffer : expvar.Var
*Buffer : fmt.Stringer
*Buffer : google.golang.org/protobuf/encoding/protodelim.Reader
*Buffer : gorm.io/gorm/clause.Writer
*Buffer : internal/bisect.Writer
*Buffer : io.ByteReader
*Buffer : io.ByteWriter
*Buffer : io.Reader
*Buffer : io.ReaderFrom
*Buffer : io.ReadWriter
*Buffer : io.StringWriter
*Buffer : io.Writer
*Buffer : io.WriterTo
func NewBuffer(buf []byte) *Buffer
func NewBufferString(buf string) *Buffer
BufferPool is a pool to handle cases of reusing elements of varying sizes. It
maintains 32 internal pools, for each power of 2 in 0-32.
You should generally just call the package level Get and Put methods or use
the GlobalPool BufferPool instead of constructing your own.
You MUST NOT copy Pool after using. Get retrieves a buffer of the appropriate length from the buffer pool or
allocates a new one. Get may choose to ignore the pool and treat it as empty.
Callers should not assume any relation between values passed to Put and the
values returned by Get.
If no suitable buffer exists in the pool, Get creates one. Put adds x to the pool.
func github.com/libp2p/go-msgio.NewReaderSizeWithPool(r io.Reader, maxMessageSize int, p *BufferPool) msgio.ReadCloser
func github.com/libp2p/go-msgio.NewReaderWithPool(r io.Reader, p *BufferPool) msgio.ReadCloser
func github.com/libp2p/go-msgio.NewVarintReaderSizeWithPool(r io.Reader, maxMessageSize int, p *BufferPool) msgio.ReadCloser
func github.com/libp2p/go-msgio.NewVarintReaderWithPool(r io.Reader, p *BufferPool) msgio.ReadCloser
func github.com/libp2p/go-msgio.NewVarintWriterWithPool(w io.Writer, p *BufferPool) msgio.WriteCloser
func github.com/libp2p/go-msgio.NewWriterWithPool(w io.Writer, p *BufferPool) msgio.WriteCloser
var GlobalPool *BufferPool
Writer is a buffered writer that returns its internal buffer in a pool when
not in use.Wio.Writer Available returns the amount buffer space available. Buffered returns the amount of data buffered. Close flushes the underlying writer and closes it if it implements the
io.Closer interface.
Note: Close() closes the writer even if Flush() fails to avoid leaking system
resources. If you want to make sure Flush() succeeds, call it first. Flush flushes the write buffer, if any, and returns it to the pool. Size returns the size of the underlying buffer. Write writes the given byte slice to the underlying connection.
Note: Write won't return the write buffer to the pool even if it ends up
being empty after the write. You must call Flush() to do that. WriteByte writes a single byte. WriteRune writes a single rune, returning the number of bytes written. WriteString writes a string, returning the number of bytes written.
*Writer : github.com/apache/thrift/lib/go/thrift.Flusher
*Writer : github.com/gogo/protobuf/proto.Sizer
*Writer : github.com/miekg/dns.Writer
*Writer : github.com/prometheus/common/expfmt.Closer
*Writer : github.com/quic-go/quic-go/quicvarint.Writer
*Writer : github.com/yuin/goldmark/util.BufWriter
*Writer : gorm.io/gorm/clause.Writer
*Writer : internal/bisect.Writer
*Writer : io.ByteWriter
*Writer : io.Closer
*Writer : io.StringWriter
*Writer : io.WriteCloser
*Writer : io.Writer
Package-Level Functions (total 4)
Get retrieves a buffer of the appropriate length from the global buffer pool
(or allocates a new one).
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).
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).
Put returns a buffer to the global buffer pool.
Package-Level Variables (only one)
GlobalPool is a static Pool for reusing byteslices of various sizes.
Package-Level Constants (total 3)
MaxLength is the maximum length of an element that can be added to the Pool.
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.
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.