buffer

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

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

Go to latest
Published: Feb 17, 2023 License: MIT Imports: 2 Imported by: 3

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.

Documentation

Overview

Package buffer implements a variable-sized bytes pool.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBuffer

func GetBuffer(size int) []byte

GetBuffer returns a bytes from the pool with the given size.

func PutBuffer

func PutBuffer(buf []byte)

PutBuffer frees the bytes to the pool.

Types

type Buffers

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

Buffers contains buckets for sharding.

func NewBuffers

func NewBuffers(pageSize int) *Buffers

NewBuffers creates a new Buffers with the given page size.

func (*Buffers) AssignPool

func (b *Buffers) AssignPool(size int) *Pool

AssignPool assigns a fixed size bytes pool with the given size.

func (*Buffers) GetBuffer

func (b *Buffers) GetBuffer(size int) []byte

GetBuffer returns a bytes from the pool with the given size.

func (*Buffers) PutBuffer

func (b *Buffers) PutBuffer(buf []byte)

PutBuffer frees the bytes to the pool.

type Pool

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

Pool represents a fixed size bytes pool.

func AssignPool

func AssignPool(size int) *Pool

AssignPool assigns a fixed size bytes pool with the given size.

func (*Pool) AlignedSize

func (p *Pool) AlignedSize() int

AlignedSize returns the aligned size.

func (*Pool) GetBuffer

func (p *Pool) GetBuffer(size int) (buf []byte)

GetBuffer returns a bytes from the pool with the given size.

func (*Pool) PutBuffer

func (p *Pool) PutBuffer(buf []byte)

PutBuffer frees the bytes to the pool.

Jump to

Keyboard shortcuts

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