Categorygithub.com/hslam/buffer
modulepackage
0.0.0-20230217202846-e7b1b6ebf283
Repository: https://github.com/hslam/buffer.git
Documentation: pkg.go.dev

# README

buffer

PkgGoDev Build Status codecov Go Report Card LICENSE

Package buffer implements a variable-sized bytes pool.

Get started

Install

go get github.com/hslam/buffer

Import

import "github.com/hslam/buffer"

Usage

Simple Example

package main

import (
	"github.com/hslam/buffer"
)

func main() {
	buf := buffer.GetBuffer(1024)
	buffer.PutBuffer(buf)
}

Example

package main

import (
	"github.com/hslam/buffer"
)

func main() {
	buffers := buffer.NewBuffers(1024)
	size := 65536

	buf := buffers.GetBuffer(size)
	buffers.PutBuffer(buf)

	p := buffers.AssignPool(size)
	buf = p.GetBuffer(size)
	p.PutBuffer(buf)
}

Benchmark

go test -bench=. -benchmem -benchtime=10s -timeout 30m

goos: darwin
goarch: amd64
pkg: github.com/hslam/buffer
BenchmarkAssignPool-8        	1000000000	         4.507 ns/op	       0 B/op	       0 allocs/op
BenchmarkAssignSizedPool-8   	699822374	        16.72 ns/op	       0 B/op	       0 allocs/op
BenchmarkBuffers-8           	254406498	        46.63 ns/op	      24 B/op	       1 allocs/op
BenchmarkSizedBuffer-8       	302837005	        39.32 ns/op	      24 B/op	       1 allocs/op
PASS
ok  	github.com/hslam/buffer	51.466s

License

This package is licensed under a MIT license (Copyright (c) 2021 Meng Huang)

Author

buffer was written by Meng Huang.

# Functions

AssignPool assigns a fixed size bytes pool with the given size.
GetBuffer returns a bytes from the pool with the given size.
NewBuffers creates a new Buffers with the given page size.
PutBuffer frees the bytes to the pool.

# Structs

Buffers contains buckets for sharding.
Pool represents a fixed size bytes pool.