mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 16:24:19 -04:00
plugin/pkg/cache: smarter locking (#1164)
Make the locking slightly smarter in Evict and add benchmark function. Seems a bit faster (there was some variance while performing these benchmarks) Master: BenchmarkCache-2 1000000 2317 ns/op 0 B/op 0 allocs/op BenchmarkCache-2 1000000 2032 ns/op 0 B/op 0 allocs/op This branch: BenchmarkCache-2 1000000 1806 ns/op 0 B/op 0 allocs/op BenchmarkCache-2 1000000 1809 ns/op 0 B/op 0 allocs/op
This commit is contained in:
11
plugin/pkg/cache/cache.go
vendored
11
plugin/pkg/cache/cache.go
vendored
@@ -95,19 +95,22 @@ func (s *shard) Remove(key uint32) {
|
||||
|
||||
// Evict removes a random element from the cache.
|
||||
func (s *shard) Evict() {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
key := -1
|
||||
|
||||
s.RLock()
|
||||
for k := range s.items {
|
||||
key = int(k)
|
||||
break
|
||||
}
|
||||
s.RUnlock()
|
||||
|
||||
if key == -1 {
|
||||
// empty cache
|
||||
return
|
||||
}
|
||||
delete(s.items, uint32(key))
|
||||
|
||||
// If this item is gone between the RUnlock and Lock race we don't care.
|
||||
s.Remove(uint32(key))
|
||||
}
|
||||
|
||||
// Get looks up the element indexed under key.
|
||||
|
||||
Reference in New Issue
Block a user