bytebufferpool

repository·master·Indexed 23 days ago

https://github.com/valyala/bytebufferpool

A high-performance byte buffer pool implementation for Go designed to minimize memory waste and maximize speed. It provides a ByteBuffer type that implements io.ReaderFrom, io.WriterTo, and io.Writer, along with a Pool mechanism that automatically calibrates default and maximum buffer sizes based on workload patterns to reduce memory allocations and GC overhead.

Tokens
905
Snippets
0
Records
12
Agent score
78%

What's inside bytebufferpool

  1. Overview of bytebufferpool

    master

    bytebufferpool is a high-performance implementation of a pool of byte buffers designed for Go applications. Its primary goal is to provide efficient buffer reuse while including anti-memory-waste protection.

    Note that the pool may waste a limited amount of memory due to fragmentation; this amount is approximately equal to the maximum total size of the byte buffers currently in concurrent use.

  2. Use ByteBuffer to minimize memory allocations

    master
    The ByteBuffer type provides a byte buffer designed for append-like workloads, helping to minimize memory allocations. It is intended to be used with functions that append data to a []byte slice. You should obtain an empty buffer using Get (from the package level) rather than manual instantiation to leverage the pool.
  3. Use the default byte buffer pool

    master

    The bytebufferpool package provides a global defaultPool for easy access to byte buffer pooling without manual instantiation. Use Get() to acquire a *ByteBuffer and Put() to return it to the pool. This pattern reduces memory allocations and GC overhead.

    Important: Once a *ByteBuffer is returned to the pool via Put(), you must not access its underlying byte slice (B) again, as this will cause data races.

  4. Create and use a custom Pool

    master

    If you need to manage distinct types of byte buffers separately to reduce memory waste, you can instantiate your own Pool. A custom pool allows you to control the lifecycle of buffers specific to a certain workload.

    Methods:

    • Get(): Returns an empty *ByteBuffer. If the pool is empty, it allocates a new one with a capacity based on the pool's current defaultSize.
    • Put(b *ByteBuffer): Returns the buffer to the pool. The pool automatically calibrates its defaultSize and maxSize based on the sizes of buffers being returned to optimize for your specific usage patterns.
  5. Reset or Set ByteBuffer contents

    master

    To reuse a ByteBuffer without reallocating, use these methods:

    • Reset(): Makes the buffer empty by setting the length to 0 (retains capacity).
    • Set(p []byte): Sets the buffer content to the provided slice p (retains capacity).
    • SetString(s string): Sets the buffer content to the provided string s (retains capacity).
  6. Write data to ByteBuffer

    master

    The ByteBuffer provides several ways to append data, making it compatible with bytes.Buffer patterns:

    • Write(p []byte) (int, error): Appends the byte slice p to the buffer. Implements io.Writer.
    • WriteString(s string) (int, error): Appends the string s to the buffer.
    • WriteByte(c byte) error: Appends a single byte c to the buffer.
  7. The Pool type

    master

    The Pool struct manages a collection of *ByteBuffer objects using an internal sync.Pool. It features an automatic calibration mechanism that tracks the frequency of different buffer sizes being used.

    Over time, the pool adjusts its defaultSize (the capacity of newly allocated buffers) and maxSize (the threshold below which buffers are kept in the pool) to match the actual workload, minimizing both allocation frequency and memory waste.