cmap

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: MIT Imports: 8 Imported by: 0

README

cmap - Thread-safe map library

Go Reference

Hard fork of concurrent-map with a lot of breaking changes.

This library provides a thread-safe map that is split into individual shards, each of which has a separate RWMutex, which reduces the time spent waiting for locks.

Documentation

Overview

Package cmap provides thread-safe map.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ComputeOp added in v0.3.0

type ComputeOp int

ComputeOp is an operation to be applied to the value stored under a key by Compute.

const (
	// KeepOp keeps the current value in the map unchanged.
	KeepOp ComputeOp = iota
	// SetOp stores the new value returned by the callback under the key.
	SetOp
	// RemoveOp deletes the key from the map.
	RemoveOp
)

type Item added in v0.3.0

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

Item is a key-value pair stored in a Map, passed to the callback of Iter, IterMut, Seq and SeqMut.

An Item is only valid inside the iteration callback it was passed to: the underlying entry may be changed or removed by other goroutines after the callback returns.

func (*Item[K, V]) Delete added in v0.3.0

func (it *Item[K, V]) Delete()

Delete removes the item from the map.

It only takes effect when the item was obtained from a mutable iteration (IterMut or SeqMut); otherwise it is a no-op.

func (*Item[K, V]) Key added in v0.3.0

func (it *Item[K, V]) Key() K

Key returns the key of the item.

func (*Item[K, V]) Value added in v0.3.0

func (it *Item[K, V]) Value() V

Value returns the value of the item.

type Map

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

Map is a thread-safe map. To avoid lock bottlenecks this map is divided into several map shards, each guarded by its own mutex.

func New

func New[K comparable, V any](opts ...Option) *Map[K, V]

New creates a new concurrent map.

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear()

Clear removes all items from the map.

func (*Map[K, V]) Compute added in v0.3.0

func (m *Map[K, V]) Compute(key K, fn func(oldValue V, exists bool) (newValue V, op ComputeOp)) (actual V, ok bool)

Compute atomically updates the value stored under the given key. fn is called with the current value and whether the key is present in the map, and must return the new value together with the operation to apply:

  • KeepOp leaves the stored value (or the key absence) unchanged;
  • SetOp stores newValue under the key;
  • RemoveOp deletes the key from the map.

Compute returns the resulting value for the key and whether the key is present after the operation.

fn is invoked while the shard containing the key is locked; it must not call methods of the same map, as that may deadlock.

func (*Map[K, V]) Get

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

Get retrieves an element from the map under the specified key.

func (*Map[K, V]) IsEmpty

func (m *Map[K, V]) IsEmpty() bool

IsEmpty checks if the map is empty.

func (*Map[K, V]) Items

func (m *Map[K, V]) Items() map[K]V

Items returns all items in the map.

func (*Map[K, V]) Iter

func (m *Map[K, V]) Iter(fn func(*Item[K, V]) bool)

Iter iterates over all items in the map in an unspecified order, calling fn for each item until fn returns false or every item has been visited.

Iteration holds a read lock on each shard, so the map must not be modified from within fn (Item.Delete is a no-op here); use IterMut instead.

func (*Map[K, V]) IterMut added in v0.3.0

func (m *Map[K, V]) IterMut(fn func(*Item[K, V]) bool)

IterMut is like Iter, but holds a write lock on each shard, so items can be removed from the map with Item.Delete.

As with Iter, the map must not be modified from within fn by any other means.

func (*Map[K, V]) Keys

func (m *Map[K, V]) Keys() []K

Keys returns all keys in the map.

func (*Map[K, V]) Len added in v0.3.0

func (m *Map[K, V]) Len() int

Len returns the number of elements within the map.

func (*Map[K, V]) Remove

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

Remove removes an element from the map and returns it.

func (*Map[K, V]) Seq

func (m *Map[K, V]) Seq() iter.Seq[*Item[K, V]]

Seq returns an iterator over the items of the map, suitable for use with range. It is equivalent to Iter.

func (*Map[K, V]) SeqMut added in v0.3.0

func (m *Map[K, V]) SeqMut() iter.Seq[*Item[K, V]]

SeqMut returns a mutable iterator over the items of the map, suitable for use with range. It is equivalent to IterMut and allows items to be removed with Item.Delete.

func (*Map[K, V]) Set

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

Set sets the given value under the specified key.

func (*Map[K, V]) SetIfAbsent

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

SetIfAbsent sets the given value under the specified key if no value was associated with it.

type Option

type Option func(*options)

Option is used to configure concurrent map.

func WithShardCount

func WithShardCount(n int) Option

WithShardCount allows to set the number of shards in a map.

func WithShardingFunc

func WithShardingFunc[K comparable](fn ShardingFunc[K]) Option

WithShardingFunc allows to set the sharding function of a map.

type ShardingFunc

type ShardingFunc[K comparable] func(key K) uint64

ShardingFunc is a function for sharding a map.

Jump to

Keyboard shortcuts

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