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.
49 lines
1.6 KiB
Go
49 lines
1.6 KiB
Go
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")
|
|
}
|
|
}
|