provider: MemberGroups and GroupKept are built once while a request holds

The providers' page asked MemberGroups for each provider and GroupKept
for each model, and each call built the groups again from every
provider's entries: 11.6 MB of what twenty window polls allocated with
real accounts (m2463 profile). Both now go through heldFrom, as the
other catalog reads do: built once per Hold, kept across requests
while the stamp stays, shared and only for reading.

Test: TestMemberGroupsBuiltOnceWhileHeld. 20 looks under one Hold,
each asking MemberGroups 12 times, build it once at most; a group
saved after (g5) is in the next look.

Run on mote, at this tree on dd7442f28:
- gofmt, go vet (darwin, windows, linux): ok
- the new test with TestUnlistedGroupKept, 3 times: ok
- broken by hand (MemberGroups built afresh each call): red, "20
  looks under one Hold built the member groups 20 times, want 1 at
  most"
- go test ./internal/provider: ok. TestModelSpecWinsOverTheListAndASync
  was red once in the run before (with only the test's assertion
  since fixed) and ok alone 3 times and in this run: #4637
- go test ./internal/gui: every test ok except the known
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners (#3949)

Not run: an A/B with the real-account sandbox for this change alone.
This commit is contained in:
yetone
2026-11-01 04:53:36 +08:00
parent 323159f1cc
commit 61b536d788
3 changed files with 56 additions and 3 deletions
+11 -2
View File
@@ -1593,9 +1593,18 @@ func GroupsWith(id string) []Group {
// by the model's "provider/model" id; a member fixed at an effort counts as
// its model, and a group removed is none. A provider kept for routing
// groups (Provider.Unlisted) reaches agents through these alone: a model
// of it in none of them is used by nothing.
// of it in none of them is used by nothing. Held while a request holds the
// catalog, so it is shared, only for reading: the providers' page asked
// for it once for each provider, 11.6 MB of what twenty polls allocated.
func MemberGroups() map[string][]string {
entries := providerEntries()
return heldFrom("member-groups", providerEntries(), memberGroupsIn)
}
// memberGroupsBuilt counts MemberGroups built, for a test.
var memberGroupsBuilt atomic.Int64
func memberGroupsIn(entries []Entry) map[string][]string {
memberGroupsBuilt.Add(1)
out := map[string][]string{}
for _, g := range groupsIn(entries) {
if g.Hidden {
@@ -0,0 +1,40 @@
package provider
import (
"slices"
"testing"
)
// The providers' page asked MemberGroups once for each provider, and each
// built the groups' members over the whole catalog again: 11.6 MB of
// what twenty polls allocated. While a request holds the catalog it is
// built once, and a group saved is in the next request's.
func TestMemberGroupsBuiltOnceWhileHeld(t *testing.T) {
groupHeldFixture(t)
look := func() map[string][]string {
defer Hold()()
var in map[string][]string
for range 12 {
in = MemberGroups()
}
return in
}
look()
before := memberGroupsBuilt.Load()
release := Hold()
for range 20 {
if gs := MemberGroups()["p2/m1"]; !slices.Contains(gs, GroupPrefix+"g4") {
t.Fatalf("p2/m1 is in %q, not group/g4", gs)
}
}
release()
if n := memberGroupsBuilt.Load() - before; n > 1 {
t.Fatalf("20 looks under one Hold built the member groups %d times, want 1 at most", n)
}
if err := SaveGroup(Group{ID: "g5", Name: "G5", Members: []string{"p5/m5"}, Routing: Ordered}); err != nil {
t.Fatal(err)
}
if gs := look()["p5/m5"]; !slices.Contains(gs, GroupPrefix+"g5") {
t.Fatalf("a group saved: p5/m5 is in %q, not group/g5", gs)
}
}
+5 -1
View File
@@ -2820,8 +2820,12 @@ func unlistedByGroups(entries []Entry) map[string][]string {
// names: a provider's editor names them, as a model its own switch put
// back for agents is still left out by them. A signed-in subscription's
// models, which agents keep (SubscriptionKept), aren't among them.
// Held as MemberGroups is: shared, only for reading.
func GroupKept() map[string][]string {
entries := providerEntries()
return heldFrom("group-kept", providerEntries(), groupKeptIn)
}
func groupKeptIn(entries []Entry) map[string][]string {
kept := unlistedByGroups(entries)
if len(kept) == 0 {
return nil