Skip to content

Use sync.Mutex rather than RWMutex for simple protections

Arran Walker requested to merge ajwalker/rw-mutex-where-needed into main

What does this MR do?

Uses sync.Mutex instead of RWMutex for simple protections.

Why was this MR needed?

https://github.com/golang/go/wiki/CodeReviewConcurrency#rwmutex

Locking with RWMutex incurs extra overhead compared to plain sync.Mutex, and, furthermore, there might be some scalability issue with the current implementation of RWMutex in Go. Unless the case is very clear (such as an RWMutex used to synchronize many read-only operations each lasting hundreds of milliseconds or more, and the writes which require an exclusive lock seldom happen), there should be some benchmarks proving that RWMutex indeed helps to improve the performance.

I've changed just the most obvious instances of using RWMutex for really simple protections.

What's the best way to test this MR?

N/A:

  • sync.RWMutex is intended to be used to improve performance in very specific cases. Using sync.Mutex won't change the behaviour.
  • sync.Mutex doesn't have RLock/RUnlock, so compiler errors would occur if a change was missing.

What are the relevant issue numbers?

Closes #28373 (closed)

Edited by Arran Walker

Merge request reports