From 3cfbfdcd4e1980d624918f4b4299512706cf50e9 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Sat, 7 Oct 2017 00:04:06 +0200 Subject: [PATCH] accounts/keystore: improve deleteByFile sort.Search requires that the function returns true for all elements after the one it's looking for. Once we have the index, avoid another iteration over the whole slice to remove the element. --- accounts/keystore/account_cache.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/accounts/keystore/account_cache.go b/accounts/keystore/account_cache.go index 6946894920..c528f3f5d8 100644 --- a/accounts/keystore/account_cache.go +++ b/accounts/keystore/account_cache.go @@ -137,15 +137,15 @@ func (ac *accountCache) delete(removed accounts.Account) { } } -//deleteByFile removes an account referenced by the given path +// deleteByFile removes an account referenced by the given path. func (ac *accountCache) deleteByFile(path string) { ac.mu.Lock() defer ac.mu.Unlock() - i := sort.Search(len(ac.all), func(i int) bool { return ac.all[i].URL.Path == path }) + i := sort.Search(len(ac.all), func(i int) bool { return ac.all[i].URL.Path >= path }) - if i < len(ac.all) { + if i < len(ac.all) && ac.all[i].URL.Path == path { removed := ac.all[i] - ac.all = removeAccount(ac.all, removed) + ac.all = append(ac.all[:i], ac.all[i+1:]...) if ba := removeAccount(ac.byAddr[removed.Address], removed); len(ba) == 0 { delete(ac.byAddr, removed.Address) } else {