Tackling contention: the monsters inside the `sync.Locker`
summary
Shows concrete measurement workflows using pprof and trace and the runtime mutex/block profiles to find which lock or cache line eats CPU seconds, with examples where all four seconds of work are spent waiting. Demonstrates fixes that actually reduce contention: shard state to cut contention roughly by the shard count, batch updates to lower synchronization frequency, replacing channels with mutexes for hot paths, and the tradeoffs of atomic compare-and-swap loops and sync.Map (fewer cache hurts, more pointer chasing). Advises measure β change β remeasure.
AI-written summary Β· about
chapters (14)
- 0:00Introduction to contention
- 1:06Measuring contention with tools
- 3:36Tracing contention in production
- 8:58Understanding measurement costs
- 10:51Sharding state to reduce contention
- 12:59Batching for performance
- 14:25Mutexes vs channels
- 16:55Why mutexes are fast
- 17:18False sharing in memory
- 20:40Read-write mutex pitfalls
- 22:06When to use sync.Map
- 23:08Atomic operations basics
- 25:17ABA problem and risks
- 27:48Q&A on atomic usage
