bytebufferpool

package
v0.0.0-...-38575d5 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: AGPL-3.0, MIT Imports: 8 Imported by: 8

README

Build Status GoDoc Go Report

bytebufferpool

An implementation of a pool of byte buffers with anti-memory-waste protection.

The pool may waste limited amount of memory due to fragmentation. This amount equals to the maximum total size of the byte buffers in concurrent use.

Benchmark results

Currently bytebufferpool is fastest and most effective buffer pool written in Go.

You can find results here.

bytebufferpool users

Documentation

Overview

Package bytebufferpool implements a pool of byte buffers with anti-fragmentation protection.

The pool may waste limited amount of memory due to fragmentation. This amount equals to the maximum total size of the byte buffers in concurrent use.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuffStats

func BuffStats() map[string]interface{}

func CleanupIdleCachedBytesBuffer

func CleanupIdleCachedBytesBuffer()

func DumpBufferItemSize

func DumpBufferItemSize() map[string]interface{}

func Put

func Put(tag string, b *ByteBuffer)

Put returns byte buffer to the pool.

ByteBuffer.B mustn't be touched after returning it to the pool. Otherwise data races will occur.

func PutWithoutReset

func PutWithoutReset(tag string, b *ByteBuffer)

func SetMaxBufferCount

func SetMaxBufferCount(tag string, size uint32)

func SetMaxBufferSize

func SetMaxBufferSize(tag string, size uint32)

Types

type ByteBuffer

type ByteBuffer struct {
	LastAccess time.Time
	Used       uint32
	ID         uint32
	// B is a byte buffer to use in append-like workloads.
	// See example code for details.
	B []byte
	// contains filtered or unexported fields
}

ByteBuffer provides byte buffer, which can be used for minimizing memory allocations.

ByteBuffer may be used with functions appending data to the given []byte slice. See example code for details.

Use Get for obtaining an empty byte buffer.

Example
package main

import (
	"fmt"

	"github.com/rubyniu105/framework/lib/bytebufferpool"
)

func main() {
	bb := bytebufferpool.Get("test")

	bb.WriteString("first line\n")
	bb.Write([]byte("second line\n"))
	bb.B = append(bb.B, "third line\n"...)

	fmt.Printf("bytebuffer contents=%q", bb.B)

	// It is safe to release byte buffer now, since it is
	// no longer used.
	bytebufferpool.Put("test", bb)
}
Output:

func Get

func Get(tag string) *ByteBuffer

Get returns an empty byte buffer from the pool.

Got byte buffer may be returned to the pool via Put call. This reduces the number of memory allocations required for byte buffer management.

func (*ByteBuffer) Bytes

func (b *ByteBuffer) Bytes() []byte

Bytes returns b.B, i.e. all the bytes accumulated in the buffer.

The purpose of this function is bytes.Buffer compatibility.

func (*ByteBuffer) Cap

func (b *ByteBuffer) Cap() int

func (*ByteBuffer) GrowTo

func (b *ByteBuffer) GrowTo(n int)

func (*ByteBuffer) Len

func (b *ByteBuffer) Len() int

Len returns the size of the byte buffer.

func (*ByteBuffer) ReadFrom

func (b *ByteBuffer) ReadFrom(r io.Reader) (int64, error)

ReadFrom implements io.ReaderFrom.

The function appends all the data read from r to b.

func (*ByteBuffer) Reset

func (b *ByteBuffer) Reset()

Reset makes ByteBuffer.B empty.

func (*ByteBuffer) Set

func (b *ByteBuffer) Set(p []byte)

Set sets ByteBuffer.B to p.

func (*ByteBuffer) SetString

func (b *ByteBuffer) SetString(s string)

SetString sets ByteBuffer.B to s.

func (*ByteBuffer) String

func (b *ByteBuffer) String() string

String returns string representation of ByteBuffer.B.

func (*ByteBuffer) Write

func (b *ByteBuffer) Write(p []byte) (int, error)

Write implements io.Writer - it appends p to ByteBuffer.B

func (*ByteBuffer) WriteByte

func (b *ByteBuffer) WriteByte(c byte) error

WriteByte appends the byte c to the buffer.

The purpose of this function is bytes.Buffer compatibility.

The function always returns nil.

func (*ByteBuffer) WriteBytesArray

func (b *ByteBuffer) WriteBytesArray(ps ...[]byte) (count int, err error)

func (*ByteBuffer) WriteString

func (b *ByteBuffer) WriteString(s string) (int, error)

WriteString appends s to ByteBuffer.B.

func (*ByteBuffer) WriteTo

func (b *ByteBuffer) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo.

type ItemMap

type ItemMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewItemMap

func NewItemMap() *ItemMap

func (*ItemMap) Delete

func (this *ItemMap) Delete(id uint32)

func (*ItemMap) Range

func (this *ItemMap) Range(f func(key uint32, value *ByteBuffer) bool)

func (*ItemMap) Store

func (this *ItemMap) Store(id uint32, buffer *ByteBuffer)

type ObjectPool

type ObjectPool struct {
	New func() interface{}
	// contains filtered or unexported fields
}

func NewObjectPool

func NewObjectPool(tag string, allocateFunc func() interface{}, returnCallback func() interface{}, maxItems, maxBytes int) *ObjectPool

func (*ObjectPool) Get

func (p *ObjectPool) Get() interface{}

func (*ObjectPool) Put

func (p *ObjectPool) Put(o interface{})

type Pool

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

Pool represents byte buffer pool.

Distinct pools may be used for distinct types of byte buffers. Properly determined byte buffer types with their own pools may help reducing memory waste.

func NewTaggedPool

func NewTaggedPool(tag string, defaultSize, maxSize uint32, maxItems uint32) *Pool

func (*Pool) Drop

func (p *Pool) Drop(b *ByteBuffer)

func (*Pool) Get

func (p *Pool) Get() *ByteBuffer

Get returns new byte buffer with zero length.

The byte buffer may be returned to the pool via Put after the use in order to minimize GC overhead.

func (*Pool) Put

func (p *Pool) Put(b *ByteBuffer)

Jump to

Keyboard shortcuts

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