Go Perf Atomics Vs Locks

Guides the atomics-vs-locks performance tradeoff in Go — typed sync/atomic ops (atomic.Int64/Pointer/Bool, 1.19) are a single CAS-style instruction, cheaper than a mutex uncontended but still cache-line-bouncing when contended; sync.Mutex's fast path is itself a CAS that only parks the goroutine when contended; RWMutex wins only for long read sections and is slower (scales worse) than a plain Mutex for short ones; the atomic.Pointer copy-on-write config swap; when lock-free isn't worth the correctness risk. Fires on "atomic vs mutex", "is RWMutex faster", "lock-free counter", "reduce locking overhead", "atomic.Pointer config". Routes copylock/atomic-API correctness to go-sync-primitives, memory ordering to go-race-and-memory-model, measuring to go-perf-block-mutex-profiles.

ctoth 9155c5a 3 files · 28.0 KB Updated

File contents

ctoth/golang-skills-plugin/tree/main/plugins/performant-golang/skills/go-perf-atomics-vs-locks commit 9155c5aa09

Frequently asked questions

npx skillmds@latest add ctoth/go-perf-atomics-vs-locks