gui: a page of accounts builds the agent list once, not once a row
accountLabel and the excluded-agents loop called agent.Find for each row,
and Find builds All() again each time: about 3,300 allocations a call.
Over 20 window polls that was 58 MB of the 1.4 GB a magpie allocated.
provider.Held keeps All() for the request that holds it (the gui's held
middleware holds every /api/ request), and agent.FindIn looks the name up
in that list. Outside a hold Held builds it as before, so a caller with
no request in hand gets the same answer.
TestAccountLabelsBuildTheAgentsOnce (sandbox HOME): outside a hold the
label's name and icon are the same; under Hold a row allocates 3 times
against 3302 to build every agent. Broken by hand on mote (agentNamed
back to agent.Find) it is red at 3305 a row.
Run on mote at this tree (on d714c4216): agent (846s) and provider
(459s) ok; gui red only in TestSettingsSaveKeepsWhatItDoesNotSend
(#4514, red on main) and TestMainWindowCorners (#3949, macOS 27). vet ok
on darwin and windows (agent, provider, gui) and linux (agent, provider;
gui needs cgo there).
This commit is contained in:
@@ -627,9 +627,11 @@ func Detected() []*Agent {
|
||||
}
|
||||
|
||||
// Find resolves a user-typed name (id, alias, or unique prefix).
|
||||
func Find(q string) (*Agent, error) {
|
||||
func Find(q string) (*Agent, error) { return FindIn(All(), q) }
|
||||
|
||||
// FindIn is Find among all, a list of All() the caller has built already.
|
||||
func FindIn(all []*Agent, q string) (*Agent, error) {
|
||||
q = strings.ToLower(strings.TrimSpace(q))
|
||||
all := All()
|
||||
var prefix []*Agent
|
||||
for _, a := range all {
|
||||
if strings.EqualFold(a.ID, q) { // codex@wsl:Ubuntu
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/agent"
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// Each account's row on the Providers page is named for its agent, and
|
||||
// agent.Find built every agent magpie knows (about a hundred, each with
|
||||
// its files looked at) again for each row: with the owner's config 58 MB
|
||||
// of the 1.4 GB a magpie allocated over 20 window polls. A request builds
|
||||
// them once for all its rows.
|
||||
func TestAccountLabelsBuildTheAgentsOnce(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("USERPROFILE", home)
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
||||
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
||||
codex, err := agent.Find("codex")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := provider.Provider{ID: "codex-a", Name: "Codex A", Account: &provider.Account{Agent: "codex", User: "a"}}
|
||||
if name, icon := accountLabel(p); name != codex.Name || icon != codex.Icon {
|
||||
t.Fatalf("not held: %q %q, want %q %q", name, icon, codex.Name, codex.Icon)
|
||||
}
|
||||
all := testing.AllocsPerRun(3, func() { agent.All() })
|
||||
|
||||
// as a GET /api/* is served (held)
|
||||
defer provider.Hold()()
|
||||
defer filememo.Hold()()
|
||||
accountLabel(p) // the request's first row builds them
|
||||
row := testing.AllocsPerRun(50, func() {
|
||||
if name, icon := accountLabel(p); name != codex.Name || icon != codex.Icon {
|
||||
t.Errorf("held: %q %q, want %q %q", name, icon, codex.Name, codex.Icon)
|
||||
}
|
||||
})
|
||||
t.Logf("a row: %.0f allocations; building the agents: %.0f", row, all)
|
||||
if row > all/20 {
|
||||
t.Errorf("a row took %.0f allocations against %.0f to build every agent: the agents were built for it", row, all)
|
||||
}
|
||||
}
|
||||
@@ -369,6 +369,14 @@ type accountJSON struct {
|
||||
WSL string `json:"wsl,omitempty"`
|
||||
}
|
||||
|
||||
// agentNamed is agent.Find among the agents built once for the request
|
||||
// (provider.Held). Each account's row named its agent, and Find built all
|
||||
// of them again for it: with the owner's config 58 MB of the 1.4 GB a
|
||||
// magpie allocated over 20 window polls.
|
||||
func agentNamed(q string) (*agent.Agent, error) {
|
||||
return agent.FindIn(provider.Held("agent.All", agent.All), q)
|
||||
}
|
||||
|
||||
// accountLabel is the name and logo an account is shown with: its agent's,
|
||||
// the subscription's for one no agent magpie configures, and a plugin's
|
||||
// provider's for a plugin's sign-in, whose agent is "plugin" (#694 listed a
|
||||
@@ -388,7 +396,7 @@ func accountLabel(p provider.Provider) (name, icon string) {
|
||||
} else if a.Agent == provider.ChatGPTAPIID {
|
||||
// a ChatGPT plan through OpenAI's API, not Codex's backend
|
||||
name, icon = "ChatGPT API", "openai"
|
||||
} else if ag, err := agent.Find(a.Agent); err == nil {
|
||||
} else if ag, err := agentNamed(a.Agent); err == nil {
|
||||
name, icon = ag.Name, ag.Icon
|
||||
} else if a.Agent == "cursor" {
|
||||
// a Cursor subscription is served by the gateway, not an agent magpie configures
|
||||
@@ -992,7 +1000,7 @@ func providersState() providersJSON {
|
||||
// a removed account as its row was named: a plugin's by the
|
||||
// provider it signs in to, not its agent "plugin" (#694)
|
||||
e.Name, e.Icon = accountLabel(p)
|
||||
} else if a, err := agent.Find(x.Agent); err == nil {
|
||||
} else if a, err := agentNamed(x.Agent); err == nil {
|
||||
e.Name, e.Icon = a.Name, a.Icon
|
||||
}
|
||||
s.Excluded = append(s.Excluded, e)
|
||||
|
||||
@@ -179,6 +179,10 @@ func StampNow() Stamp {
|
||||
roots: [3]rootStamp{stampOf(Path()), stampOf(settings.Path()), stampOf(catalog.CachePath())}}
|
||||
}
|
||||
|
||||
// Held is what build gives, built once while a request holds it (Hold),
|
||||
// for what each row of a page looks up: shared by them, so read only.
|
||||
func Held[T any](key string, build func() T) T { return heldOf(key, build) }
|
||||
|
||||
// heldOf is what build gives, built once while a request holds it.
|
||||
func heldOf[T any](key string, build func() T) T {
|
||||
held.Lock()
|
||||
|
||||
Reference in New Issue
Block a user