diff --git a/internal/provider/logins.go b/internal/provider/logins.go index 430ed43b5..b80691b14 100644 --- a/internal/provider/logins.go +++ b/internal/provider/logins.go @@ -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 diff --git a/internal/provider/logins_shared_test.go b/internal/provider/logins_shared_test.go new file mode 100644 index 000000000..e4ee957cc --- /dev/null +++ b/internal/provider/logins_shared_test.go @@ -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) + } +} diff --git a/internal/provider/plugin_accounts.go b/internal/provider/plugin_accounts.go index 5e0fcf736..8eccf7350 100644 --- a/internal/provider/plugin_accounts.go +++ b/internal/provider/plugin_accounts.go @@ -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 { diff --git a/internal/provider/side_logins.go b/internal/provider/side_logins.go index 114cf59a5..38f783e30 100644 --- a/internal/provider/side_logins.go +++ b/internal/provider/side_logins.go @@ -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)