Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Swimmer ¶
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 ¶
BufferPool is a sync.Pool implementation designed to use with bytes.Buffer pointers.
type Pool ¶
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]) 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.