Concurrent Map Access — Go Benchmark
A benchmark to compare the performance of different concurrent map access implementations in Go.
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.
Mutex
Fastest (Read, 1 CPU)Slowest (Read, 32 CPUs)Fastest (Write, 1 CPU)Slowest (Write, 32 CPUs)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.
Sync
Fastest (Read, 32 CPUs)Slowest (Read, 1 CPU)Fastest (Write, 32 CPUs)Slowest (Write, 1 CPU)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 — performs best when entries are written once and read many times.
Contributors