typedsync

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2023 License: GPL-2.0-or-later Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheMap

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

A CacheMap is similar to a Map, but also has a .LoadOrCompute sibling to .LoadOrStore. This is useful for caching the results of computation where it is undesirable for concurrent calls to duplicate work.

Compared to a plain Map, a CacheMap has both more space overhead and more time overhead.

func (*CacheMap[K, V]) Delete

func (m *CacheMap[K, V]) Delete(key K)

func (*CacheMap[K, V]) Load

func (m *CacheMap[K, V]) Load(key K) (value V, ok bool)

Load returns the value stored in the map for a key. If the value for that key is actively being computed by LoadOrCompute, this blocks until the value has been computed.

func (*CacheMap[K, V]) LoadAndDelete

func (m *CacheMap[K, V]) LoadAndDelete(key K) (value 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. If the value for that key was actively being computed by LoadOrCompute, this immediately removes the partial value from the CacheMap, but does not return until the computation has finished.

func (*CacheMap[K, V]) LoadOrCompute

func (m *CacheMap[K, V]) LoadOrCompute(key K, fn func(K) V) (actual V, loaded bool)

LoadOrCompute returns the existing value for the key if present. Otherwise, it computes and stores a value using the given function. The loaded result is true if the value was loaded, false if computed. If a prior call to LoadOrCompute is still computing the value for that key, a latter call blocks until the computation is complete, and then returns the initial call's value.

func (*CacheMap[K, V]) LoadOrStore

func (m *CacheMap[K, V]) LoadOrStore(key K, value 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. If the value for that key is actively being computed by LoadOrCompute, this blocks until the value has been computed.

func (*CacheMap[K, V]) Store

func (m *CacheMap[K, V]) Store(key K, value V)

type Map

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

Map is a type-safe equivalent of the standard library's sync.Map.

With versions of Go prior to Go 1.20, Map is specified too loosely, as

Map[K any, V any]

while with Go 1.20 and later, Map is specified as

Map[K comparable, V any]

This is because with Go versions prior to 1.20, 'comparable' was overly strict, disallowing many types that are valid map-keys (see https://github.com/golang/go/issues/56548). The type used as K in a Map older versions of Go must be a valid map-key type, even though the type specification of Map does not enforce that.

func (*Map[K, V]) Delete

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

func (*Map[K, V]) Load

func (m *Map[K, V]) Load(key K) (value V, ok bool)

func (*Map[K, V]) LoadAndDelete

func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)

func (*Map[K, V]) LoadOrStore

func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

func (*Map[K, V]) Range

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

func (*Map[K, V]) Store

func (m *Map[K, V]) Store(key K, value V)

type Pool

type Pool[T any] struct {
	New func() T
	// contains filtered or unexported fields
}

func (*Pool[T]) Get

func (p *Pool[T]) Get() (val T, ok bool)

func (*Pool[T]) Put

func (p *Pool[T]) Put(val T)

type Value

type Value[T comparable] struct {
	// contains filtered or unexported fields
}

Value is a typed equivalent of sync/atomic.Value.

It is not actually a wrapper around sync/atomic.Value for allocation-performance reasons.

func (*Value[T]) CompareAndSwap

func (v *Value[T]) CompareAndSwap(oldV, newV T) (swapped bool)

func (*Value[T]) Load

func (v *Value[T]) Load() (val T, ok bool)

func (*Value[T]) Store

func (v *Value[T]) Store(val T)

func (*Value[T]) Swap

func (v *Value[T]) Swap(newV T) (oldV T, oldOK bool)

Jump to

Keyboard shortcuts

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