bytespool

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2024 License: MIT Imports: 6 Imported by: 0

README

💫 BytesPool

Reuse used byte slices to achieve zero allocation.

The existing byte slices are stored in groups according to the capacity length range, and suitable byte slice objects are automatically allocated according to the capacity length when used.

For more functions, please use: https://github.com/fufuok/bytespool

✨ Features

  • Get byte slices always succeed without panic.
  • Optional length of 0 or fixed-length byte slices.
  • Automatic garbage collection of big-byte slices.
  • High performance, See: Benchmarks.

⚙️ Installation

go get -u github.com/fufuok/utils

📚 Examples

⚡️ Quickstart
package main

import (
	"fmt"

	"github.com/fufuok/utils/pools/bytespool"
)

func main() {
	// Get() is the same as New()
	bs := bytespool.Get(1024)
	// len: 1024, cap: 1024
	fmt.Printf("len: %d, cap: %d\n", len(bs), cap(bs))

	// Put() is the same as Release(), Put it back into the pool after use
	bytespool.Put(bs)

	// len: 0, capacity: 8 (Specified capacity)
	bs = bytespool.Make(8)
	bs = append(bs, "abc"...)
	// len: 3, cap: 8
	fmt.Printf("len: %d, cap: %d\n", len(bs), cap(bs))
	ok := bytespool.Release(bs)
	// true
	fmt.Println(ok)

	// len: 8, capacity: 8 (Fixed length)
	bs = bytespool.New(8)
	copy(bs, "12345678")
	// len: 8, cap: 8, value: 12345678
	fmt.Printf("len: %d, cap: %d, value: %s\n", len(bs), cap(bs), bs)
	bytespool.Release(bs)

	// Output:
	// len: 1024, cap: 1024
	// len: 3, cap: 8
	// true
	// len: 8, cap: 8, value: 12345678
}
⏳ Automated reuse
package main

import (
	"fmt"

	"github.com/fufuok/utils/pools/bytespool"
)

func main() {
	// len: 0, cap: 4 (Specified capacity, automatically adapt to the capacity scale)
	bs3 := bytespool.Make(3)

	bs3 = append(bs3, "123"...)
	fmt.Printf("len: %d, cap: %d, %s\n", len(bs3), cap(bs3), bs3)

	bytespool.Release(bs3)

	// len: 4, cap: 4 (Fixed length)
	bs4 := bytespool.New(4)

	// Reuse of bs3
	fmt.Printf("same array: %v\n", &bs3[0] == &bs4[0])
	// Contain old data
	fmt.Printf("bs3: %s, bs4: %s\n", bs3, bs4[:3])

	copy(bs4, "xy")
	fmt.Printf("len: %d, cap: %d, %s\n", len(bs4), cap(bs4), bs4[:3])

	bytespool.Release(bs4)

	// Output:
	// len: 3, cap: 4, 123
	// same array: true
	// bs3: 123, bs4: 123
	// len: 4, cap: 4, xy3
}
🛠 SetMaxSize
package main

import (
	"fmt"

	"github.com/fufuok/utils/pools/bytespool"
)

func main() {
	maxSize := 4096
	bytespool.SetMaxSize(maxSize)

	bs := bytespool.Make(10)
	fmt.Printf("len: %d, cap: %d\n", len(bs), cap(bs))
	bytespool.Release(bs)

	bs = bytespool.Make(maxSize)
	fmt.Printf("len: %d, cap: %d\n", len(bs), cap(bs))
	bytespool.Release(bs)

	bs = bytespool.New(maxSize + 1)
	fmt.Printf("len: %d, cap: %d\n", len(bs), cap(bs))
	ok := bytespool.Release(bs)
	fmt.Printf("Discard: %v\n", !ok)

	// Output:
	// len: 0, cap: 16
	// len: 0, cap: 4096
	// len: 4097, cap: 4097
	// Discard: true
}

🤖 Benchmarks

go test -run=^$ -benchmem -benchtime=1s -count=2 -bench=.
goos: linux
goarch: amd64
pkg: github.com/fufuok/utils/pools/bytespool
cpu: Intel(R) Xeon(R) Gold 6151 CPU @ 3.00GHz
BenchmarkCapacityPools/New-4            60133353                19.34 ns/op            0 B/op          0 allocs/op
BenchmarkCapacityPools/New-4            61206177                19.49 ns/op            0 B/op          0 allocs/op
BenchmarkCapacityPools/Make-4           61580971                19.58 ns/op            0 B/op          0 allocs/op
BenchmarkCapacityPools/Make-4           61389439                19.71 ns/op            0 B/op          0 allocs/op
BenchmarkCapacityPools/New.Parallel-4           240337632                5.041 ns/op           0 B/op          0 allocs/op
BenchmarkCapacityPools/New.Parallel-4           235125742                5.133 ns/op           0 B/op          0 allocs/op
BenchmarkCapacityPools/Make.Parallel-4          229302106                5.073 ns/op           0 B/op          0 allocs/op
BenchmarkCapacityPools/Make.Parallel-4          238298523                5.308 ns/op           0 B/op          0 allocs/op

ff

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(buf []byte, elems ...byte) []byte

func AppendString

func AppendString(buf []byte, elems string) []byte

func Get

func Get(size int) []byte

func Make

func Make(size int) []byte

func New

func New(size int) []byte

func NewBytes added in v0.9.2

func NewBytes(bs []byte) []byte

func NewString added in v0.9.2

func NewString(s string) []byte

func Put

func Put(buf []byte)

func Release

func Release(buf []byte) bool

func SetMaxSize

func SetMaxSize(size int) bool

SetMaxSize 设置回收时允许的最大字节

Types

type CapacityPools

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

func (*CapacityPools) Append

func (p *CapacityPools) Append(buf []byte, elems ...byte) []byte

Append 与内置的 append 功能相同, 当底层数组需要重建时, 会回收原数组

func (*CapacityPools) AppendString

func (p *CapacityPools) AppendString(buf []byte, elems string) []byte

func (*CapacityPools) Get

func (p *CapacityPools) Get(size int) []byte

Get 完全同 New()

func (*CapacityPools) Make

func (p *CapacityPools) Make(capacity int) []byte

Make 返回 len 为 0, cap >= 给定值的 []byte

func (*CapacityPools) New

func (p *CapacityPools) New(size int) (buf []byte)

New 返回指定长度的 []byte 注:

  1. 返回的 buf != nil
  2. 由于复用底层数组, buf 可能残留有旧数据

func (*CapacityPools) NewBytes added in v0.9.2

func (p *CapacityPools) NewBytes(bs []byte) []byte

NewBytes returns a byte slice of the specified content.

func (*CapacityPools) NewString added in v0.9.2

func (p *CapacityPools) NewString(s string) []byte

NewString returns a byte slice of the specified content.

func (*CapacityPools) Put

func (p *CapacityPools) Put(buf []byte)

Put 同 Release(), 不返回是否回收成功

func (*CapacityPools) Release

func (p *CapacityPools) Release(buf []byte) bool

Jump to

Keyboard shortcuts

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