bufferpool

package
v0.0.0-...-24ca9bf Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Swimmer

func Swimmer[T any](p sync.Pool, object *T) (Put func())

Swimmer takes a pointer to a desired pool object to be initialized and returns the function that is used to Put it back in the pool. This results in a ~10 to 30% performance increase over the standard Get() ... defer Put() pattern.

If any part of the process fails, the function will return nil.

Used with defer statement to Get the pool object and defer the matching Put in one line.

Types

type BufferPool

type BufferPool = Pool[*bytes.Buffer]

BufferPool is a sync.Pool implementation designed to use with bytes.Buffer pointers.

type Pool

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

A pool is a set of temporary objects that may be individually saved and retrieved.

Any item stored in the pool may be removed automatically at any time without notification. If the pool holds the only reference when this happens, the item might be deallocated.

A pool is safe for use by multiple goroutines simultaneously.

pool's purpose is to cache allocated but unused items for later reuse, relieving pressure on the garbage collector. That is, it makes it easy to build efficient, thread-safe free lists. However, it is not suitable for all free lists.

An appropriate use of a pool is to manage a group of temporary items silently shared among and potentially reused by concurrent independent clients of a package. pool provides a way to amortize allocation overhead across many clients.

An example of good use of a pool is in the fmt package, which maintains a dynamically-sized store of temporary output buffers. The store scales under load (when many goroutines are actively printing) and shrinks when quiescent.

On the other hand, a free list maintained as part of a short-lived object is not a suitable use for a pool, since the overhead does not amortize well in that scenario. It is more efficient to have such objects implement their own free list.

A pool must not be copied after first use.

Reference: standard library sync package (sync.Pool)

func NewPool

func NewPool[T any]() Pool[T]

NewPool creates a new sync.Pool implementation with a generic New function defining the type temporary object that will be used.

func (Pool[T]) Diver

func (b Pool[T]) Diver(fn func(buf T))

Diver wraps an anonymous function in Get()/Put() calls.

func (Pool[T]) Get

func (b Pool[T]) Get() T

Get is the generic sync.Pool Get implementation but uses a stored function to add additional functionality as needed.

func (Pool[T]) GetReset

func (b Pool[T]) GetReset() T

func (Pool[T]) Put

func (b Pool[T]) Put(v T)

Put is a generic sync.Pool Put implementation but uses a stored function to add additional functionality as needed.

func (Pool[T]) Swimmer

func (b Pool[T]) Swimmer(buf T) (Put func())

Swimmer takes a pointer to a desired pool object to be initialized and returns the function that is used to Put it back in the pool. This results in a ~10 to 30% performance increase over the standard Get() ... defer Put() pattern.

If any part of the process fails, the function will return nil.

Used with defer statement to Get the pool object and defer the matching Put in one line.

type Pooler

type Pooler[T any] interface {
	Get() T
	Put(x T)
}

Pooler is a generic implementation of Pooler

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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