diff --git a/storage.go b/storage.go index c46fa1a7f..8e65de0b6 100644 --- a/storage.go +++ b/storage.go @@ -27,18 +27,20 @@ type entry struct { type _storage map[*cobra.Command]*entry -var storageMutex sync.Mutex +var storageMutex sync.RWMutex func (s _storage) get(cmd *cobra.Command) (e *entry) { + storageMutex.RLock() + var ok bool if e, ok = s[cmd]; !ok { + storageMutex.RUnlock() storageMutex.Lock() defer storageMutex.Unlock() - - if e, ok = s[cmd]; !ok { - e = &entry{} - s[cmd] = e - } + e = &entry{} + s[cmd] = e + } else { + storageMutex.RUnlock() } return } diff --git a/storage_test.go b/storage_test.go index 42bd34598..a6a892710 100644 --- a/storage_test.go +++ b/storage_test.go @@ -76,20 +76,22 @@ func TestCheck(t *testing.T) { } } -// BenchmarkStorage test for concurrent map read/write +// BenchmarkStorage tests for concurrent map read/write func BenchmarkStorage(b *testing.B) { cmd := &cobra.Command{} cmd2 := &cobra.Command{} - b.RunParallel(func(p *testing.PB) { - Gen(cmd).FlagCompletion(ActionMap{ - "flag1": ActionValues("a", "b"), - }) - Gen(cmd).PositionalCompletion(ActionValues("a", "b")) - - Gen(cmd2).FlagCompletion(ActionMap{ - "flag2": ActionValues("a", "b"), - }) - Gen(cmd).PositionalCompletion(ActionValues("a", "b")) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + // Gen(cmd).FlagCompletion(ActionMap{ + // "flag1": ActionValues("a", "b"), + // }) + Gen(cmd).PositionalCompletion(ActionValues("a", "b")) + + // Gen(cmd2).FlagCompletion(ActionMap{ + // "flag2": ActionValues("a", "b"), + // }) + Gen(cmd2).PositionalCompletion(ActionValues("a", "b")) + } }) }