gsm

package module
v0.1.26 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2025 License: MIT Imports: 2 Imported by: 0

README

gsm

Enhanced version of Go's sync.Map with generic types and iterators.

Features:

  1. Wrapping of all sync.Map methods with methods having generic parameter and return types.
  2. Iterator methods Range(), Keys(), and Values()
  3. Very fast, on Apple M2 in parallel Load() benchmarks:
    1. Adds 0.1 nanoseconds overhead vs sync.Map
    2. 60x faster than sync.RWMutex.RLock and builtin go map
    3. See benchmarks for raw output
  4. Unit tests of every method
  5. Documentation including runnable example

Example:

package main

import (
	"fmt"
	"slices"

	"github.com/aaronriekenberg/gsm"
)

func main() {

	type name string

	type person struct {
		title string
		age   int
	}

	nameToPerson := gsm.GenericSyncMap[name, person]{}

	nameToPerson.Store("alice", person{title: "engineer", age: 25})
	nameToPerson.Store("bob", person{title: "manager", age: 35})

	value, ok := nameToPerson.Load("alice")
	fmt.Printf("alice value = %+v ok = %v\n", value, ok)
	fmt.Printf("alice title = %q\n", value.title)

	value, ok = nameToPerson.Load("bob")
	fmt.Printf("bob value = %+v ok = %v\n", value, ok)
	fmt.Printf("bob age = %v\n", value.age)

	swapped := nameToPerson.CompareAndSwap(
		"alice",
		person{title: "engineer", age: 25},
		person{title: "manager", age: 25},
	)
	value, ok = nameToPerson.Load("alice")
	fmt.Printf("swapped = %v value = %+v ok = %v\n", swapped, value, ok)

	keys := slices.Sorted(nameToPerson.Keys())
	fmt.Printf("keys = %v", keys)
}

output:

alice value = {title:engineer age:25} ok = true
alice title = "engineer"
bob value = {title:manager age:35} ok = true
bob age = 35
swapped = true value = {title:manager age:25} ok = true
keys = [alice bob]

Benchmarks:

$ go test -bench=. -benchmem ./...

goos: darwin
goarch: arm64
pkg: github.com/aaronriekenberg/gsm
cpu: Apple M2
BenchmarkSyncMapParallelLoad-8          	769333302	         1.428 ns/op	       0 B/op	       0 allocs/op
BenchmarkGenericSyncMapParallelLoad-8   	758809900	         1.588 ns/op	       0 B/op	       0 allocs/op
BenchmarkRWMutexMapParallelLoad-8       	12766698	        96.07 ns/op	      63 B/op	       1 allocs/op

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GenericSyncMap

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

GenericSyncMap is like sync.Map with generic key and value types K and V. The zero GenericSyncMap is empty and ready for use. A GenericSyncMap must not be copied after first use.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/aaronriekenberg/gsm"
)

func main() {

	type name string

	type person struct {
		title string
		age   int
	}

	nameToPerson := gsm.GenericSyncMap[name, person]{}

	nameToPerson.Store("alice", person{title: "engineer", age: 25})
	nameToPerson.Store("bob", person{title: "manager", age: 35})

	value, ok := nameToPerson.Load("alice")
	fmt.Printf("alice value = %+v ok = %v\n", value, ok)
	fmt.Printf("alice title = %q\n", value.title)

	value, ok = nameToPerson.Load("bob")
	fmt.Printf("bob value = %+v ok = %v\n", value, ok)
	fmt.Printf("bob age = %v\n", value.age)

	swapped := nameToPerson.CompareAndSwap(
		"alice",
		person{title: "engineer", age: 25},
		person{title: "manager", age: 25},
	)
	value, ok = nameToPerson.Load("alice")
	fmt.Printf("swapped = %v value = %+v ok = %v\n", swapped, value, ok)

	keys := slices.Sorted(nameToPerson.Keys())
	fmt.Printf("keys = %v", keys)

}
Output:

alice value = {title:engineer age:25} ok = true
alice title = "engineer"
bob value = {title:manager age:35} ok = true
bob age = 35
swapped = true value = {title:manager age:25} ok = true
keys = [alice bob]

func (*GenericSyncMap[K, V]) Clear

func (gsm *GenericSyncMap[K, V]) Clear()

Clear deletes all the entries, resulting in an empty Map.

func (*GenericSyncMap[K, V]) CompareAndDelete

func (gsm *GenericSyncMap[K, V]) CompareAndDelete(
	key K,
	old V,
) (deleted bool)

CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type.

If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).

func (*GenericSyncMap[K, V]) CompareAndSwap

func (gsm *GenericSyncMap[K, V]) CompareAndSwap(
	key K,
	old V,
	new V,
) (swapped bool)

CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.

func (*GenericSyncMap[K, V]) Delete

func (gsm *GenericSyncMap[K, V]) Delete(
	key K,
)

Delete deletes the value for a key.

func (*GenericSyncMap[K, V]) Keys

func (gsm *GenericSyncMap[K, V]) Keys() iter.Seq[K]

Range returns an iter.Seq[K] over all keys in the map.

func (*GenericSyncMap[K, V]) Load

func (gsm *GenericSyncMap[K, V]) Load(
	key K,
) (value V, ok bool)

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*GenericSyncMap[K, V]) LoadAndDelete

func (gsm *GenericSyncMap[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.

func (*GenericSyncMap[K, V]) LoadOrStore

func (gsm *GenericSyncMap[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.

func (*GenericSyncMap[K, V]) Range

func (gsm *GenericSyncMap[K, V]) Range() iter.Seq2[K, V]

Range returns an iter.Seq2[K, V] over all entries in the map.

func (*GenericSyncMap[K, V]) Store

func (gsm *GenericSyncMap[K, V]) Store(
	key K,
	value V,
)

Store sets the value for a key.

func (*GenericSyncMap[K, V]) Swap

func (gsm *GenericSyncMap[K, V]) Swap(
	key K,
	value V,
) (previous V, loaded bool)

Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present.

func (*GenericSyncMap[K, V]) Values

func (gsm *GenericSyncMap[K, V]) Values() iter.Seq[V]

Range returns an iter.Seq[V] over all values in the map.

Jump to

Keyboard shortcuts

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