The models.dev catalog is kept while anything reads it, and an open page keeps reading it. Its JSON gives each of ~9k models its own copy of "text", "image", its family, its release date, its reasoning levels and its SDK's name. shareCatalog, run once after a load, has each repeat held once; a shared list is clipped, so an append to one model's copies it. Measured on mote with the real-account sandbox (acctp), page open, pageheap.sh, alternating w54v (base) and w55v (this): - pprof inuse, focus catalog.loadGen: 5.51 / 7.06 MB -> 4.01 / 3.52 MB - liveHeap: 77.7 / 78.5 MB -> 75.9 / 76.8 MB - Footprint: 128 / 131 MB -> 121 / 129 MB - CPU at mark: 2.44 / 2.57 s -> 2.53 / 2.47 s (unchanged, noise) Run: go vet ./internal/catalog; go test ./internal/catalog ./internal/provider/... ok (mote). GOOS=windows and GOOS=linux go vet ./internal/catalog ok (mica). Broken by hand: without the shareCatalog call TestLoadedCatalogSharesRepeats fails on all five checks (mica). Not run: the full suite.
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package catalog
|
|
|
|
import (
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// shareCatalog has what the loaded catalog's models repeat held once: the
|
|
// JSON gives each of ~9k models its own copy of "text", "image", its
|
|
// family, its release date, its levels and its SDK's name, and the catalog
|
|
// is kept while anything reads it (#2278). A list is shared clipped, so an
|
|
// append to one copies it rather than writing into another model's.
|
|
func shareCatalog(m map[string]mdProvider) {
|
|
strs := map[string]string{}
|
|
str := func(s string) string {
|
|
if s == "" {
|
|
return ""
|
|
}
|
|
if k, ok := strs[s]; ok {
|
|
return k
|
|
}
|
|
strs[s] = s
|
|
return s
|
|
}
|
|
lists := map[string][]string{}
|
|
list := func(l []string) []string {
|
|
if l == nil {
|
|
return nil
|
|
}
|
|
k := strconv.Itoa(len(l)) + "\x00" + strings.Join(l, "\x00")
|
|
if v, ok := lists[k]; ok {
|
|
return v
|
|
}
|
|
for i := range l {
|
|
l[i] = str(l[i])
|
|
}
|
|
l = slices.Clip(l)
|
|
lists[k] = l
|
|
return l
|
|
}
|
|
sdks := map[string]any{}
|
|
for pid, p := range m {
|
|
p.Env = list(p.Env)
|
|
for id, x := range p.Models {
|
|
if x.ID == id {
|
|
x.ID = id // the map's key already holds it
|
|
}
|
|
x.Name, x.ReleaseDate, x.Family, x.Canonical = str(x.Name), str(x.ReleaseDate), str(x.Family), str(x.Canonical)
|
|
x.Modalities.Input, x.Modalities.Output = list(x.Modalities.Input), list(x.Modalities.Output)
|
|
for i := range x.Reasoning {
|
|
r := &x.Reasoning[i]
|
|
r.Type, r.Values = str(r.Type), list(r.Values)
|
|
}
|
|
if x.Provider != nil {
|
|
x.Provider = sharedOf(sdks, x.Provider.NPM, x.Provider)
|
|
}
|
|
p.Models[id] = x
|
|
}
|
|
m[pid] = p
|
|
}
|
|
}
|
|
|
|
// sharedOf is the value first kept under k, keeping v when there is none.
|
|
func sharedOf[T any](kept map[string]any, k string, v T) T {
|
|
if was, ok := kept[k]; ok {
|
|
return was.(T)
|
|
}
|
|
kept[k] = v
|
|
return v
|
|
}
|