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.
This commit is contained in:
yetone
2026-11-01 03:24:38 +08:00
parent ddc225c964
commit d714c4216d
2 changed files with 52 additions and 11 deletions
+13 -11
View File
@@ -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 {
+39
View File
@@ -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)
}
}