bytespool

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2021 License: MIT Imports: 3 Imported by: 2

README

bytespool GoDoc Report card

Package bytespool provides a simple pool implementation for reusing []byte.

go get github.com/humuzhu/bytespool

Usage

buf := bytespool.Acquire(256)
defer bytespool.Release(buf)
// using buf whatever you like

Credits

Documentation

Overview

Package bytespool provides a simple pool implementation for reusing []byte.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/humuzhu/bytespool"
)

func main() {
	buf := bytespool.Acquire(256)
	defer bytespool.Release(buf)

	rd := strings.NewReader("bytespool")
	n, err := rd.Read(buf)
	if err != nil {
		return
	}
	name := string(buf[:n])
	fmt.Println(name)
}
Output:

bytespool

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Acquire

func Acquire(n int) []byte

Acquire retrieves a byte slice of the appropriate length from the global pool (or allocates a new one).

func Release

func Release(bs []byte)

Release returns a byte slice to the global pool.

Types

type Pool

type Pool interface {
	// Acquire retrieves a byte slice of the appropriate length from the pool or
	// allocates a new one.
	Acquire(n int) []byte

	// Release adds buf to the pool.
	Release(buf []byte)
}

Pool is a pool to handle cases of reusing byte slice of varying sizes.

type SyncPool

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

SyncPool maintains 32 internal pools using sync.Pool, for each power of 2 in 1-32.

func NewSyncPool

func NewSyncPool() *SyncPool

NewSyncPool return a SyncPool instance.

func (*SyncPool) Acquire

func (p *SyncPool) Acquire(n int) []byte

Acquire retrieves a byte slice of the appropriate length from the pool or allocates a new one.

func (*SyncPool) Release

func (p *SyncPool) Release(buf []byte)

Release adds buf to the pool.

Jump to

Keyboard shortcuts

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