Top Related Projects
⚖️ A tool for transpiling C to Go.
A collection of generic data structures written in Go.
🐜🐜🐜 ants is the most powerful and reliable pooling solution for Go.
A collection of useful, performant, and threadsafe Go datastructures.
GoDS (Go Data Structures) - Sets, Lists, Stacks, Maps, Trees, Queues, and much more
A Golang lock-free thread-safe HashMap optimized for fastest read access.
Quick Overview
The concurrent-map
library is a thread-safe, high-performance concurrent map implementation for Go. It provides a simple and efficient way to manage shared data structures in concurrent Go applications.
Pros
- Thread-Safety: The library ensures thread-safety, allowing multiple goroutines to access and modify the map concurrently without race conditions.
- High-Performance: The implementation is optimized for performance, utilizing techniques like lock-free operations and sharding to minimize contention.
- Simplicity: The API is straightforward and easy to use, providing a familiar map-like interface for developers.
- Flexibility: The library supports various operations, such as adding, removing, and retrieving elements, as well as iterating over the map.
Cons
- Limited Functionality: The library provides a basic map-like functionality, but may lack some advanced features found in the standard Go
sync.Map
implementation. - Potential Overhead: The concurrency mechanisms used in the library may introduce some overhead compared to a simple
sync.Map
in certain scenarios with low concurrency. - Lack of Generics: The library uses interface{} for key and value types, which can lead to the need for type assertions and potentially less type-safe code.
- Dependency on External Library: The library depends on the
github.com/orcaman/struct-go
package, which may be a concern for some users.
Code Examples
Here are a few examples of how to use the concurrent-map
library:
// Creating a new concurrent map
m := cmap.New()
// Adding a key-value pair
m.Set("key", "value")
// Retrieving a value
value, ok := m.Get("key")
if ok {
fmt.Println(value)
}
// Removing a key-value pair
m.Remove("key")
// Iterating over the map
m.IterCb(func(key, value interface{}) {
fmt.Printf("Key: %v, Value: %v\n", key, value)
})
Getting Started
To use the concurrent-map
library in your Go project, follow these steps:
-
Install the library using
go get
:go get github.com/orcaman/concurrent-map
-
Import the library in your Go file:
import "github.com/orcaman/concurrent-map"
-
Create a new concurrent map and use its methods to interact with the data:
m := cmap.New() m.Set("key", "value") value, ok := m.Get("key") if ok { fmt.Println(value) }
-
Refer to the library's README for more detailed usage examples and documentation.
Competitor Comparisons
⚖️ A tool for transpiling C to Go.
Pros of c2go
- Focuses on C to Go translation, offering a specialized tool for migrating C codebases to Go
- Provides a comprehensive solution for handling C-specific constructs and idioms in Go
- Includes a test suite and documentation to aid in the translation process
Cons of c2go
- Limited to C-to-Go translation, lacking the general-purpose concurrency features of concurrent-map
- May require additional work to optimize generated Go code for performance
- Less actively maintained compared to concurrent-map (last commit over 2 years ago)
Code Comparison
c2go example (C to Go translation):
// C code:
// int main() {
// printf("Hello, World!");
// return 0;
// }
// Translated Go code:
package main
import "fmt"
func main() {
fmt.Printf("Hello, World!")
}
concurrent-map example (concurrent map usage):
import "github.com/orcaman/concurrent-map/v2"
map := cmap.New[string]()
map.Set("foo", "bar")
if tmp, ok := map.Get("foo"); ok {
bar := tmp.(string)
}
These repositories serve different purposes: c2go is a specialized tool for C-to-Go translation, while concurrent-map provides a thread-safe map implementation for Go. The choice between them depends on the specific needs of your project.
A collection of generic data structures written in Go.
Pros of generic
- Offers a wider range of generic data structures and algorithms beyond concurrent maps
- Provides type-safe implementations using Go's generics feature
- Includes utility functions for common operations on slices and maps
Cons of generic
- Lacks specific focus on concurrent data structures
- May have a steeper learning curve due to its broader scope
- Requires Go 1.18+ for generics support, limiting compatibility with older Go versions
Code comparison
concurrent-map:
import cmap "github.com/orcaman/concurrent-map/v2"
m := cmap.New[string]()
m.Set("foo", "bar")
bar, ok := m.Get("foo")
generic:
import "github.com/zyedidia/generic/mapset"
s := mapset.New[string]()
s.Add("foo")
s.Add("bar")
contains := s.Contains("foo")
Summary
concurrent-map focuses specifically on providing a thread-safe map implementation, while generic offers a broader range of generic data structures and algorithms. concurrent-map is more specialized for concurrent use cases, whereas generic provides a wider set of tools for various programming needs. The choice between the two depends on whether you need a focused concurrent map solution or a more comprehensive set of generic data structures and algorithms.
🐜🐜🐜 ants is the most powerful and reliable pooling solution for Go.
Pros of ants
- Provides a goroutine pool for efficient concurrency management
- Offers more advanced features like automatic pool size adjustment
- Includes benchmarks and performance optimizations
Cons of ants
- More complex API, potentially steeper learning curve
- Focused on goroutine pooling rather than concurrent data structures
- May introduce overhead for simple concurrent operations
Code Comparison
ants:
pool, _ := ants.NewPool(10000)
for i := 0; i < 1000000; i++ {
pool.Submit(func() {
// Task logic here
})
}
concurrent-map:
cmap := cmap.New()
cmap.Set("key", "value")
value, ok := cmap.Get("key")
Summary
ants is a goroutine pool library focused on efficient concurrency management, while concurrent-map is a thread-safe map implementation. ants offers more advanced features and potential performance benefits for complex concurrent tasks, but may be overkill for simple operations. concurrent-map provides a straightforward solution for concurrent access to a shared map data structure, with a simpler API and lower overhead for basic use cases.
A collection of useful, performant, and threadsafe Go datastructures.
Pros of go-datastructures
- Offers a wider variety of concurrent data structures beyond maps
- Includes advanced structures like skip lists and augmented trees
- Provides more fine-grained control over concurrency
Cons of go-datastructures
- More complex API, potentially steeper learning curve
- May have higher memory overhead for simpler use cases
- Less focused on map-specific optimizations
Code Comparison
concurrent-map:
import cmap "github.com/orcaman/concurrent-map"
m := cmap.New()
m.Set("key", "value")
value, ok := m.Get("key")
go-datastructures:
import "github.com/Workiva/go-datastructures/queue"
q := queue.New(10)
q.Put("item")
item, err := q.Get(1)
Summary
concurrent-map is a simpler, more focused library specifically for concurrent maps, while go-datastructures offers a broader range of concurrent data structures with more advanced features. concurrent-map may be easier to use for basic map operations, while go-datastructures provides more flexibility for complex concurrent data management needs. The choice between them depends on the specific requirements of your project and the level of complexity you're willing to manage.
GoDS (Go Data Structures) - Sets, Lists, Stacks, Maps, Trees, Queues, and much more
Pros of gods
- Offers a wide variety of data structures beyond just concurrent maps
- Provides both mutable and immutable implementations for many structures
- Includes sorting algorithms and iterators for enhanced functionality
Cons of gods
- Not specifically optimized for concurrent operations like concurrent-map
- May have higher memory overhead due to its more comprehensive feature set
- Potentially more complex API due to the breadth of functionality
Code Comparison
concurrent-map:
import cmap "github.com/orcaman/concurrent-map/v2"
m := cmap.New[string]()
m.Set("foo", "bar")
value, ok := m.Get("foo")
gods:
import "github.com/emirpasic/gods/maps/treemap"
m := treemap.NewWithStringComparator()
m.Put("foo", "bar")
value, found := m.Get("foo")
Both libraries provide similar basic functionality for map operations, but gods offers more diverse data structures and algorithms. concurrent-map focuses specifically on thread-safe map operations, while gods provides a broader set of data structures and algorithms, some of which may not be optimized for concurrency. The choice between the two depends on the specific requirements of the project, with concurrent-map being more suitable for highly concurrent map operations and gods offering more versatility for various data structure needs.
A Golang lock-free thread-safe HashMap optimized for fastest read access.
Pros of hashmap
- Higher performance due to optimized data structures and lock-free algorithms
- Supports custom key types without reflection
- Provides additional features like lazy loading and automatic cleanup of expired entries
Cons of hashmap
- More complex implementation, potentially harder to maintain or extend
- Less widely adopted compared to concurrent-map
- May have a steeper learning curve for developers new to lock-free programming
Code Comparison
hashmap:
m := hashmap.New[string, int]()
m.Set("key", 123)
value, ok := m.Get("key")
concurrent-map:
m := cmap.New[int]()
m.Set("key", 123)
value, ok := m.Get("key")
Both libraries provide similar basic functionality for concurrent map operations. However, hashmap offers more flexibility with custom key types and additional features, while concurrent-map focuses on simplicity and ease of use.
hashmap generally provides better performance, especially under high concurrency, due to its lock-free design. concurrent-map, on the other hand, uses a simpler sharded approach with traditional locks, which may be more familiar to developers and easier to reason about in certain scenarios.
The choice between these libraries depends on specific project requirements, performance needs, and the development team's expertise in concurrent programming techniques.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
concurrent map
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 nativemap
. Please keep this in mind when opening issues.
language
license
MIT (see LICENSE file)
Top Related Projects
⚖️ A tool for transpiling C to Go.
A collection of generic data structures written in Go.
🐜🐜🐜 ants is the most powerful and reliable pooling solution for Go.
A collection of useful, performant, and threadsafe Go datastructures.
GoDS (Go Data Structures) - Sets, Lists, Stacks, Maps, Trees, Queues, and much more
A Golang lock-free thread-safe HashMap optimized for fastest read access.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot