provider: the balance history is parsed once while the file is unchanged
Every usage read (the tray's, the panel's, a minute apart at most while something asks) gave each balance its trend by reading and parsing all of balance-history.json, 365 KB on the owner's real accounts, and sharedBalanceHist parsed it a second time. It showed as 6% of an idle tray's CPU profile, all of it encoding/json. balanceHistShared now reads it through filememo, kept while the file's size and time are the same (and, written just now, its bytes). readBalanceHist, for the two callers that change and write it (noteBalanceHistory's new points, fold's moved keys), is a copy of that with each key's points its own, so nothing changes the shared parse. The trends and shareBalances only read, and use the shared one. Run (mote): go vet ./internal/provider; go test -run 'Balance|Fold|Unread|Quota|Shared' ./internal/provider ok (writes and reads within one second among them).
This commit is contained in:
@@ -28,6 +28,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
"github.com/yetone/magpie/internal/lastgood"
|
||||
"github.com/yetone/magpie/internal/lazyre"
|
||||
)
|
||||
@@ -75,16 +76,34 @@ var balanceHistMu sync.Mutex
|
||||
|
||||
func balanceHistPath() string { return filepath.Join(filepath.Dir(Path()), "balance-history.json") }
|
||||
|
||||
func readBalanceHist() balanceHist {
|
||||
h := balanceHist{}
|
||||
if b, err := os.ReadFile(balanceHistPath()); err == nil {
|
||||
// balanceHistShared is the file as last parsed, while it is unchanged
|
||||
// (filememo): every usage read gives each balance its trend from it, and
|
||||
// the file runs to hundreds of KB. It is shared: never changed.
|
||||
func balanceHistShared() balanceHist {
|
||||
h, err := filememo.Read("balance-history", balanceHistPath(), func(b []byte) (balanceHist, error) {
|
||||
h := balanceHist{}
|
||||
if json.Unmarshal(b, &h) != nil || h == nil {
|
||||
return balanceHist{}
|
||||
return balanceHist{}, nil
|
||||
}
|
||||
return h, nil
|
||||
})
|
||||
if err != nil || h == nil {
|
||||
return balanceHist{}
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// readBalanceHist is the file for a caller to change and write: a copy of
|
||||
// balanceHistShared, each key's points its own.
|
||||
func readBalanceHist() balanceHist {
|
||||
h := balanceHistShared()
|
||||
out := make(balanceHist, len(h))
|
||||
for k, pts := range h {
|
||||
out[k] = slices.Clone(pts)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func writeBalanceHist(h balanceHist) {
|
||||
b, err := json.Marshal(h)
|
||||
if err != nil {
|
||||
@@ -242,7 +261,7 @@ func noteBalanceHistory(qs []SubscriptionQuota, now time.Time) {
|
||||
continue
|
||||
}
|
||||
if h == nil {
|
||||
h = readBalanceHist()
|
||||
h = balanceHistShared() // read only from here on
|
||||
}
|
||||
key := quotaHistKey(q.Provider, q.User)
|
||||
cs := balanceCurrencies(q.Balance)
|
||||
|
||||
@@ -72,7 +72,7 @@ func sharedBalanceHist(cards []SubscriptionQuota) balanceHist {
|
||||
if sharesAccountBalance(q) {
|
||||
balanceHistMu.Lock()
|
||||
defer balanceHistMu.Unlock()
|
||||
return readBalanceHist()
|
||||
return balanceHistShared() // shareBalances only reads it
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user