objectpool

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2025 License: MIT Imports: 7 Imported by: 7

README

objectpool

Generic Object Pool

Usage


type Struct struct {
    Name string
}

// a = &Struct{}
a:=objectpool.Get[Struct]()
defer objectpool.Put(a)

// len(s) == 0,  cap(s) == 16
// s = make([]Struct, 0, 16)
s:=objectpool.GetSlice[Struct](10)
defer objectpool.PutSlice(s)

// len(s) == 0,  cap(s) == 128
s1 =objectpool.GetSlice[Struct](127)
defer objectpool.PutSlice(s1)

// m = map[int]int{}
m:= objectpool.GetMap[int,int]()
defer objectpool.PutMap(m)

// Get Sync.Pool of Type
objectpool.GetTypePool[T]() == &sync.Pool{ New: func() any { return new(T) }}
objectpool.GetSliceTypePool[T]() == &sync.Pool{ New: func() any { return make([]T, 0, 16) }}
objectpool.GetTypePool[K,V]() == &sync.Pool{ New: func() any { return make(map[K]V,16) }}

benchmark

goos: windows
goarch: amd64
pkg: github.com/ravinggo/objectpool
cpu: AMD Ryzen 5 5600 6-Core Processor
BenchmarkGetPut
BenchmarkGetPut-12              	66342636	        18.35 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetSlicePutSlice
BenchmarkGetSlicePutSlice-12    	57361924	        21.53 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetMapPutMap
BenchmarkGetMapPutMap-12        	70080768	        17.43 ns/op	       0 B/op	       0 allocs/op
BenchmarkMallocgc
BenchmarkMallocgc-12            	46533090	        21.80 ns/op	       8 B/op	       1 allocs/op
BenchmarkMallocgcSlice
BenchmarkMallocgcSlice-12       	31850175	        35.72 ns/op	     128 B/op	       1 allocs/op
BenchmarkMallocgcMap
BenchmarkMallocgcMap-12         	 7050296	       161.4 ns/op	     688 B/op	       2 allocs/op
BenchmarkSyncPool
BenchmarkSyncPool-12            	100000000	        10.54 ns/op	       0 B/op	       0 allocs/op
BenchmarkSyncPoolSlice
BenchmarkSyncPoolSlice-12       	33001575	        35.11 ns/op	      24 B/op	       1 allocs/op
BenchmarkSyncPoolMap
BenchmarkSyncPoolMap-12         	100000000	        10.66 ns/op	       0 B/op	       0 allocs/op
PASS

We welcome suggestions for optimization. We really can't optimize anymore.

Documentation

Index

Constants

View Source
const (
	KindMask = (1 << 5) - 1
)

Variables

This section is empty.

Functions

func Get

func Get[T any]() *T

Get a object from object pool with T

func GetMap

func GetMap[K comparable, V any]() map[K]V

GetMap get a map from object pool with K and V

func GetMapPtr

func GetMapPtr[K comparable, V any]() uintptr

GetMapPtr get type pointer of map[K]V

func GetMapTypePool added in v0.0.3

func GetMapTypePool[K comparable, V any]() *sync.Pool

GetMapTypePool get a sync.Pool with map[K]V

func GetPtr

func GetPtr[T any]() uintptr

GetPtr get type pointer of T

func GetPtrAndIndex added in v0.0.2

func GetPtrAndIndex[T any]() (uintptr, int)

func GetPtrAny

func GetPtrAny(a any) uintptr

GetPtrAny get type pointer of param a

func GetPtrAnyAndIndex added in v0.0.2

func GetPtrAnyAndIndex(a any) (uintptr, int)

func GetSlice

func GetSlice[T any](cap int) []T

GetSlice get a slice from object pool with T,len() == 0

func GetSlice2 added in v0.0.3

func GetSlice2[T any](s *SlicePool, cap int) []T

func GetSliceForSize

func GetSliceForSize[T any](size int) []T

GetSliceForSize get a slice from object pool with T and size, len() == size

func GetSliceForSize2 added in v0.0.3

func GetSliceForSize2[T any](sp *SlicePool, size int) []T

func GetTypePool

func GetTypePool[T any]() *sync.Pool

GetTypePool get a sync.Pool with T

func Put

func Put[T any](t *T)

Put a object to object pool with T

func PutBytes

func PutBytes(b Bytes)

func PutMap

func PutMap[K comparable, V any](t map[K]V)

PutMap put a map to object pool with K and V

func PutSlice

func PutSlice[T any](t []T)

PutSlice put a slice to object pool with T

func PutSliceClear

func PutSliceClear[T any](t []T)

Types

type Bytes

type Bytes []byte

Bytes is a slice object pool for byte

func GetBytes

func GetBytes(cap int) Bytes

func (*Bytes) Bytes

func (b *Bytes) Bytes() []byte

func (*Bytes) Cap

func (b *Bytes) Cap() int

func (*Bytes) Len

func (b *Bytes) Len() int

func (*Bytes) Reset

func (b *Bytes) Reset()

func (*Bytes) String

func (b *Bytes) String() string

func (*Bytes) WriteBool

func (b *Bytes) WriteBool(v bool)

func (*Bytes) WriteBytes

func (b *Bytes) WriteBytes(c ...byte)

func (*Bytes) WriteFloat

func (b *Bytes) WriteFloat(f float64)

func (*Bytes) WriteInt

func (b *Bytes) WriteInt(i int64)

func (*Bytes) WriteString

func (b *Bytes) WriteString(s string)

func (*Bytes) WriteUint

func (b *Bytes) WriteUint(i uint64)

type Clear

type Clear interface {
	Reset()
}

Clear is an interface that can be implemented by a struct to reset its value Just for the convenience of objectpool

type MapType

type MapType struct {
	Type
	Key    *Type
	Elem   *Type
	Bucket *Type // internal type representing a hash bucket
	// function for hashing keys (ptr to key, seed) -> hash
	Hasher     func(unsafe.Pointer, uintptr) uintptr
	KeySize    uint8  // size of key slot
	ValueSize  uint8  // size of elem slot
	BucketSize uint16 // size of bucket
	Flags      uint32
}

MapType copy from abi.MapType

type PtrType

type PtrType struct {
	Type
	Elem *Type
}

PtrType copy from abi.PtrType

type Slice

type Slice struct {
	Data unsafe.Pointer
	Len  int
	Cap  int
}

Slice is a slice object pool for T []T put sync.Pool is invalid

type SlicePool added in v0.0.3

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

func GetSliceTypePool added in v0.0.3

func GetSliceTypePool[T any]() *SlicePool

GetSliceTypePool get a SlicePool with T

type Type

type Type struct {
	Size_       uintptr
	PtrBytes    uintptr
	Hash        uint32
	TFlag       uint8
	Align_      uint8
	FieldAlign_ uint8
	Kind_       uint8
	Equal       func(unsafe.Pointer, unsafe.Pointer) bool
	GCData      *byte
	Str         int32
	PtrToThis   int32
}

Type copy from abi.Type

Jump to

Keyboard shortcuts

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