Home >Backend Development >Golang >gmap in GoFrame: A Deep Dive into High-Performance Concurrent Maps
Ever found yourself wrestling with concurrent map access in Go? You're not alone! While sync.Map is built into Go, sometimes we need something more powerful. Enter gmap from the GoFrame framework - a high-performance concurrent-safe map that might just be what you're looking for.
In this article, we'll explore:
Let's dive in! ?♂️
gmap is a concurrent-safe map implementation provided by GoFrame that's specifically designed for high-concurrency scenarios. If you're building applications that need to handle lots of concurrent read/write operations on shared maps, this is worth your attention.
First, let's see how to get up and running with gmap:
import "github.com/gogf/gf/v2/container/gmap" func main() { m := gmap.New() // Set some values m.Set("hello", "world") m.Set("foo", "bar") // Get values safely fmt.Println(m.Get("hello")) // Output: world }
Pretty straightforward, right? But wait, there's more! ?
gmap comes packed with useful operations. Here are some you'll probably use often:
// Batch set multiple values m.Sets(g.MapAnyAny{ "key1": "value1", "key2": "value2", }) // Check if a key exists if m.Contains("key1") { fmt.Println("Found it!") } // Remove a key m.Remove("key1") // Get the map size size := m.Size() // Clear everything m.Clear() // Iterate over all items m.Iterator(func(k interface{}, v interface{}) bool { fmt.Printf("%v: %v\n", k, v) return true })
Let's look at a practical example. Here's how you might use gmap to create a simple caching layer:
func Cache(key string) (interface{}, error) { data := gmap.New() // Try cache first if cached := data.Get(key); cached != nil { return cached, nil } // Cache miss - get from database result := db.GetSomething(key) if result != nil { data.Set(key, result) } return result, nil }
Now for the exciting part - how does gmap stack up against Go's built-in sync.Map? Let's look at some scenarios.
Here's a benchmark that simulates high key collision:
func BenchmarkKeyConflict(b *testing.B) { m1 := gmap.New() m2 := sync.Map{} b.RunParallel(func(pb *testing.PB) { for pb.Next() { key := rand.Intn(10) // Limited key range m1.Set(key, key) m2.Store(key, key) } }) }
Results? gmap is about 3x faster! ? This is thanks to its smart sharding design that reduces lock contention.
Here are some things I learned the hard way so you don't have to:
Memory Usage: gmap uses more memory than regular maps due to its concurrent-safe design. For small maps or low-concurrency scenarios, stick with regular maps.
Key Types: Your keys must be comparable (support == and !=). For custom types, you'll need to implement Hash() and Equal() methods.
Iterator Behavior: The iterator takes a snapshot, so changes during iteration won't be visible until the next iteration.
// Example of iterator behavior m := gmap.New() m.Set("key1", "value1") go func() { time.Sleep(time.Millisecond) m.Set("key2", "value2") // Won't be seen in current iteration }() m.Iterator(func(k, v interface{}) bool { fmt.Printf("%v: %v\n", k, v) return true })
gmap shines when:
gmap is a powerful tool in the Go developer's toolkit. While it's not a one-size-fits-all solution, it can significantly improve performance in the right scenarios.
Remember:
Have you used gmap in your projects? I'd love to hear about your experiences in the comments! ?
Happy coding! ?
The above is the detailed content of gmap in GoFrame: A Deep Dive into High-Performance Concurrent Maps. For more information, please follow other related articles on the PHP Chinese website!