cmap

package module
v0.0.0-...-cb084a6 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: MIT Imports: 4 Imported by: 9

README

cmap Build Status GoDoc codecov Go Report Card

The map type in Go doesn't support concurrent reads and writes. cmap(concurrent-map) provides a high-performance solution to this by sharding the map with minimal time spent waiting for locks.

The sync.Map has a few key differences from this map. The stdlib sync.Map is designed for append-only scenarios. So if you want to use the map for something more like in-memory db, you might benefit from using our version. You can read more about it in the golang repo, for example here and here

Here we fork some README document from concurrent-map

usage

Import the package:

import (
	"github.com/lrita/cmap"
)

go get "github.com/lrita/cmap"

The package is now imported under the "cmap" namespace.

example


	// Create a new map.
	var m cmap.Cmap

	// Stores item within map, sets "bar" under key "foo"
	m.Store("foo", "bar")

	// Retrieve item from map.
	if tmp, ok := m.Load("foo"); ok {
		bar := tmp.(string)
	}

	// Deletes item under key "foo"
	m.Delete("foo")

	// If you are using g1.18+, you can use the generics implementation

	var n cmap.Map[string, string]

	// Stores item within map, sets "bar" under key "foo"
	n.Store("foo", "bar")

	// Retrieve item from map.
	if tmp, ok := n.Load("foo"); ok {
	    bar := tmp
	}

	// Deletes item under key "foo"
	n.Delete("foo")

benchmark

goos: darwin
goarch: amd64
pkg: github.com/lrita/cmap
BenchmarkLoadMostlyHits/*cmap_test.DeepCopyMap-4         	50000000	        34.5 ns/op
BenchmarkLoadMostlyHits/*cmap_test.RWMutexMap-4          	20000000	        65.2 ns/op
BenchmarkLoadMostlyHits/*sync.Map-4                      	50000000	        34.8 ns/op
BenchmarkLoadMostlyHits/*cmap.Cmap-4                     	30000000	        53.5 ns/op
BenchmarkLoadMostlyMisses/*cmap_test.DeepCopyMap-4       	50000000	        26.7 ns/op
BenchmarkLoadMostlyMisses/*cmap_test.RWMutexMap-4        	20000000	        62.5 ns/op
BenchmarkLoadMostlyMisses/*sync.Map-4                    	50000000	        22.7 ns/op
BenchmarkLoadMostlyMisses/*cmap.Cmap-4                   	30000000	        40.3 ns/op
--- SKIP: BenchmarkLoadOrStoreBalanced/*cmap_test.DeepCopyMap
    cmap_bench_test.go:91: DeepCopyMap has quadratic running time.
BenchmarkLoadOrStoreBalanced/*cmap_test.RWMutexMap-4     	 3000000	       437 ns/op
BenchmarkLoadOrStoreBalanced/*sync.Map-4                 	 3000000	       546 ns/op
BenchmarkLoadOrStoreBalanced/*cmap.Cmap-4                	 3000000	       497 ns/op
--- SKIP: BenchmarkLoadOrStoreUnique/*cmap_test.DeepCopyMap
    cmap_bench_test.go:123: DeepCopyMap has quadratic running time.
BenchmarkLoadOrStoreUnique/*cmap_test.RWMutexMap-4       	 2000000	       990 ns/op
BenchmarkLoadOrStoreUnique/*sync.Map-4                   	 1000000	      1032 ns/op
BenchmarkLoadOrStoreUnique/*cmap.Cmap-4                  	 2000000	       892 ns/op
BenchmarkLoadOrStoreCollision/*cmap_test.DeepCopyMap-4   	100000000	        18.2 ns/op
BenchmarkLoadOrStoreCollision/*cmap_test.RWMutexMap-4    	10000000	       165 ns/op
BenchmarkLoadOrStoreCollision/*sync.Map-4                	100000000	        19.6 ns/op
BenchmarkLoadOrStoreCollision/*cmap.Cmap-4               	20000000	        65.7 ns/op
BenchmarkRange/*cmap_test.DeepCopyMap-4                  	  200000	      8646 ns/op
BenchmarkRange/*cmap_test.RWMutexMap-4                   	   20000	     62046 ns/op
BenchmarkRange/*sync.Map-4                               	  200000	      9317 ns/op
BenchmarkRange/*cmap.Cmap-4                              	   50000	     31107 ns/op
BenchmarkAdversarialAlloc/*cmap_test.DeepCopyMap-4       	 2000000	       531 ns/op
BenchmarkAdversarialAlloc/*cmap_test.RWMutexMap-4        	20000000	        74.3 ns/op
BenchmarkAdversarialAlloc/*sync.Map-4                    	 5000000	       390 ns/op
BenchmarkAdversarialAlloc/*cmap.Cmap-4                   	30000000	        53.6 ns/op
BenchmarkAdversarialDelete/*cmap_test.DeepCopyMap-4      	 5000000	       273 ns/op
BenchmarkAdversarialDelete/*cmap_test.RWMutexMap-4       	20000000	        94.4 ns/op
BenchmarkAdversarialDelete/*sync.Map-4                   	10000000	       137 ns/op
BenchmarkAdversarialDelete/*cmap.Cmap-4                  	30000000	        43.3 ns/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmap

type Cmap struct {
	// contains filtered or unexported fields
}

Cmap is a "thread" safe map of type AnyComparableType:Any. To avoid lock bottlenecks this map is dived to several map shards. We can store different type key and value into the same map.

func (*Cmap) Count

func (m *Cmap) Count() int

Count returns the number of elements within the map.

func (*Cmap) Delete

func (m *Cmap) Delete(key interface{})

Delete deletes the value for a key.

func (*Cmap) IsEmpty

func (m *Cmap) IsEmpty() bool

IsEmpty checks if map is empty.

func (*Cmap) Load

func (m *Cmap) Load(key interface{}) (value interface{}, 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 (*Cmap) LoadOrStore

func (m *Cmap) LoadOrStore(key, value interface{}) (actual interface{}, 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 (*Cmap) Range

func (m *Cmap) Range(f func(key, value interface{}) 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, Range may reflect any mapping for that key from any point during the Range call.

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 (*Cmap) Store

func (m *Cmap) Store(key, value interface{})

Store sets the value for a key.

type Map

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

Map is a "thread" generics safe map of type AnyComparableType:Any (AnyComparableType exclude interface type). To avoid lock bottlenecks this map is dived to several map shards.

func (*Map[K, V]) Count

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

Count returns the number of elements within the map.

func (*Map[K, V]) Delete

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

Delete deletes the value for a key.

func (*Map[K, V]) IsEmpty

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

IsEmpty checks if map is empty.

func (*Map[K, V]) Load

func (m *Map[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 (*Map[K, V]) LoadOrStore

func (m *Map[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 (*Map[K, V]) Range

func (m *Map[K, V]) Range(f func(key K, value V) 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, Range may reflect any mapping for that key from any point during the Range call.

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(key K, value V)

Store sets the value for a key.

Jump to

Keyboard shortcuts

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