reuse

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SyncBased  = SPI(0)
	MpoolBased = SPI(1)
)

Functions

func Alloc

func Alloc[T ReusableObject](p Pool[T]) *T

Alloc allocates a pooled object.

func CreatePool

func CreatePool[T ReusableObject](
	new func() *T,
	reset func(*T),
	opts *Options[T])

CreatePool create pool instance.

func Free

func Free[T ReusableObject](v *T, p Pool[T])

Free free a pooled object.

func RunReuseTests

func RunReuseTests(fn func())

Types

type Options

type Options[T ReusableObject] struct {
	// contains filtered or unexported fields
}

Options options to create object pool

func DefaultOptions

func DefaultOptions[T ReusableObject]() *Options[T]

DefaultOptions default options

func (*Options[T]) WithEnableChecker

func (opts *Options[T]) WithEnableChecker() *Options[T]

WithEnableChecker enable check double free, leak free.

func (*Options[T]) WithReleaseFunc

func (opts *Options[T]) WithReleaseFunc(release func(*T)) *Options[T]

WithReleaseFunc with specified release function. The release function is used to release resources before gc.

type Pool

type Pool[T ReusableObject] interface {
	Alloc() *T
	Free(*T)
}

Pool is a pool of temporary objects, designed to reduce the creation of temporary objects to reduce GC pressure.

There are many places throughout the system where temporary objects need to be created, and if these places are in our hot path, we need to consider using a pool to reduce the number of temporary objects.

type ReusableObject

type ReusableObject interface {
	// TypeName returns the name of the object type. We cannot use reflect.TypeOf to get
	// the name of the object type, to avoid mem allocate.
	// Outdated, may delete later.
	TypeName() string
}

ReusableObject all reusable objects must implements this interface

type SPI

type SPI int

SPI choose pool implementation

Jump to

Keyboard shortcuts

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