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.
This commit is contained in:
Felix Lange 2017-10-07 00:04:06 +02:00
parent d6fa6eabcf
commit 3cfbfdcd4e

View file

@ -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) { func (ac *accountCache) deleteByFile(path string) {
ac.mu.Lock() ac.mu.Lock()
defer ac.mu.Unlock() 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] 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 { if ba := removeAccount(ac.byAddr[removed.Address], removed); len(ba) == 0 {
delete(ac.byAddr, removed.Address) delete(ac.byAddr, removed.Address)
} else { } else {