From ce20d5658f572d637d0bb196a2df469cd99b706d Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 06:16:35 +0800 Subject: [PATCH] provider: Catalog's list is made once with room for every entry catalogKeeping cloned the groups' entries at their size and grew the list by append for every provider's model, about twice the list it gave. It is now made once with room for the groups and every entry. Real accounts (sandbox copy of the owner's HOME, network blocked), 40 polls of /api/state, /api/providers and drift, pprof alloc_space between two heap snapshots, bb478f0e5 (mshare) vs a build with this and the next commit (mall): catalogKeeping flat 21.1 MB -> 13.5 MB, cum 23.7 MB -> 14.0 MB /api/state and drift bodies byte-identical, /api/providers equal but for `fetched` and `lapsed` (a refused refresh in the blocked sandbox) the total went 584 -> 613 MB, the rise in paths neither commit touches (providerInfoWith +16 MB, agentFields +9 MB): runs of the same build vary that much TestCatalogMadeOnce: a 400-model provider, the catalog held; making it allocates under 1.5x the list. Broken by hand on mote (the clone put back): "a catalog of 595200 bytes allocated 1563137 to make". Run on mote at this tree plus the next commit: gofmt; go vet darwin and windows (provider, gui, agent); GOOS=linux go vet ./internal/provider ok; the new test with TestCatalogKeptAcrossRequests and TestCatalogForKeptAcrossPolls 3/3 ok; go test ./internal/provider ok (118s); go test ./internal/gui: the known reds #4514, #3949 and TestColdTurnsTaken (#4510, 0.00s; passes alone 3/3 and in the next full gui run). Not run: the full ./... suite. --- internal/provider/catalog_made_once_test.go | 51 +++++++++++++++++++++ internal/provider/models.go | 6 ++- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 internal/provider/catalog_made_once_test.go diff --git a/internal/provider/catalog_made_once_test.go b/internal/provider/catalog_made_once_test.go new file mode 100644 index 000000000..30c0a22e0 --- /dev/null +++ b/internal/provider/catalog_made_once_test.go @@ -0,0 +1,51 @@ +package provider + +import ( + "path/filepath" + "runtime" + "slices" + "strconv" + "testing" + "time" + "unsafe" +) + +// Catalog's list is made once with room for every entry: cloned at the +// groups' size and grown by append for the providers' models, it was 29 +// MB of what forty polls of the Providers page allocated with real +// accounts, about twice the list it gave. +func TestCatalogMadeOnce(t *testing.T) { + isolate(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")) + defer func(d, k time.Duration) { heldFor, keptFor = d, k }(heldFor, keptFor) + heldFor, keptFor = time.Minute, time.Minute + var models []string + for i := range 400 { + models = append(models, "m"+strconv.Itoa(i)) + } + if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: models}); err != nil { + t.Fatal(err) + } + defer Hold()() + cat := Catalog() // the entries and groups, built and held + if len(cat) < 400 || !slices.ContainsFunc(cat, func(e Entry) bool { return e.ID == "relay/m399" }) { + t.Fatalf("the catalog: %d entries", len(cat)) + } + list := uint64(len(cat)) * uint64(unsafe.Sizeof(Entry{})) + var all uint64 + const runs = 10 + for range runs { + var m0, m1 runtime.MemStats + runtime.ReadMemStats(&m0) + Catalog() + runtime.ReadMemStats(&m1) + all += m1.TotalAlloc - m0.TotalAlloc + } + if per := all / runs; per > list*3/2 { + t.Fatalf("a catalog of %d bytes allocated %d to make", list, per) + } +} diff --git a/internal/provider/models.go b/internal/provider/models.go index f5a7aaf38..bff1e596a 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -2779,7 +2779,11 @@ func SubscriptionKept(e Entry) bool { // catalogKeeping is Catalog with the unlisted models keep says, too. func catalogKeeping(keep func(Entry) bool) []Entry { entries := providerEntries() - out := slices.Clone(groupEntries(entries)) // held: the caller's to change + groups := groupEntries(entries) // held: copied into the caller's own + // made once with room for every entry: cloned at the groups' size and + // grown by append for the providers' models, it was 29 MB of what forty + // polls of the Providers page allocated with real accounts + out := append(make([]Entry, 0, len(groups)+len(entries)), groups...) kept := unlistedByGroups(entries) for _, e := range entries { if !e.Provider.Unlisted && len(kept[e.ID]) == 0 || keep != nil && keep(e) {