diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index 2f5c38044..d393b0746 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -9,6 +9,7 @@ import ( "encoding/json" "fmt" "io" + "maps" "net/http" "os" "path/filepath" @@ -1678,7 +1679,7 @@ func ListedByBorrowed(providers []string, id string) ([]string, bool) { } func listedBy(providers []string, id string, providerOverrides bool) ([]string, bool) { - all := load() + all, at := loadGen() b := bareID(id) if r, _, ok := renamed(b); ok { b = r @@ -1698,11 +1699,8 @@ func listedBy(providers []string, id string, providerOverrides bool) ([]string, } for _, want := range []string{b, cutAt(b, '('), cutAt(b, ':')} { for _, pid := range providers { - for key, m := range all[pid].Models { - if bareID(key) != want { - continue - } - if e, ok := pick(pid, m); ok { + for _, key := range levelIndexOf(all, at, pid).bare[want] { + if e, ok := pick(pid, all[pid].Models[key]); ok { return e, true } } @@ -1717,14 +1715,7 @@ func listedBy(providers []string, id string, providerOverrides bool) ([]string, return nil, false } for _, pid := range providers { - var keys []string - for key := range all[pid].Models { - if SameModel(bareID(key)) == n { - keys = append(keys, key) - } - } - slices.Sort(keys) - for _, key := range keys { + for _, key := range levelIndexOf(all, at, pid).same[n] { if e, ok := pick(pid, all[pid].Models[key]); ok { return e, true } @@ -1733,6 +1724,44 @@ func listedBy(providers []string, id string, providerOverrides bool) ([]string, return nil, false } +// levelIndex is a provider's model keys by what listedBy looks them up +// by, each in key order: their id without a path or case (bare), and +// SameModel's spelling of it (same). listedBy, asked for every model's +// levels on each look at the providers, read every vendor's every model +// through SameModel's regexp each time: 0.28 GB of the 2.6 GB a real +// config allocated over 20 polls. +type levelIndex struct { + bare, same map[string][]string +} + +var levelIndexes struct { + sync.Mutex + gen int + by map[string]*levelIndex +} + +// levelIndexOf is pid's levelIndex in all, the catalog loaded at Reset +// count at. +func levelIndexOf(all map[string]mdProvider, at int, pid string) *levelIndex { + levelIndexes.Lock() + defer levelIndexes.Unlock() + if levelIndexes.by == nil || levelIndexes.gen != at { + levelIndexes.gen, levelIndexes.by = at, map[string]*levelIndex{} + } + if x, ok := levelIndexes.by[pid]; ok { + return x + } + x := &levelIndex{bare: map[string][]string{}, same: map[string][]string{}} + for _, key := range slices.Sorted(maps.Keys(all[pid].Models)) { + b := bareID(key) + x.bare[b] = append(x.bare[b], key) + n := SameModel(b) + x.same[n] = append(x.same[n], key) + } + levelIndexes.by[pid] = x + return x +} + // variantSuffix is a word a proxy puts after a model's id to name the // level or speed it serves the model at: CLIProxyAPI's Antigravity // channel lists gemini-3.8-flash-high, claude-opus-5-5-high, diff --git a/internal/catalog/listed_index_test.go b/internal/catalog/listed_index_test.go new file mode 100644 index 000000000..eb6e55440 --- /dev/null +++ b/internal/catalog/listed_index_test.go @@ -0,0 +1,54 @@ +package catalog + +import ( + "fmt" + "runtime" + "strings" + "testing" +) + +// A model's levels are looked up through an index of each provider's +// keys, made once per catalog load: listedBy read every model of every +// provider asked through SameModel's regexp on each ask, and every look +// at the providers asks it for each model. A catalog loaded since is +// read at once. +func TestListedByReadsAnIndex(t *testing.T) { + big := func(levels string) string { + var b strings.Builder + b.WriteString(`{"big": {"models": {`) + for i := range 2000 { + fmt.Fprintf(&b, `"vendor/Model_%d.5": {"id":"model-%d.5"},`, i, i) + } + b.WriteString(`"x": {"id":"x"}}}, + "zai": {"models": {"glm-5.3-flash": {"id":"glm-5.3-flash","reasoning_options":[{"type":"effort","values":[` + levels + `]}]}}}}`) + return b.String() + } + writeCatalog(t, big(`"low","high","max"`)) + ask := func() string { + got, _ := ListedByBorrowed([]string{"big", "zai"}, "GLM 5-3 flash") + return strings.Join(got, ",") + } + // under another spelling (SameModel), after every provider's keys as given + if got := ask(); got != "low,high,max" { + t.Fatalf("glm-5.3-flash under another spelling: %q", got) + } + if got, ok := ListedByBorrowed([]string{"big"}, "big/model_7.5"); !ok || len(got) != 0 { + t.Fatalf("a key under its bare id: %v %v", got, ok) + } + var a, b runtime.MemStats + runtime.ReadMemStats(&a) + for range 20 { + ask() + } + runtime.ReadMemStats(&b) + // a SameModel of each of 2000 keys is ~200KB; a look in the index a + // few strings + if per := (b.TotalAlloc - a.TotalAlloc) / 20; per > 8<<10 { + t.Fatalf("an ask took %d bytes with the catalog unchanged", per) + } + + writeCatalog(t, big(`"low","high"`)) + if got := ask(); got != "low,high" { + t.Fatalf("a catalog loaded since is not read: %q", got) + } +}