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) + } +}