From d714c4216d64ff703b3579764889fc191c8e0e8c Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 03:24:19 +0800 Subject: [PATCH] catalog: a newer model's line is looked up in the index made once per load KinEfforts gives a model newer than the catalog its line's levels (gpt- 6.1-sol's from gpt-6-sol). borrowedEfforts asks it for every model with no levels of its own, on each look at the providers. Each ask ranged over every model of every maker's catalog and split and joined each key to compare its versionless shape: 66 MB of the 1.4 GB a main magpie allocated over 20 window polls with the owner's real config. levelIndex (97ec2f8ae) now also holds each provider's thinking keys that aren't budgeted, by their versionless shape (kin), in key order, made once per catalog load. KinEfforts reads the keys of the id's shape. Which one wins doesn't change: the newest, then within one provider the greater key, then the first provider. TestKinEffortsReadsAnIndex: 2000 thinking models of other shapes beside zai's glm-5.3-flash; glm-6.1-flash takes its levels, an ask takes under 8KB, and a catalog loaded since is read at once. Broken by hand on mote (the old catalog.go): red, "an ask took 128458 bytes with the catalog unchanged". Run on mote at 80bcef18f (go test -timeout 45m): internal/catalog ok 0.8s, internal/provider ok 238s. go vet of both on darwin, windows and linux ok; gofmt -l clean. --- internal/catalog/catalog.go | 24 +++++++++-------- internal/catalog/listed_index_test.go | 39 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index d393b0746..a5ecb7caa 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -1729,9 +1729,11 @@ func listedBy(providers []string, id string, providerOverrides bool) ([]string, // 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. +// config allocated over 20 polls. KinEfforts looks its thinking models up +// by their versionless shape (kin), where it split and joined every key +// on each ask: 66 MB of 1.4 GB. type levelIndex struct { - bare, same map[string][]string + bare, same, kin map[string][]string } var levelIndexes struct { @@ -1751,12 +1753,17 @@ func levelIndexOf(all map[string]mdProvider, at int, pid string) *levelIndex { if x, ok := levelIndexes.by[pid]; ok { return x } - x := &levelIndex{bare: map[string][]string{}, same: map[string][]string{}} + x := &levelIndex{bare: map[string][]string{}, same: map[string][]string{}, kin: 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) + if m := all[pid].Models[key]; (m.Thinks || len(m.efforts("")) > 0) && !m.budgeted() { + if s, ok := versionless(b); ok { + x.kin[s] = append(x.kin[s], key) + } + } } levelIndexes.by[pid] = x return x @@ -1786,7 +1793,7 @@ func Unsuffixed(id string) (string, bool) { // from mimo-v2.6-flash): a model newer than the catalog on this machine // offers its line's levels, not none. False when none of them has one. func KinEfforts(providers []string, id string) ([]string, bool) { - all := load() + all, at := loadGen() shape, ok := versionless(bareID(id)) if !ok { return nil, false @@ -1797,13 +1804,8 @@ func KinEfforts(providers []string, id string) ([]string, bool) { found bool ) for _, pid := range providers { - for key, m := range all[pid].Models { - if !m.Thinks && len(m.efforts("")) == 0 || m.budgeted() { - continue - } - if s, ok := versionless(bareID(key)); !ok || s != shape { - continue - } + for _, key := range levelIndexOf(all, at, pid).kin[shape] { + m := all[pid].Models[key] // the newest; of two the same day, the first provider's, then // the greater id, whatever order the map gives if !found || m.ReleaseDate > newest || m.ReleaseDate == newest && pid == from && key > name { diff --git a/internal/catalog/listed_index_test.go b/internal/catalog/listed_index_test.go index eb6e55440..8c4b93a5a 100644 --- a/internal/catalog/listed_index_test.go +++ b/internal/catalog/listed_index_test.go @@ -52,3 +52,42 @@ func TestListedByReadsAnIndex(t *testing.T) { t.Fatalf("a catalog loaded since is not read: %q", got) } } + +// A model newer than the catalog takes its line's levels from the same +// index: KinEfforts split and joined every key of every maker on each ask. +func TestKinEffortsReadsAnIndex(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-v1.5": {"id":"model-%d-v1.5","reasoning":true},`, 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, _ := KinEfforts([]string{"big", "zai"}, "glm-6.1-flash") + return strings.Join(got, ",") + } + if got := ask(); got != "low,high,max" { + t.Fatalf("glm-6.1-flash's line: %q", got) + } + var a, b runtime.MemStats + runtime.ReadMemStats(&a) + for range 20 { + ask() + } + runtime.ReadMemStats(&b) + // a versionless 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) + } +}