syncpool

package module
v0.0.0-...-21441ea Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2025 License: MIT Imports: 7 Imported by: 2

README

syncpool

In Golang if a massive amount of objects are allocated repeatedly, It causes a huge workload of GC. Golang provides sync.Pool to decrease the allocations and GC workload. But sync.Pool is low level package and its not easy to use. syncpool package is a wrapper on sync.pool and some other related packages that provides a high level easy solution to solve the problem without engaging with low level codes. Briefly syncpool can provide a temp pool of any arbitrary objects easily. As well as it provides a slice of any objects. In a especially case it provides SlicePool that can be used in many applications. for example we almost need BufferPool always in most of applications.

Usage

syncpool provides a generic pool of any entity easily.

var MyString string
var myStringPool = syncpool.NewPool[MyString]()
s:=myStringPool.Get()
s="something"
print(s)
myStringPool.Put(s)

default pools

for more conviniency syncpool defines some traditional global pool. the following shows defaults pools.

var IntPool = NewPool[int]()
var BytePool = NewPool[byte]()
var ErrorPool = NewPool[error]()
var StringPool = NewPool[string]()

var IntSlicePool SlicePool[int]
var ByteSlicePool SlicePool[byte]
var ErrorSlicePool SlicePool[error]
var StringSlicePool SlicePool[string]

SlicePool

Whenever we want to handle a slice objects instead a singular object we should use of SlicePool. For example if we have to hold a group of []byte, BufferPool can be best candidate.

Bechmarks

Benchmarks shows the main reason that why we should use of syncpool and how it can prevent memeory managements challenge in Golang.

BenchmarkJsonEncoderWithPool-4          42861     27023 ns/op    0    B/op    0    allocs/op
BenchmarkJsonMarshalWithoutPool-4       36492     29455 ns/op    4800 B/op    100  allocs/op
BenchmarkPool-4                         51396350  23.10 ns/op    0    B/op    0    allocs/op
BenchmarkNoPool-4                       19882788  59.78 ns/op    24   B/op    1    allocs/op

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BytePool = NewPool[byte]()
View Source
var ErrorPool = NewPool[error]()
View Source
var IntPool = NewPool[int]()
View Source
var StringPool = NewPool[string]()

Functions

This section is empty.

Types

type Buffer

type Buffer struct{ *zapbuffer.Buffer }

func (*Buffer) Read

func (b *Buffer) Read(bs []byte) (n int, err error)

type BufferPool

type BufferPool zapbuffer.Pool

func NewBufferPool

func NewBufferPool() BufferPool

func (BufferPool) Get

func (bp BufferPool) Get(size int) Buffer

type Pool

type Pool[V any] sync.Pool

func NewPool

func NewPool[V any]() *Pool[V]

func (*Pool[V]) Get

func (p *Pool[V]) Get() *V

func (*Pool[V]) Put

func (p *Pool[V]) Put(v *V)

type SlicePool

type SlicePool[T any] struct {
	// contains filtered or unexported fields
}

base refrence: https://github.com/panjf2000/gnet/blob/v1.6.7/pkg/pool/byteslice/byteslice.go

var ByteSlicePool SlicePool[byte]
var ErrorSlicePool SlicePool[error]
var IntSlicePool SlicePool[int]
var StringSlicePool SlicePool[string]

func (*SlicePool[T]) Get

func (p *SlicePool[T]) Get(size int) (buf []T)

func (*SlicePool[T]) Put

func (p *SlicePool[T]) Put(buf []T)

Jump to

Keyboard shortcuts

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