Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

catalog: a provider's model list is built once a load, without copying every model to the heap for each

yetonef73f39050ddb
diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go
index 34b74a024..d6a3e4675 100644
--- a/internal/catalog/catalog.go
+++ b/internal/catalog/catalog.go
@@ -371,22 +371,27 @@ func (p mdProvider) levels(m mdModel, providerID string) []string {
if e := m.efforts(providerID); len(e) > 0 || !m.Thinks || m.Family == "" || m.budgeted() {
return e
}
- var kin *mdModel
- for _, x := range p.Models {
- if x.Family != m.Family || !x.Thinks || x.ID == m.ID || len(x.efforts(providerID)) == 0 {
+ // the kin is kept by its id, date and levels, not as &x: a pointer to
+ // the loop's copy put every model of p on the heap, for every model
+ // asked (~6 GB in 48 minutes of a user's magpie, #1956)
+ var kinID, kinDate string
+ var kin []string
+ for id := range p.Models {
+ x := p.Models[id]
+ if x.Family != m.Family || !x.Thinks || x.ID == m.ID {
continue
}
if m.ReleaseDate != "" && x.ReleaseDate > m.ReleaseDate {
continue
}
- if kin == nil || x.ReleaseDate > kin.ReleaseDate || x.ReleaseDate == kin.ReleaseDate && x.ID > kin.ID {
- kin = &x
+ if kin != nil && (x.ReleaseDate < kinDate || x.ReleaseDate == kinDate && x.ID <= kinID) {
+ continue
+ }
+ if e := x.efforts(providerID); len(e) > 0 {
+ kinID, kinDate, kin = x.ID, x.ReleaseDate, e
}
}
- if kin == nil {
- return nil
- }
- return kin.efforts(providerID)
+ return kin
}
// window is the tokens a prompt to m may hold: the input limit where
@@ -432,7 +437,12 @@ var (
// while a call is pricing its model
loadMu sync.Mutex
loaded bool
- mdev map[string]mdProvider
+ // built are Provider's lists of this load, by provider: dsh's wiring
+ // asks for them every 30 seconds, and each built every model again.
+ // gen counts Resets, so a list built from an older load isn't kept.
+ built map[string][]Model
+ gen int
+ mdev map[string]mdProvider
// images are the models, by bare id, most of the providers serving
// them say take images (a few mislabel a text model)
images map[string]bool
@@ -622,6 +632,8 @@ func load() map[string]mdProvider {
func Reset() {
loadMu.Lock()
loaded = false
+ built = nil
+ gen++
mdev, images, windows, outputs, efforts, thinks, names, listed, canonical = nil, nil, nil, nil, nil, nil, nil, nil, nil
loadMu.Unlock()
if Forget != nil {
@@ -838,7 +850,27 @@ 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 {
- p, ok := load()[id]
+ loadMu.Lock()
+ out, ok := built[id]
+ at := gen
+ loadMu.Unlock()
+ if ok {
+ return slices.Clone(out)
+ }
+ out = provider(load(), id)
+ loadMu.Lock()
+ if at == gen {
+ if built == nil {
+ built = map[string][]Model{}
+ }
+ built[id] = out
+ }
+ loadMu.Unlock()
+ return slices.Clone(out)
+}
+
+func provider(all map[string]mdProvider, id string) []Model {
+ p, ok := all[id]
if !ok {
return nil
}
diff --git a/internal/catalog/catalog_test.go b/internal/catalog/catalog_test.go
index ff3e5a920..09f965c19 100644
--- a/internal/catalog/catalog_test.go
+++ b/internal/catalog/catalog_test.go
@@ -2,6 +2,7 @@ package catalog
import (
"context"
+ "fmt"
"net/http"
"net/http/httptest"
"os"
@@ -439,3 +440,41 @@ func TestFetchedModalitiesKeepEveryKind(t *testing.T) {
t.Errorf("unknown: in %v out %v", got[1].InputModalities, got[1].OutputModalities)
}
}
+
+// Finding a model's kin doesn't copy each model of its provider to the
+// heap: Provider ran once a round of dsh's wiring, and a pointer to the
+// loop's copy made its allocations grow with the square of a provider's
+// models (~6 GB in 48 minutes of a user's magpie). 200 models of one
+// family ask a few allocations each, not 200. And a provider's list is
+// built once a load: asked again, it is a copy of the one built.
+func TestProviderAllocsGrowWithModels(t *testing.T) {
+ var b strings.Builder
+ b.WriteString(`{"openai": {"models": {`)
+ const n = 200
+ for i := range n {
+ if i > 0 {
+ b.WriteString(",")
+ }
+ fmt.Fprintf(&b, `"m-%03d": {"id":"m-%03d","family":"m","reasoning":true,"release_date":"2026-01-%02d"}`, i, i, i%28+1)
+ }
+ b.WriteString(`}}}`)
+ writeCatalog(t, b.String())
+ if len(Provider("openai")) != n {
+ t.Fatalf("Provider(openai) has %d models, want %d", len(Provider("openai")), n)
+ }
+ if a := testing.AllocsPerRun(5, func() { provider(load(), "openai") }); a > 10*n {
+ t.Errorf("building openai's %d models: %.0f allocations, want at most %d", n, a, 10*n)
+ }
+ if a := testing.AllocsPerRun(5, func() { Provider("openai") }); a > 2 {
+ t.Errorf("Provider(openai) asked again: %.0f allocations, want a copy (1)", a)
+ }
+ ms := Provider("openai")
+ ms[0].ID = "changed"
+ if Provider("openai")[0].ID == "changed" {
+ t.Error("a caller's change to Provider's list reached the next caller")
+ }
+ writeCatalog(t, `{"openai": {"models": {"m-new": {"id":"m-new"}}}}`)
+ if ms := Provider("openai"); len(ms) != 1 || ms[0].ID != "m-new" {
+ t.Errorf("after Reset, Provider(openai) = %v, want the new load's m-new", ms)
+ }
+}