-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sync: RWMutex.RLock is 2x slower than Mutex.Lock #38813
Comments
|
I suspect that your test is more about collisions on the single output file when using |
@ianlancetaylor , thanks for the analysis and valuable insights. That makes sense to me. So the single output file really became the point of contention for the goroutines. I did another experiment and that validates your point. Now, instead of writing to a file, I simply append the values to a temporary slice within the goroutine (which will be automatically garbage collected) and now the results make more sense. Benchmark results: code link: https://play.golang.com/p/zf0n9ytgH4E |
Id be concerned that your program is benchmarking heap growth and gc performance. Have you tried rewriting your benchmark has a regular testing.B style benchmark. If you did that you would benefit from the tools like -count, -cpu and benchstat. |
Thanks. I don't think there is any action to take here, so I'm inclined to close this issue. We're always interested in performance improvements, of course, but I don't see anything here that leads to any promising approach to better performance. |
@ianlancetaylor , sure I agree. Thanks again for the clarification. |
RWMutex could stand a bit of an overhaul in general. (See also #17973.) |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Ran a benchmark with sync.Mutex vs sync.RWMutex and found the mutex.Lock() to be 2x faster than the RWMutex.RLock()
Description: Two scenarios (with lock and rwlock) with 1000 goroutines contending to read data from a slice (size = 10) and write to independent files.
https://play.golang.com/p/RZ-bE3OJ4H5
Output: Lock vs RWLocks() in seconds
Locks RWLocks
2.589610687s 5.52346426s
2.693844854s 6.13264221s
2.574230731s 6.857576762s
What did you expect to see?
Expected to see RWLock() to be atleast twice as fast than Lock()
What did you see instead?
Lock() is faster than RWLock() by almost 2X, then what's the point of having the RWMutex in the first place ?
The text was updated successfully, but these errors were encountered: