Skip to content
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

Change cache mutex to sync.RWMutex #14

Merged
merged 1 commit into from
Dec 1, 2013
Merged

Change cache mutex to sync.RWMutex #14

merged 1 commit into from
Dec 1, 2013

Conversation

abursavich
Copy link
Contributor

A small tweak to the cache mutex that could help performance. Multiple reads to the cache don't need to exclude each other. Let the lock be held by an arbitrary number of readers or a single writer. Now once the cache is fully primed, there won't be any blocking.

@kisielk
Copy link
Contributor

kisielk commented Dec 1, 2013

Have you actually benchmarked this change? I'm not sure it would actually be faster since the actual read operation after a mutex lock is quite quick, and there is a fair bit more overhead to an RWMutex over a standard Mutex.

@abursavich
Copy link
Contributor Author

Nah. It doesn't make sense for RWMutex to exist if the read lock isn't more performant than a regular lock. I'll throw some benchmarks together.

@kisielk
Copy link
Contributor

kisielk commented Dec 1, 2013

It is more performant if the read lock needs to be held for a long time, but that time is much longer than it takes to read an entry from a map.

@abursavich
Copy link
Contributor Author

https://gist.github.com/abursavich/7729965

$ go test --bench .
PASS
Benchmark_TypeMap_Get-8 50000000            39.0 ns/op
Benchmark_RWMutex_RLock-8   50000000            48.4 ns/op
Benchmark_RWMutex_Lock-8    50000000            66.5 ns/op
Benchmark_Mutex_Lock-8  50000000            49.4 ns/op
Benchmark_Concurrent_RWMutex_RLock-8    50000000            39.5 ns/op
Benchmark_Concurrent_RWMutex_Lock-8 10000000           241 ns/op
Benchmark_Concurrent_Mutex_Lock-8   10000000           228 ns/op
ok      github.com/abursavich/schema    17.593s

RWMutex does seem to perform better. In certain use cases the difference is likely to be negligible, but in others it's more impressive.

@kisielk
Copy link
Contributor

kisielk commented Dec 1, 2013

Looks good. I did some testing of my own and it seems RLock is indeed faster, at least if you have GOMAXPROCS of 2 or greater.

kisielk added a commit that referenced this pull request Dec 1, 2013
Change cache mutex to sync.RWMutex
@kisielk kisielk merged commit 180b62f into gorilla:master Dec 1, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants