From 5aa0d55e72cda8ac4cf95b67b051959b91d8f183 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Mon, 23 Oct 2017 17:24:48 +0100 Subject: [PATCH] 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 --- plugin/pkg/cache/cache.go | 11 +++++++---- plugin/pkg/cache/cache_test.go | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/plugin/pkg/cache/cache.go b/plugin/pkg/cache/cache.go index 56cae2180..25faf93ea 100644 --- a/plugin/pkg/cache/cache.go +++ b/plugin/pkg/cache/cache.go @@ -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. diff --git a/plugin/pkg/cache/cache_test.go b/plugin/pkg/cache/cache_test.go index 2c92bf438..0c56bb9b3 100644 --- a/plugin/pkg/cache/cache_test.go +++ b/plugin/pkg/cache/cache_test.go @@ -29,3 +29,13 @@ func TestCacheLen(t *testing.T) { t.Fatalf("Cache size should %d, got %d", 2, l) } } + +func BenchmarkCache(b *testing.B) { + b.ReportAllocs() + + c := New(4) + for n := 0; n < b.N; n++ { + c.Add(1, 1) + c.Get(1) + } +}