gorilla/context — pull request #12 by tylerb (2014-05-20 03:17:04+00:00)
Title: Use a RWMutex in place of a Mutex

Fix #2

### Reasoning

RWMutex shows significant benefits in contentious environments. There is a small performance hit incurred in low-contention, single processor environments. However, the typical use case will be multi-processor, high contention, so this change seems justified.

### Benchmarks

A benchmark function was written to test parallel getting and setting across many goroutines. Please see [context_test.go:BenchmarkMutex](https://github.com/tylerb/context/blob/master/context_test.go#L107) for the full code.

The following benchmarks are the results when using the original Mutex, the new RWMutex and removing the defer calls:

Mutex:
```
BenchmarkMutex	200	9787629 ns/op
BenchmarkMutex-2	100	16161809 ns/op
BenchmarkMutex-8	100	21128937 ns/op
BenchmarkMutex-16	100	22455500 ns/op
BenchmarkMutex-32	100	22477323 ns/op
```

RWMutex:
```
BenchmarkMutex	200	9990271 ns/op
BenchmarkMutex-2	500	6410595 ns/op
BenchmarkMutex-8	500	3928236 ns/op
BenchmarkMutex-16	500	3946676 ns/op
BenchmarkMutex-32	500	3963064 ns/op
```

RWMutex, no defer:
```
BenchmarkMutex	500	5535964 ns/op
BenchmarkMutex-2	500	4876897 ns/op
BenchmarkMutex-8	500	3712937 ns/op
BenchmarkMutex-16	500	3633307 ns/op
BenchmarkMutex-32	500	3661723 ns/op
```

Please review this pull request and let me know if the benchmark needs to be changed, or additional benchmarks need to be added.

Thank you.