provider: a provider's models.dev models are read, not copied three times

Provider.available, the base of every Available and Exposed, made three
copies of a provider's catalog models on each call: catalog.Provider
cloned the built list, available appended it into a list of its own to
drop an id two catalogs share, and the list without experiments and
previews was grown from nothing again. On a page open with the owner's
accounts that was 74 MB of 952 MB allocated in 2m32s, the largest flat
allocator of all.

catalog.ProviderShared is the built list itself, for a reader
(catalog.Provider still hands out a copy). available reads it through
catalogsOf, which copies only when two catalogs are joined, and sizes
the released list before filling it. The shared list is never returned:
every way out of available returns Decorate's list, the released copy
or the signed-in account's own. The built list is clipped, so an append
to it reallocates.

Run: go test ./internal/provider ./internal/catalog on mote (ok,
provider 135s). Measured on mote's real-account sandbox (acctp, the
Usage and Providers pages polled for 2m32s, pageheap.sh), w41 = the
parent, w42 = this commit: allocations 993 -> 858 MB, available's
cumulative 144.5 -> 52.3 MB, CPU at the settle mark 3.32 -> 3.18 s;
footprint 130 MB and live heap ~76.7 MB both unchanged. The parent's
full suite at d1e823d5f (same Go tree) was 99 ok, 0 FAIL; this
commit's provider and catalog packages ran again, not the full suite.
This commit is contained in:
yetone
2026-11-13 05:15:44 +08:00
parent ac15036ab4
commit e6184b368a
2 changed files with 49 additions and 14 deletions
+10 -3
View File
@@ -1217,14 +1217,21 @@ func APIOf(providerID, modelID string) string {
// A vendor's own /models answer, once fetched, is layered over it by the
// provider package (see provider.Provider.Available).
func Provider(id string) []Model {
return slices.Clone(ProviderShared(id))
}
// ProviderShared is Provider without the copy, for a caller that only
// reads it: every provider's model list is built from it on each look at
// the catalog, and the copies were a third of what building them took.
func ProviderShared(id string) []Model {
loadMu.Lock()
out, ok := built[id]
at := gen
loadMu.Unlock()
if ok {
return slices.Clone(out)
return out
}
out = provider(load(), id)
out = slices.Clip(provider(load(), id))
loadMu.Lock()
if at == gen {
if built == nil {
@@ -1233,7 +1240,7 @@ func Provider(id string) []Model {
built[id] = out
}
loadMu.Unlock()
return slices.Clone(out)
return out
}
func provider(all map[string]mdProvider, id string) []Model {
+39 -11
View File
@@ -60,15 +60,9 @@ func (p Provider) available() []catalog.Model {
if signedIn {
known = p.Account.models()
} else {
seen := map[string]bool{}
for _, id := range p.Catalogs() {
for _, m := range catalog.Provider(id) {
if !seen[m.ID] {
seen[m.ID] = true
known = append(known, m)
}
}
}
// read only below: every way out of here returns a new list
// (Decorate's, or the one without previews) or none of known
known = catalogsOf(p.Catalogs())
}
if live, _, ok := p.live(); ok && (p.Account == nil || !p.Account.magpieList) {
if p.Account != nil && p.Account.unusable != nil {
@@ -127,15 +121,49 @@ func (p Provider) available() []catalog.Model {
return ms
}
}
var out []catalog.Model
n := 0
for _, m := range known {
if !strings.Contains(m.ID, "-exp") && !strings.Contains(m.ID, "preview") {
if released(m) {
n++
}
}
if n == 0 {
return nil
}
out := make([]catalog.Model, 0, n)
for _, m := range known {
if released(m) {
out = append(out, m)
}
}
return out
}
// released is a catalog's model that is not an experiment or a preview.
func released(m catalog.Model) bool {
return !strings.Contains(m.ID, "-exp") && !strings.Contains(m.ID, "preview")
}
// catalogsOf are the models of these models.dev catalogs, the first's of
// an id kept. One catalog's is its shared list, with no copy: the caller
// only reads it.
func catalogsOf(ids []string) []catalog.Model {
if len(ids) == 1 {
return catalog.ProviderShared(ids[0])
}
var known []catalog.Model
seen := map[string]bool{}
for _, id := range ids {
for _, m := range catalog.ProviderShared(id) {
if !seen[m.ID] {
seen[m.ID] = true
known = append(known, m)
}
}
}
return known
}
func (p Provider) firstCatalog() string {
if cs := p.Catalogs(); len(cs) > 0 {
return cs[0]