catalog: strings and lists the models repeat are held once (#2278)
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.
This commit is contained in:
@@ -660,6 +660,7 @@ func loadGen() (map[string]mdProvider, int) {
|
||||
}
|
||||
var m map[string]mdProvider
|
||||
if json.Unmarshal(b, &m) == nil && len(m) > 0 {
|
||||
shareCatalog(m)
|
||||
mdev = m
|
||||
votes, reasons := map[string]int{}, map[string]int{}
|
||||
named := map[string]map[string]int{}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// The loaded catalog holds what its models repeat once: two models with
|
||||
// the same modalities, family, release date, levels and SDK share them.
|
||||
func TestLoadedCatalogSharesRepeats(t *testing.T) {
|
||||
writeCatalog(t, `{
|
||||
"openai": {"models": {
|
||||
"a": {"id":"a","name":"A","family":"gpt","release_date":"2026-01-01",
|
||||
"modalities":{"input":["text","image"],"output":["text"]},
|
||||
"reasoning_options":[{"type":"effort","values":["low","high"]}],
|
||||
"provider":{"npm":"@ai-sdk/openai"}},
|
||||
"b": {"id":"b","name":"B","family":"gpt","release_date":"2026-01-01",
|
||||
"modalities":{"input":["text","image"],"output":["text"]},
|
||||
"reasoning_options":[{"type":"effort","values":["low","high"]}],
|
||||
"provider":{"npm":"@ai-sdk/openai"}}
|
||||
}},
|
||||
"other": {"models": {
|
||||
"c": {"id":"c","name":"C","family":"gpt","modalities":{"input":["text"],"output":["text"]}}
|
||||
}}
|
||||
}`)
|
||||
m, _ := loadGen()
|
||||
a, b, c := m["openai"].Models["a"], m["openai"].Models["b"], m["other"].Models["c"]
|
||||
same := func(x, y string) bool { return unsafe.StringData(x) == unsafe.StringData(y) }
|
||||
if !same(a.Family, b.Family) || !same(a.Family, c.Family) || !same(a.ReleaseDate, b.ReleaseDate) {
|
||||
t.Error("each model has its own copy of its family and release date")
|
||||
}
|
||||
if &a.Modalities.Input[0] != &b.Modalities.Input[0] || &a.Modalities.Output[0] != &b.Modalities.Output[0] {
|
||||
t.Error("each model has its own modalities list")
|
||||
}
|
||||
if !same(a.Modalities.Output[0], c.Modalities.Input[0]) {
|
||||
t.Error("each list has its own copy of a modality")
|
||||
}
|
||||
if &a.Reasoning[0].Values[0] != &b.Reasoning[0].Values[0] || !same(a.Reasoning[0].Type, b.Reasoning[0].Type) {
|
||||
t.Error("each model has its own levels")
|
||||
}
|
||||
if a.Provider != b.Provider {
|
||||
t.Error("each model has its own SDK")
|
||||
}
|
||||
// a shared list is clipped: an append copies it
|
||||
in := a.Modalities.Input
|
||||
if cap(in) != len(in) {
|
||||
t.Fatalf("shared list has room past its end: len %d cap %d", len(in), cap(in))
|
||||
}
|
||||
_ = append(in, "pdf")
|
||||
if len(b.Modalities.Input) != 2 {
|
||||
t.Error("an append to one model's list changed another's")
|
||||
}
|
||||
// what is read through it is as before
|
||||
if a.Name != "A" || b.Name != "B" || c.Modalities.Input[0] != "text" || a.Reasoning[0].Values[1] != "high" || a.Provider.NPM != "@ai-sdk/openai" {
|
||||
t.Errorf("values changed: %+v %+v %+v", a, b, c)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user