Go Benchmark
Concurrent Map Access
Compare sync.Map against a RWMutex-protected map for concurrent reads and writes.
The classic map access is done by using the map[key] syntax. This implementation works fine in the most cases, but it is not thread-safe. A solution is to use the sync.Map type or to add a mutex to the map. This benchmark shows which implementation is the fastest.
Uses a sync.RWMutex to protect a plain map[int]int. Reads take a shared lock (RLock), writes take an exclusive lock (Lock). This is the standard approach when you control all access sites and need fine-grained locking with concurrent readers.
Uses the sync.Map type from the standard library. It is inherently thread-safe and needs no external locking. Optimised for keys that are stable over time. It performs best when entries are written once and read many times.
Contributors