pool

package
v0.0.0-...-052ef2a Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: MIT Imports: 13 Imported by: 0

README

Pool

Drop in replacement for sync.Pool that has zero allocations for Get and Put internally.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNeedAllocFunc = errors.New("need alloc func")
)

Functions

func Alloc

func Alloc(size int) []byte

func AllocCap

func AllocCap(size, capacity int) []byte

func AllocZeroed

func AllocZeroed(size int) []byte

func AllocZeroedCap

func AllocZeroedCap(size, capacity int) []byte

func Free

func Free(b []byte)

Types

type AllocFunc

type AllocFunc[T any] func() unsafe.Pointer

type ByteSlices

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

func DefaultBytes

func DefaultBytes() *ByteSlices

func NewByteSlices

func NewByteSlices(sizes *SizeClasses) *ByteSlices

func (*ByteSlices) Alloc

func (b *ByteSlices) Alloc(size int) []byte

func (*ByteSlices) AllocCap

func (b *ByteSlices) AllocCap(size, capacity int) []byte

func (*ByteSlices) AllocZeroed

func (b *ByteSlices) AllocZeroed(size int) []byte

func (*ByteSlices) AllocZeroedCap

func (b *ByteSlices) AllocZeroedCap(size, capacity int) []byte

func (*ByteSlices) Free

func (b *ByteSlices) Free(data []byte)

type CircleBuf

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

func NewCircleBuf

func NewCircleBuf(capacity int) *CircleBuf

func (*CircleBuf) Cap

func (c *CircleBuf) Cap() int

func (*CircleBuf) Dequeue

func (c *CircleBuf) Dequeue() unsafe.Pointer

func (*CircleBuf) DequeueUnsafe

func (c *CircleBuf) DequeueUnsafe() unsafe.Pointer

func (*CircleBuf) Enqueue

func (c *CircleBuf) Enqueue(v unsafe.Pointer) bool

func (*CircleBuf) EnqueueUnsafe

func (c *CircleBuf) EnqueueUnsafe(v unsafe.Pointer) bool

func (*CircleBuf) Len

func (c *CircleBuf) Len() int64

type Config

type Config[T any] struct {
	SizeClass, NumShards    int
	PageSize, PagesPerShard int64
	AllocFunc[T]
	DeallocFunc[T]
	InitFunc[T]
	DeInitFunc[T]
}

func (*Config[T]) Validate

func (c *Config[T]) Validate()

type DeInitFunc

type DeInitFunc[T any] func(pointer unsafe.Pointer)

type DeallocFunc

type DeallocFunc[T any] func(pointer unsafe.Pointer)

type InitFunc

type InitFunc[T any] func(pointer unsafe.Pointer)

type Pool

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

func NewPool

func NewPool[T any](
	config Config[T],
) *Pool[T]

func (*Pool[T]) Get

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

func (*Pool[T]) GetUnsafe

func (p *Pool[T]) GetUnsafe() unsafe.Pointer

func (*Pool[T]) Len

func (p *Pool[T]) Len() int

func (*Pool[T]) Put

func (p *Pool[T]) Put(data *T)

func (*Pool[T]) PutUnsafe

func (p *Pool[T]) PutUnsafe(data unsafe.Pointer)

func (*Pool[T]) Shard

func (p *Pool[T]) Shard() *Shard[T]

func (*Pool[T]) Shards

func (p *Pool[T]) Shards() []Shard[T]

func (*Pool[T]) SizeClass

func (p *Pool[T]) SizeClass() int

type RC

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

func (*RC[T]) Clone

func (rc *RC[T]) Clone() *RC[T]

func (*RC[T]) Release

func (rc *RC[T]) Release()

func (*RC[T]) Value

func (rc *RC[T]) Value() T

type Shard

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

func (*Shard[T]) GetUnsafe

func (s *Shard[T]) GetUnsafe() unsafe.Pointer

func (*Shard[T]) Len

func (s *Shard[T]) Len() int

func (*Shard[T]) Pool

func (s *Shard[T]) Pool() *Pool[T]

func (*Shard[T]) PutUnsafe

func (s *Shard[T]) PutUnsafe(data unsafe.Pointer)

func (*Shard[T]) SizeClass

func (s *Shard[T]) SizeClass() int

func (*Shard[T]) TryPutUnsafe

func (s *Shard[T]) TryPutUnsafe(data unsafe.Pointer) bool

type ShardStats

type ShardStats struct {
	Allocates   counter.Counter
	Allocates2  counter.Counter
	Deallocates counter.Counter
}

type SizeClass

type SizeClass struct {
	Size     int64
	PageSize int64
}

SizeClass configures the maximum number of pages and the size of each page.

func (*SizeClass) IsActive

func (sc *SizeClass) IsActive() bool

type SizeClasses

type SizeClasses struct {
	Size8    SizeClass
	Size16   SizeClass
	Size32   SizeClass
	Size64   SizeClass
	Size128  SizeClass
	Size256  SizeClass
	Size512  SizeClass
	Size1K   SizeClass
	Size2K   SizeClass
	Size4K   SizeClass
	Size8K   SizeClass
	Size16K  SizeClass
	Size32K  SizeClass
	Size64K  SizeClass
	Size128K SizeClass
	Size256K SizeClass
	Size512K SizeClass
	Size1M   SizeClass
	Size2M   SizeClass
	Size4M   SizeClass
	Size8M   SizeClass
	Size16M  SizeClass
	Size32M  SizeClass
	Size64M  SizeClass
	Size128M SizeClass
	Size256M SizeClass
	Size512M SizeClass
	Size1G   SizeClass
}

SizeClasses are power of 2 up to 1G Pool is unlikely the ideal choice for sizes larger than 8kb. Best use cases are a lot of smallish allocations and frees with high contention.

func DefaultSizeClasses

func DefaultSizeClasses() *SizeClasses

type Slices

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

func NewSlices

func NewSlices[T any](config *Config[[]T], sizes *SizeClasses) *Slices[T]

func (*Slices[T]) Get

func (s *Slices[T]) Get(size int) []T

func (*Slices[T]) PoolOf

func (s *Slices[T]) PoolOf(size int) *Pool[[]T]

func (*Slices[T]) Put

func (s *Slices[T]) Put(item []T)

func (*Slices[T]) PutZeroed

func (s *Slices[T]) PutZeroed(item []T)

func (*Slices[T]) Slab

func (s *Slices[T]) Slab(size int) SlicesSlab[T]

type SlicesSlab

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

func (SlicesSlab[T]) Get

func (s SlicesSlab[T]) Get() []T

func (SlicesSlab[T]) Put

func (s SlicesSlab[T]) Put(data []T)

func (SlicesSlab[T]) PutZeroed

func (s SlicesSlab[T]) PutZeroed(data []T)

type Stats

type Stats struct {
	Allocs            counter.Counter
	Allocs2           counter.Counter
	Deallocs          counter.Counter
	PageAllocs        counter.Counter
	PageAllocAttempts counter.Counter
	PageDeallocs      counter.Counter
}

Jump to

Keyboard shortcuts

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