ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - lrita/cmap: a thread-safe concurrent map for go
a thread-safe concurrent map for go. Contribute to lrita/cmap development by creating an account on GitHub.
Visit Site

GitHub - lrita/cmap: a thread-safe concurrent map for go

GitHub - lrita/cmap: a thread-safe concurrent map for go

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

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory