From 80bcef18f0a20cb6ea69ccf6ffe39e8d2e9ed84b Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 02:34:51 +0800 Subject: [PATCH] provider: PublicRef reads the groups held without copying them PublicRef is asked for every model a page lists (the Providers page, every agent's picker). Each call went through Groups(), which copies every group and its rules so the caller can change them, to read one group's Spelled and Ref. With the owner's real config that was 115 MB of the 2.6 GB a v0.1.2432 magpie allocated over 20 window polls. sharedGroups() is the held slice itself, for callers that only read it: PublicRef, the families list (visible.go) and DisabledGroup, which still hands its caller a copy of the one group it found. TestPublicRefCopiesNoGroups (41 groups with members and a rule, under provider.Hold and filememo.Hold as a request runs): PublicRef takes far fewer allocations than a copy of the groups, and DisabledGroup's caller can't write into the groups held. Broken by hand on mote: PublicRef back on Groups() is red (165 allocations against 164 for a copy), and DisabledGroup returning the shared group is red. Run on mote at the rebased head (go test -timeout 45m): internal/provider ok 125s, internal/agent ok 207s. go vet of internal/provider on darwin, windows and linux ok; gofmt -l clean. On the Windows box internal/provider has the same 14 reds at origin/main f9ca3dd44 with and without this change (#4115, #2808, #4446, #4633), and no other. --- internal/provider/autoref.go | 2 +- internal/provider/group.go | 12 +++++- internal/provider/publicref_shared_test.go | 48 ++++++++++++++++++++++ internal/provider/visible.go | 2 +- 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 internal/provider/publicref_shared_test.go diff --git a/internal/provider/autoref.go b/internal/provider/autoref.go index 6a30ecbd3..2a4a41a9b 100644 --- a/internal/provider/autoref.go +++ b/internal/provider/autoref.go @@ -147,7 +147,7 @@ func PublicRef(ref string) string { if i := strings.LastIndex(gid, ":"); i > 0 { gid, level = gid[:i], gid[i:] } - g, ok := groupOf(Groups(), gid) + g, ok := groupOf(sharedGroups(), gid) if !ok || g.Spelled == "" { return ref } diff --git a/internal/provider/group.go b/internal/provider/group.go index 2866097c5..861cf1f41 100644 --- a/internal/provider/group.go +++ b/internal/provider/group.go @@ -399,6 +399,14 @@ func groupsIn(entries []Entry) []Group { return cloneGroups(heldFrom("groups", entries, buildGroupsIn)) } +// sharedGroups is Groups for a caller that only reads them: no copy, so +// nothing it is given may be changed. PublicRef, asked for every model a +// page lists, copied every group and its rules each time (115 MB of a live +// magpie's 2.6 GB in a minute and a half). +func sharedGroups() []Group { + return heldFrom("groups", providerEntries(), buildGroupsIn) +} + // cloneGroups is gs with nothing shared with it: no slice, map or pointer // of a group or its rules. func cloneGroups(gs []Group) []Group { @@ -1760,8 +1768,8 @@ func DisabledGroup(id string) (Group, bool) { } gid = strings.TrimPrefix(gid, GroupPrefix) } - if g, ok := groupOf(Groups(), gid); ok && g.Disabled { - return g, true + if g, ok := groupOf(sharedGroups(), gid); ok && g.Disabled { + return cloneGroups([]Group{g})[0], true } return Group{}, false } diff --git a/internal/provider/publicref_shared_test.go b/internal/provider/publicref_shared_test.go new file mode 100644 index 000000000..1c79e13d1 --- /dev/null +++ b/internal/provider/publicref_shared_test.go @@ -0,0 +1,48 @@ +package provider + +import ( + "fmt" + "testing" + + "github.com/yetone/magpie/internal/filememo" +) + +// PublicRef reads the groups magpie holds without copying them: it is asked +// for every model a page lists, and each copied every group and its rules +// (115 MB of a live magpie's 2.6 GB in a minute and a half). A group saved +// since is seen, and DisabledGroup's caller still gets a group of its own. +func TestPublicRefCopiesNoGroups(t *testing.T) { + autoRefHome(t) + if err := SaveGroup(Group{Name: "Mine", Members: []string{"a/x", "b/x"}}); err != nil { + t.Fatal(err) + } + // the groups a user with many keeps: each with members and a rule + for i := range 40 { + if err := SaveGroup(Group{Name: fmt.Sprintf("G%d", i), Members: []string{"a/x", "b/x", "c/x"}, + Rules: []Rule{{Use: "a/x", Agents: []string{"claude"}, Folders: []string{"/w"}}}}); err != nil { + t.Fatal(err) + } + } + defer Hold()() + defer filememo.Hold()() + if got := PublicRef("group/auto-claude-opus-5-5:high[1m]"); got != "auto/claude-opus-5.5:high[1m]" { + t.Fatalf("the found group's ref: %q", got) + } + copied := testing.AllocsPerRun(5, func() { Groups() }) + asked := testing.AllocsPerRun(20, func() { PublicRef("group/auto-claude-opus-5-5") }) + if asked*10 > copied { + t.Fatalf("PublicRef took %.0f allocations, a copy of the groups %.0f", asked, copied) + } + + if err := SwitchGroup("mine", false); err != nil { + t.Fatal(err) + } + d, ok := DisabledGroup("group/mine") + if !ok { + t.Fatal("a group switched off since is not found off") + } + d.Members[0] = "changed" + if g, _ := groupOf(sharedGroups(), "mine"); g.Members[0] == "changed" { + t.Fatal("DisabledGroup's caller wrote into the groups held") + } +} diff --git a/internal/provider/visible.go b/internal/provider/visible.go index dc0de8563..99b2b08ea 100644 --- a/internal/provider/visible.go +++ b/internal/provider/visible.go @@ -315,7 +315,7 @@ func Families() []string { for _, p := range All() { add(p.Family) } - for _, g := range Groups() { + for _, g := range sharedGroups() { add(g.Family) } slices.Sort(out)