esync

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

package containers/esync implements type-safe wrappers around the stdlib's sync.Pool and sync.Map, and a concurrency-safe set implementation (using sync.Map)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map[K, V any] struct {
	// contains filtered or unexported fields
}

Map is a type-safe convenience wrapper around sync.Map. The documentation is taken from the documentation for sync.Map, (C) the go authors, as of go1.18, with minor edits to reflect the type changes.

Map is like a Go map[K]V but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time. The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex. The zero Map is empty and ready for use. A Map must not be copied after first use.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(k K)

Delete deletes the value for a key.

func (*Map[K, V]) Load

func (m *Map[K, V]) Load(k K) (v V, ok bool)

Load returns the value stored in the map for a key. The ok result indicates whether value was found in the map.

func (*Map[K, V]) LoadAndDelete

func (m *Map[K, V]) LoadAndDelete(k K) (v V, loaded bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*Map[K, V]) LoadOrStore

func (m *Map[K, V]) LoadOrStore(k K, v V) (actual V, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(f func(K, V) (next bool))

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration. Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m. Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.

func (*Map[K, V]) Store

func (m *Map[K, V]) Store(k K, v V)

type Pool

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

Pool is a type-safe wrapper around sync.Pool. A Pool[T] can Get() or Put() a *T.

A sync.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.

BufPool is a pool of *bytes.Buffer used by string formatting functions throughout estd.

func NewDefaultPool

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

NewDefaultPool is syntatic sugar for NewPool(func() *T { return new(T) }) }

func NewPool

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

NewPool creates a pool where new members are allocated via newT.

func (*Pool[T]) Get

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

Get is a type-safe wrapper around sync.Pool.Get. See the docs:

Get selects an arbitrary item from the Pool, removes it from the Pool, and returns it to the caller. Get may choose to ignore the pool and treat it as empty. Callers should not assume any relation between values passed to Put and the values returned by Get. If Get would otherwise return nil and p.New is non-nil, Get returns the result of calling p.New.

func (*Pool[T]) Put

func (zp *Pool[T]) Put(pt *T)

Put adds pt to the pool.,

Jump to

Keyboard shortcuts

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