orcaman/concurrent-map

a thread-safe concurrent map for go

Go

4,525

96 commits

updated May 22, 2024

See the code

README

concurrent map Build Status

As explained here and here, the map type in Go doesn't support concurrent reads and writes. concurrent-map provides a high-performance solution to this by sharding the map with minimal time spent waiting for locks.

Prior to Go 1.9, there was no concurrent map implementation in the stdlib. In Go 1.9, sync.Map was introduced. The new 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

usage

Import the package:

import (
	"github.com/orcaman/concurrent-map/v2"
)

go get "github.com/orcaman/concurrent-map/v2"

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

example


	// Create a new map.
	m := cmap.New[string]()

	// Sets item within map, sets "bar" under key "foo"
	m.Set("foo", "bar")

	// Retrieve item from map.
	bar, ok := m.Get("foo")

	// Removes item under key "foo"
	m.Remove("foo")

For more examples have a look at concurrent_map_test.go.

Running tests:

go test "github.com/orcaman/concurrent-map/v2"

guidelines for contributing

Contributions are highly welcome. In order for a contribution to be merged, please follow these guidelines:

  • Open an issue and describe what you are after (fixing a bug, adding an enhancement, etc.).
  • According to the core team's feedback on the above mentioned issue, submit a pull request, describing the changes and linking to the issue.
  • New code must have test coverage.
  • If the code is about performance issues, you must include benchmarks in the process (either in the issue or in the PR).
  • In general, we would like to keep concurrent-map as simple as possible and as similar to the native map. Please keep this in mind when opening issues.

language

license

MIT (see LICENSE file)

concurrency
concurrent-programming
go
golang
map
thread-safety

Project health

Overall

3.4/10

Maintained

0/10

Code review

9/10
OpenSSF Scorecard

Latest release

v2.0.1 · Nov 8, 2022

Releases this year

0

Significant stargazers

(top 24 of 34)

Daniel Nephin

541 followers · starred Nov 2020

zhenghaoz

288 followers · starred Sep 2021

Cong Ding

0 followers · starred Aug 2016

JINNOUCHI Yasushi

111 followers · starred Dec 2016

orcaman/concurrent-map

a thread-safe concurrent map for go

Go

4,525

96 commits

updated May 22, 2024

See the code

README

concurrent map Build Status

As explained here and here, the map type in Go doesn't support concurrent reads and writes. concurrent-map provides a high-performance solution to this by sharding the map with minimal time spent waiting for locks.

Prior to Go 1.9, there was no concurrent map implementation in the stdlib. In Go 1.9, sync.Map was introduced. The new 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

usage

Import the package:

import (
	"github.com/orcaman/concurrent-map/v2"
)

go get "github.com/orcaman/concurrent-map/v2"

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

example


	// Create a new map.
	m := cmap.New[string]()

	// Sets item within map, sets "bar" under key "foo"
	m.Set("foo", "bar")

	// Retrieve item from map.
	bar, ok := m.Get("foo")

	// Removes item under key "foo"
	m.Remove("foo")

For more examples have a look at concurrent_map_test.go.

Running tests:

go test "github.com/orcaman/concurrent-map/v2"

guidelines for contributing

Contributions are highly welcome. In order for a contribution to be merged, please follow these guidelines:

  • Open an issue and describe what you are after (fixing a bug, adding an enhancement, etc.).
  • According to the core team's feedback on the above mentioned issue, submit a pull request, describing the changes and linking to the issue.
  • New code must have test coverage.
  • If the code is about performance issues, you must include benchmarks in the process (either in the issue or in the PR).
  • In general, we would like to keep concurrent-map as simple as possible and as similar to the native map. Please keep this in mind when opening issues.

language

license

MIT (see LICENSE file)

concurrency
concurrent-programming
go
golang
map
thread-safety

Project health

Overall

3.4/10

Maintained

0/10

Code review

9/10
OpenSSF Scorecard

Latest release

v2.0.1 · Nov 8, 2022

Releases this year

0

Significant stargazers

(top 24 of 34)

Daniel Nephin

541 followers · starred Nov 2020

zhenghaoz

288 followers · starred Sep 2021

Cong Ding

0 followers · starred Aug 2016

JINNOUCHI Yasushi

111 followers · starred Dec 2016