This commit is contained in:
Alexander Yastrebov 2026-05-21 21:58:06 -07:00 committed by GitHub
commit 163e100216
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 134 additions and 24 deletions

View file

@ -66,7 +66,7 @@ type accountCache struct {
keydir string keydir string
watcher *watcher watcher *watcher
mu sync.Mutex mu sync.Mutex
all []accounts.Account byURL map[accounts.URL][]accounts.Account
byAddr map[common.Address][]accounts.Account byAddr map[common.Address][]accounts.Account
throttle *time.Timer throttle *time.Timer
notify chan struct{} notify chan struct{}
@ -76,6 +76,7 @@ type accountCache struct {
func newAccountCache(keydir string) (*accountCache, chan struct{}) { func newAccountCache(keydir string) (*accountCache, chan struct{}) {
ac := &accountCache{ ac := &accountCache{
keydir: keydir, keydir: keydir,
byURL: make(map[accounts.URL][]accounts.Account),
byAddr: make(map[common.Address][]accounts.Account), byAddr: make(map[common.Address][]accounts.Account),
notify: make(chan struct{}, 1), notify: make(chan struct{}, 1),
fileC: fileCache{all: mapset.NewThreadUnsafeSet[string]()}, fileC: fileCache{all: mapset.NewThreadUnsafeSet[string]()},
@ -88,8 +89,11 @@ func (ac *accountCache) accounts() []accounts.Account {
ac.maybeReload() ac.maybeReload()
ac.mu.Lock() ac.mu.Lock()
defer ac.mu.Unlock() defer ac.mu.Unlock()
cpy := make([]accounts.Account, len(ac.all)) cpy := make([]accounts.Account, 0, len(ac.byURL))
copy(cpy, ac.all) for _, accs := range ac.byURL {
cpy = append(cpy, accs...)
}
sort.SliceStable(cpy, func(i, j int) bool { return cpy[i].URL.Cmp(cpy[j].URL) < 0 })
return cpy return cpy
} }
@ -104,14 +108,11 @@ func (ac *accountCache) add(newAccount accounts.Account) {
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.Cmp(newAccount.URL) >= 0 }) if accs, ok := ac.byURL[newAccount.URL]; ok && slices.Contains(accs, newAccount) {
if i < len(ac.all) && ac.all[i] == newAccount {
return return
} }
// newAccount is not in the cache. // newAccount is not in the cache.
ac.all = append(ac.all, accounts.Account{}) ac.byURL[newAccount.URL] = append(ac.byURL[newAccount.URL], newAccount)
copy(ac.all[i+1:], ac.all[i:])
ac.all[i] = newAccount
ac.byAddr[newAccount.Address] = append(ac.byAddr[newAccount.Address], newAccount) ac.byAddr[newAccount.Address] = append(ac.byAddr[newAccount.Address], newAccount)
} }
@ -120,7 +121,12 @@ func (ac *accountCache) delete(removed accounts.Account) {
ac.mu.Lock() ac.mu.Lock()
defer ac.mu.Unlock() defer ac.mu.Unlock()
ac.all = removeAccount(ac.all, removed) if bu := removeAccount(ac.byURL[removed.URL], removed); len(bu) == 0 {
delete(ac.byURL, removed.URL)
} else {
ac.byURL[removed.URL] = bu
}
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 {
@ -132,11 +138,14 @@ func (ac *accountCache) delete(removed accounts.Account) {
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 }) url := accounts.URL{Scheme: KeyStoreScheme, Path: path}
if accs, ok := ac.byURL[url]; ok {
if i < len(ac.all) && ac.all[i].URL.Path == path { removed := accs[0]
removed := ac.all[i] if len(accs) == 1 {
ac.all = append(ac.all[:i], ac.all[i+1:]...) delete(ac.byURL, url)
} else {
ac.byURL[url] = accs[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 {
@ -166,24 +175,34 @@ func removeAccount(slice []accounts.Account, elem accounts.Account) []accounts.A
// The exact matching rules are explained by the documentation of accounts.Account. // The exact matching rules are explained by the documentation of accounts.Account.
// Callers must hold ac.mu. // Callers must hold ac.mu.
func (ac *accountCache) find(a accounts.Account) (accounts.Account, error) { func (ac *accountCache) find(a accounts.Account) (accounts.Account, error) {
// Limit search to address candidates if possible.
matches := ac.all
if (a.Address != common.Address{}) {
matches = ac.byAddr[a.Address]
}
if a.URL.Path != "" { if a.URL.Path != "" {
// If only the basename is specified, complete the path. // If only the basename is specified, complete the path.
if !strings.ContainsRune(a.URL.Path, filepath.Separator) { if !strings.ContainsRune(a.URL.Path, filepath.Separator) {
a.URL.Path = filepath.Join(ac.keydir, a.URL.Path) a.URL.Path = filepath.Join(ac.keydir, a.URL.Path)
} }
for i := range matches { }
if matches[i].URL == a.URL { // Limit search to address candidates if possible.
return matches[i], nil var matches []accounts.Account
if (a.Address != common.Address{}) {
matches = ac.byAddr[a.Address]
if a.URL.Path != "" {
for i := range matches {
if matches[i].URL == a.URL {
return matches[i], nil
}
} }
} }
if (a.Address == common.Address{}) { } else {
if a.URL.Path != "" {
if accs, ok := ac.byURL[a.URL]; ok {
return accs[0], nil
}
return accounts.Account{}, ErrNoMatch return accounts.Account{}, ErrNoMatch
} }
matches = make([]accounts.Account, 0, len(ac.byURL))
for _, accs := range ac.byURL {
matches = append(matches, accs...)
}
} }
switch len(matches) { switch len(matches) {
case 1: case 1:
@ -193,7 +212,7 @@ func (ac *accountCache) find(a accounts.Account) (accounts.Account, error) {
default: default:
err := &AmbiguousAddrError{Addr: a.Address, Matches: make([]accounts.Account, len(matches))} err := &AmbiguousAddrError{Addr: a.Address, Matches: make([]accounts.Account, len(matches))}
copy(err.Matches, matches) copy(err.Matches, matches)
slices.SortFunc(err.Matches, byURL) slices.SortStableFunc(err.Matches, byURL)
return accounts.Account{}, err return accounts.Account{}, err
} }
} }

View file

@ -17,6 +17,7 @@
package keystore package keystore
import ( import (
"encoding/binary"
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand"
@ -414,3 +415,93 @@ func forceCopyFile(dst, src string) error {
} }
return os.WriteFile(dst, data, 0644) return os.WriteFile(dst, data, 0644)
} }
func BenchmarkAdd(b *testing.B) {
for _, preload := range []int{10, 100, 1000, 1_000_000} {
b.Run(fmt.Sprintf("preload=%d", preload), func(b *testing.B) {
benchmarkAdd(b, preload)
})
}
}
func benchmarkAdd(b *testing.B, preload int) {
dir := filepath.Join("testdata", "dir")
cache, _ := newAccountCache(dir)
cache.watcher.running = true // prevent unexpected reloads
for i := range preload {
acc := accounts.Account{
URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/preload%08x", i)},
}
binary.NativeEndian.PutUint64(acc.Address[0:], uint64(i))
cache.add(acc)
}
b.ResetTimer()
b.ReportAllocs()
for i := range b.N {
acc := accounts.Account{
URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/bench%08x", i)},
}
binary.NativeEndian.PutUint64(acc.Address[12:], uint64(i))
cache.add(acc)
}
}
func BenchmarkFind(b *testing.B) {
for _, preload := range []int{10, 100, 1000, 1_000_000} {
b.Run(fmt.Sprintf("preload=%d", preload), func(b *testing.B) {
benchmarkFind(b, preload)
})
}
}
func benchmarkFind(b *testing.B, preload int) {
dir := filepath.Join("testdata", "dir")
cache, _ := newAccountCache(dir)
cache.watcher.running = true // prevent unexpected reloads
for i := range preload {
acc := accounts.Account{
URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/account%08x", i)},
}
binary.NativeEndian.PutUint64(acc.Address[0:], uint64(i))
cache.add(acc)
}
b.Run("by address", func(b *testing.B) {
acc := accounts.Account{}
binary.NativeEndian.PutUint64(acc.Address[0:], uint64(preload/2))
b.ResetTimer()
b.ReportAllocs()
for range b.N {
cache.find(acc)
}
})
b.Run("by path", func(b *testing.B) {
acc := accounts.Account{
URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/account%08x", preload/2)},
}
b.ResetTimer()
b.ReportAllocs()
for range b.N {
cache.find(acc)
}
})
b.Run("ambiguous", func(b *testing.B) {
acc := accounts.Account{}
b.ResetTimer()
b.ReportAllocs()
for range b.N {
cache.find(acc)
}
})
}