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.
This commit is contained in:
yetone
2026-11-01 03:10:49 +08:00
parent 3182a66586
commit 80bcef18f0
4 changed files with 60 additions and 4 deletions
+1 -1
View File
@@ -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
}
+10 -2
View File
@@ -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
}
@@ -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")
}
}
+1 -1
View File
@@ -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)