provider: listing accounts with nothing to change copies none of them

sideLogins and pluginLogins read the saved logins as a copy of the
whole list each time, to change it if a sign-in was new. The Providers
page and the panel list an agent's or a plugin's accounts a dozen times
a poll, and almost always nothing changes: 61 MB of what twenty window
polls allocated with real accounts. They now look at the logins as kept
(sharedLogins, read only), and a list that has a change makes its own
copy (readLogins) before it changes and writes it.

TestAccountListsCopyNoLoginsUnchanged: twenty lists of a side sign-in
and a plugin's accounts with nothing new copy the logins 0 times; a new
plugin account is still written and listed.

Run on mote (macOS 27):
- gofmt, go vet ./internal/provider (darwin, windows, linux): ok
- broken by hand (side_logins back on readLogins): red, "20 lists with
  nothing to change copied the accounts 40 times"
- the new test alone, -count=3: ok
- go test ./internal/provider ./internal/gateway: ok

Not run: the whole suite; internal/provider and the gateway that reads
its accounts are what this reaches.
This commit is contained in:
yetone
2026-11-01 04:37:22 +08:00
parent 178a781ff9
commit 88372a39d8
4 changed files with 91 additions and 20 deletions
+13 -1
View File
@@ -214,6 +214,18 @@ func validLogins(b []byte) bool {
}
func readLogins() []savedLogin {
loginsCopied.Add(1)
return slices.Clone(sharedLogins()) // callers change theirs
}
// loginsCopied counts readLogins' copies, for a test.
var loginsCopied atomic.Int64
// sharedLogins is readLogins without the caller's own copy: the accounts
// as kept, read only. A page's state lists a dozen agents' accounts, and
// a copy of the whole list for each was 61 MB of what twenty window polls
// allocated.
func sharedLogins() []savedLogin {
if settings.AccountDiscoveryOff() {
return nil
}
@@ -243,7 +255,7 @@ func readLogins() []savedLogin {
log.Printf("logins.json: %v; %v", err, ferr)
}
}
return slices.Clone(ls) // callers change theirs
return slices.Clip(ls) // an append makes the caller's own
}
// unreadBytes are a file's bytes, nil when it can't be read: for the log
+47
View File
@@ -0,0 +1,47 @@
package provider
import (
"testing"
"github.com/yetone/magpie/internal/plugin"
)
// Listing an agent's or a plugin's accounts with nothing to change looks
// at the accounts as kept: a copy of the whole list for each of a page's
// dozen lists was 61 MB of twenty window polls. A list with a change
// still makes its own copy and writes it.
func TestAccountListsCopyNoLoginsUnchanged(t *testing.T) {
signIn(t)
all := func(savedLogin) bool { return true }
pp := plugin.Provider{ID: "fakeco", Accounts: []plugin.Account{{Key: "k1", AccountID: "one"}}}
sideLogins("google", "a@example.com", all) // its own sign-in: written
pluginLogins(pp) // its account: written
before := loginsCopied.Load()
for range 20 {
sideLogins("google", "a@example.com", all)
if got := pluginLogins(pp); len(got) != 1 || got[0].User != "one" {
t.Fatalf("plugin accounts: %+v", got)
}
}
if n := loginsCopied.Load() - before; n != 0 {
t.Fatalf("20 lists with nothing to change copied the accounts %d times", n)
}
pp.Accounts = append(pp.Accounts, plugin.Account{Key: "k2", AccountID: "two"})
if got := pluginLogins(pp); len(got) != 2 {
t.Fatalf("a second account wasn't listed: %+v", got)
}
pp.Accounts = pp.Accounts[1:]
if got := pluginLogins(pp); len(got) != 1 || got[0].User != "two" {
t.Fatalf("a signed-out account is still listed: %+v", got)
}
n := 0
for _, l := range readLogins() {
if l.Agent == "plugin:fakeco" {
n++
}
}
if n != 1 {
t.Fatalf("logins.json holds %d of the plugin's accounts, want 1", n)
}
}
+30 -18
View File
@@ -225,34 +225,46 @@ func pluginLogins(pp plugin.Provider) []pluginLogin {
}
labels := pluginLabels(pp, named)
loginsMu.Lock()
ls := readLogins()
// looked at as kept, and copied only at the first change: nearly
// every list changes nothing, and a page's state lists each plugin's
ls := sharedLogins()
changed := false
have := map[string]bool{}
keep := ls[:0]
for _, l := range ls {
var keep []savedLogin
for i, l := range ls {
drop, edit := false, false
if l.Agent == agent {
if _, ok := byKey[l.Home]; !ok || have[l.Home] {
changed = true // signed out, or listed twice
continue
}
have[l.Home] = true
renamed := l.User != labels[l.Home]
if renamed {
l.User, changed = labels[l.Home], true
}
// the agent's own, hidden, shows again once the agent signs
// in anew: told by its sign-in's mark, else by another account
if l.Hidden != "" && own[l.Home] && (mark != "" && mark != l.Hidden || mark == "" && l.Hidden == hiddenNoMark && renamed) {
l.Hidden, changed = "", true
drop = true // signed out, or listed twice
} else {
have[l.Home] = true
renamed := l.User != labels[l.Home]
if renamed {
l.User, edit = labels[l.Home], true
}
// the agent's own, hidden, shows again once the agent signs
// in anew: told by its sign-in's mark, else by another account
if l.Hidden != "" && own[l.Home] && (mark != "" && mark != l.Hidden || mark == "" && l.Hidden == hiddenNoMark && renamed) {
l.Hidden, edit = "", true
}
}
}
keep = append(keep, l)
if (drop || edit) && !changed {
keep, changed = slices.Clone(ls[:i:i]), true
}
if changed && !drop {
keep = append(keep, l)
}
}
if changed {
ls = keep
}
ls = keep
for _, a := range pp.Accounts {
if !have[a.Key] {
if !changed {
ls, changed = slices.Clone(ls), true
}
ls = append(ls, savedLogin{Agent: agent, User: labels[a.Key], Home: a.Key, On: true, Seen: time.Now().UTC().Truncate(time.Second)})
changed = true
}
}
if changed {
+1 -1
View File
@@ -37,7 +37,7 @@ func sideLogins(agent, ownUser string, usable func(savedLogin) bool) []sideLogin
// logins.json.lock all the same (#1665: every page's read lists a
// dozen agents' accounts, each a lock). logins.json is only ever
// replaced whole (writePrivate), so the read is one write's file.
ls := readLogins()
ls := sharedLogins() // only looked at: a change reads its own copy
if _, change := keepSideLogins(ls, agent, ownUser, true); change {
loginsMu.Lock()
ls, _ = keepSideLogins(readLogins(), agent, ownUser, false)