From 97ec2f8ae6a383c621659a79b5f8f3feba876902 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 02:00:07 +0800 Subject: [PATCH] catalog: a model's levels are looked up in an index made once per load listedBy (ListedBy, ListedByBorrowed) is asked for every model's levels on each look at the providers, through borrowedEfforts. Each ask ranged over every model of every maker's catalog twice: for its id as given, then, for a name models.dev knows, through SameModel, whose regexp ran on each of those thousands of keys every time. On a real config that was 0.28 GB of the 2.6 GB twenty polls allocated. Each provider's keys are now indexed by their bare id and by SameModel's spelling, in key order, once per catalog load (the Reset count loadGen gives), as PricedBy's priceIndex is. Of several keys with one bare id, the first in key order is now asked first, where before it was the map's order. TestListedByReadsAnIndex: a 2000-model provider beside zai; glm-5.3- flash is found under another spelling, a key under its bare id, an ask takes under 8KB, and a catalog loaded since is read at once. Red on the old code ("an ask took 341917 bytes with the catalog unchanged"). Run on mote at 98a8dee82 (go test -timeout 45m): internal/catalog ok 2.2s, internal/provider ok 214s, internal/agent ok 368s, internal/gateway ok 801s. go vet ./... darwin and windows ok; linux vet of catalog, provider and agent ok (./... as a whole needs cgo for wails). gofmt -l clean. --- internal/catalog/catalog.go | 57 ++++++++++++++++++++------- internal/catalog/listed_index_test.go | 54 +++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 internal/catalog/listed_index_test.go 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) + } +}