GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - zjj/bytespool

BranchesTags
Open more actions menu

Latest commit

 

History

9 Commits
9 Commits

Folders and files

NameName
Last commit message
Last commit date
LICENSE
LICENSE
 
 
README.md
README.md
 
 
bytes.go
bytes.go
 
 
bytes_test.go
bytes_test.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
pool.go
pool.go
 
 
utils.go
utils.go
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.