bytespool

package module
v0.0.0-...-fc1005a Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2023 License: MIT Imports: 8 Imported by: 0

README

bytespool

The main idea of this repository is to implement a memory pool mechanism to manage the memory usage of data blocks and avoid the impact of frequent memory allocation and deallocation operations on system performance. The specific implementation method is to divide the data into blocks of a specific size and store them in a linked list. The underlying management of the blocks uses sync.Pool and limits the maximum usage of the memory pool and the maximum memory of each requested Bytes. This way, whenever a new data block is needed, it can be obtained from the memory pool without the need for memory allocation operations every time.

In this repository, Bytes implements the Read and Write methods, which can be used to read and write data blocks. By using the Bytes approach, frequent memory allocation and deallocation operations can be avoided.

Installation

go get github.com/zjj/bytespool

Example

package main

import (
	"crypto/rand"
	"fmt"
	"io/ioutil"
	"log"

	"github.com/zjj/bytespool"
)

func main() {
	i := make([]byte, 1024*1024*2)
	n, err := rand.Read(i)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("generate %d bytes\n", n)

	// create a pool of max 100M, and each size is 4K
	p := bytespool.NewBytesPool(1024*1024*100, 1024*4)

	// get a max 10M bytes from pool
	bytes := p.NewBytes(1024 * 1024 * 10)
	n, err = bytes.Write(i)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("wrote %d bytes\n", n)

	read10bytes := make([]byte, 10)
	n, err = bytes.Read(read10bytes)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("read %d bytes -> read10byte\n", n)

	read10bytes = make([]byte, 10)
	n, err = bytes.Read(read10bytes)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("read %d bytes -> read10byte\n", n)

	rest, err := ioutil.ReadAll(bytes)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("read %d bytes -> rest\n", len(rest))
}

More

If the first argument of bytespool.NewBytesPool is 0, there is no limit to the memory usage of the pool.

If the first argument of NewBytes is 0, there is no limit to the maximum length of the returned Bytes, but this length is subject to the maximum memory limit set by the bytespool.NewBytesPool's first argment if that is not 0.

The Read and Write operations of Bytes may block until there are idle blocks available to return, similar to blocking I/O operations.

License

https://github.com/zjj/bytespool/blob/master/LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBytesCapacityTooSmall = errors.New("bytes capacity is too small")
View Source
var ErrPoolFull = errors.New("pool is full")
View Source
var ErrPoolTooSmall = errors.New("pool is too small")

Functions

This section is empty.

Types

type Bytes

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

func (*Bytes) Free

func (bs *Bytes) Free()

Free all buffers in the list it's not big deal if you forget to call this function gc wibs do it for you

func (*Bytes) Read

func (bs *Bytes) Read(p []byte) (n int, err error)

func (*Bytes) Write

func (bs *Bytes) Write(p []byte) (n int, err error)

type BytesPool

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

BytesPool is a pool of Bytes

func NewBytesPool

func NewBytesPool(maxMemory, segmentSize int) *BytesPool

NewBytesPool create a new BytesPool maxMemory is the threshold value (in byte) of memory the BytesPool could increase, eg. 1024 * 1024 * 1024 (1G) if maxMemory == 0, the capacity of the pool is unLimited segmentSize (byte) is the size of the segment, eg. 1024 * 4, maxMeory / segmentSize is the capacity of the pool which means the max number of the segments in the pool

func (*BytesPool) NewBytes

func (bp *BytesPool) NewBytes(length int) *Bytes

NewBytes create a new Bytes something like []byte length is the max length of the Bytes (in byte) eg, 1024 * 1024 (1M) data in Bytes is a list of []byte, the size of []byte is pool's segmentSize, like 4k so, the max length of the Bytes is bl / segmentSize if length == 0, it is not limited, it may use all of the bytes in the pool

type Pool

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

func (*Pool) Get

func (bp *Pool) Get(ctx context.Context) (*segment, error)

func (*Pool) Put

func (bp *Pool) Put(seg *segment)

Jump to

Keyboard shortcuts

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