codecbuf

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2025 License: Unlicense Imports: 2 Imported by: 0

README

Codecbuf - Concurrent-Safe Bytes Buffer Pool

This package provides a concurrent-safe pool of bytes.Buffer objects for encoding data. It helps reduce memory allocations and improve performance by reusing buffers instead of creating new ones for each operation.

Usage

Basic Usage
// Get a buffer from the default pool
buf := codecbuf.Get()

// Use the buffer
buf.WriteString("Hello, World!")
// ... do more operations with the buffer ...

// Return the buffer to the pool when done
codecbuf.Put(buf)
Using with defer
func ProcessData() {
    // Get a buffer from the default pool
    buf := codecbuf.Get()
    
    // Return the buffer to the pool when the function exits
    defer codecbuf.Put(buf)
    
    // Use the buffer
    buf.WriteString("Hello, World!")
    // ... do more operations with the buffer ...
}
Creating a Custom Pool
// Create a new buffer pool
pool := codecbuf.NewPool()

// Get a buffer from the custom pool
buf := pool.Get()

// Use the buffer
buf.WriteString("Hello, World!")

// Return the buffer to the custom pool
pool.Put(buf)

Performance

Using a buffer pool can significantly improve performance in applications that frequently create and use byte buffers, especially in high-throughput scenarios. The pool reduces garbage collection pressure by reusing buffers instead of allocating new ones.

Thread Safety

The buffer pool is safe for concurrent use by multiple goroutines. However, individual buffers obtained from the pool should not be used concurrently by multiple goroutines without additional synchronization.

Documentation

Overview

Package codecbuf provides a concurrent-safe bytes buffer pool for encoding data.

Index

Constants

This section is empty.

Variables

View Source
var DefaultPool = NewPool()

DefaultPool is the default buffer pool for the application.

Functions

func Get

func Get() *bytes.Buffer

Get returns a buffer from the default pool.

func Put

func Put(buf *bytes.Buffer)

Put returns a buffer to the default pool after zeroing its bytes for security.

Types

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool is a concurrent-safe pool of bytes.Buffer objects.

func NewPool

func NewPool() *Pool

NewPool creates a new buffer pool.

func (*Pool) Get

func (p *Pool) Get() *bytes.Buffer

Get returns a buffer from the pool or creates a new one if the pool is empty.

func (*Pool) Put

func (p *Pool) Put(buf *bytes.Buffer)

Put returns a buffer to the pool after zeroing its bytes for security and resetting it.

Jump to

Keyboard shortcuts

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