catalog: a model's list price is looked for once a catalog load
The GUI's provider list prices every model of every provider on each
request of /api/providers: ListPrice asks catalog.PricedBy and
MakerPriced by every spelling of the id across every maker's catalog,
and a model none of them lists (most of a relay's) went through all of
them each time. In a CPU profile of the GUI's polls (drift, state,
providers, 40 rounds, a real-account sandbox) ListPrice was 14% of the
process's CPU, PricedBy and makerListPrice most of it.
What PricedBy and MakerPriced find depends on the loaded catalog, the
providers asked and the id, and nothing else, so pricedOnce keeps it by
those for the load's Reset count (gen), as priceIndexOf and AtEndpoint
keep what they build. A Reset (a new catalog synced) starts it again; it
holds at most 32,768 answers and starts again past that. find runs
without the memo's lock, as PricedBy calls itself for a renamed id.
Run on mote (macOS 27, Go 1.26.8) at this tree:
- gofmt clean; go vet ./internal/catalog on darwin, GOOS=windows and
GOOS=linux ok.
- TestPricedOnceALoad 3 of 3 alone, and with -race beside
TestPricedBy*/TestMakerPriced*: ok.
- Broken by hand: the memo never answering, TestPricedOnceALoad red
("looked for 20 times again at one load"); the memo not keyed by gen,
red (the old catalog's 0.15 after a new one's 0.2).
- go test ./internal/catalog ./internal/provider ./internal/usage ok.
./internal/gui: TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestMainWindowCorners/macOS_26_SDK (#3949) red, the same two red with
the same output at origin/main ccb75030a on mote.
Real use, this machine, a copy of the real accounts in a sandbox HOME
(proxy blocked, MAGPIE_NO_STATS=1), v0.1.2518's tree (old) against this:
- /api/providers: 1506 models, 938 with a list price; every model's
list and price the same in both. The rest of the reply differs only
in fetched times and one login's lapsed time.
- 40 rounds of drift + state + providers, twice each, alternated:
providers 28.4/27.6ms old, 24.4/24.6ms new; process CPU 2.09/2.03s
old, 1.95/1.94s new.
Not run: the whole suite; Windows and Linux runs (nothing here is by OS).
This commit is contained in:
@@ -882,6 +882,10 @@ func PriceOf(providerID, modelID string) (Price, bool) {
|
||||
// (DeepSeek's deepseek-flash is deepseek-v4.1-flash).
|
||||
func PricedBy(providers []string, id string) (Price, bool) {
|
||||
all, at := loadGen()
|
||||
return pricedOnce(at, 'p', providers, id, func() (Price, bool) { return pricedBy(all, at, providers, id) })
|
||||
}
|
||||
|
||||
func pricedBy(all map[string]mdProvider, at int, providers []string, id string) (Price, bool) {
|
||||
for _, pid := range providers {
|
||||
if m, ok := all[pid].Models[id]; ok && m.Cost != nil {
|
||||
return *m.Cost, true
|
||||
@@ -1100,6 +1104,10 @@ func madeByOf(all map[string]mdProvider, at int, b string) *madeBy {
|
||||
// those prices it.
|
||||
func MakerPriced(prefer []string, id string) (Price, bool) {
|
||||
all, at := loadGen()
|
||||
return pricedOnce(at, 'm', prefer, id, func() (Price, bool) { return makerPriced(all, at, prefer, id) })
|
||||
}
|
||||
|
||||
func makerPriced(all map[string]mdProvider, at int, prefer []string, id string) (Price, bool) {
|
||||
b := bareID(id)
|
||||
if r, ok := unprofiled(b); ok {
|
||||
b = r
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// pricedMemo keeps what PricedBy and MakerPriced found, for the load of
|
||||
// Reset count gen. The GUI's provider list prices every model of every
|
||||
// provider on each poll, and a model looked for by every spelling in
|
||||
// every maker's catalog cost the poll more than anything else it did
|
||||
// (most of a provider list's 50ms). What they find depends on the loaded
|
||||
// catalog, the providers asked and the id, and nothing else, so it is
|
||||
// kept until the next Reset.
|
||||
var pricedMemo struct {
|
||||
sync.Mutex
|
||||
gen int
|
||||
by map[string]pricedFound
|
||||
}
|
||||
|
||||
type pricedFound struct {
|
||||
pr Price
|
||||
ok bool
|
||||
}
|
||||
|
||||
// pricedMemoEntries bounds the memo: past it, it starts again.
|
||||
const pricedMemoEntries = 1 << 15
|
||||
|
||||
// pricedRuns counts the look-ups pricedOnce did not have (a test reads it).
|
||||
var pricedRuns atomic.Uint64
|
||||
|
||||
// pricedOnce is find's answer for the providers and id under kind, from
|
||||
// the memo when it has it. find runs without the memo's lock held:
|
||||
// PricedBy calls itself for a renamed id.
|
||||
func pricedOnce(at int, kind byte, providers []string, id string, find func() (Price, bool)) (Price, bool) {
|
||||
var k strings.Builder
|
||||
k.WriteByte(kind)
|
||||
for _, p := range providers {
|
||||
k.WriteString(p)
|
||||
k.WriteByte(0)
|
||||
}
|
||||
k.WriteByte(1)
|
||||
k.WriteString(id)
|
||||
key := k.String()
|
||||
pricedMemo.Lock()
|
||||
if pricedMemo.gen == at && pricedMemo.by != nil {
|
||||
if f, ok := pricedMemo.by[key]; ok {
|
||||
pricedMemo.Unlock()
|
||||
return f.pr, f.ok
|
||||
}
|
||||
}
|
||||
pricedMemo.Unlock()
|
||||
pricedRuns.Add(1)
|
||||
pr, ok := find()
|
||||
pricedMemo.Lock()
|
||||
if pricedMemo.gen != at || pricedMemo.by == nil || len(pricedMemo.by) >= pricedMemoEntries {
|
||||
if pricedMemo.gen <= at {
|
||||
pricedMemo.gen, pricedMemo.by = at, map[string]pricedFound{}
|
||||
}
|
||||
}
|
||||
if pricedMemo.gen == at {
|
||||
pricedMemo.by[key] = pricedFound{pr, ok}
|
||||
}
|
||||
pricedMemo.Unlock()
|
||||
return pr, ok
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package catalog
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestPricedOnceALoad: a model priced again at the same load is not
|
||||
// looked for again (the GUI's provider list prices every model on each
|
||||
// poll), and a Reset to a catalog with another price gives that price.
|
||||
func TestPricedOnceALoad(t *testing.T) {
|
||||
writeCatalog(t, canonicalCatalog)
|
||||
ask := func() (Price, bool, Price, bool) {
|
||||
a, aok := PricedBy([]string{"anthropic", "deepseek"}, "deepseek-v4.1-flash")
|
||||
b, bok := MakerPriced([]string{"deepseek"}, "deepseek-v4.1-flash")
|
||||
return a, aok, b, bok
|
||||
}
|
||||
a, aok, b, bok := ask()
|
||||
if !aok || a.Input != 0.15 || !bok || b.Input != 0.15 {
|
||||
t.Fatalf("first look-up: %+v %v, %+v %v", a, aok, b, bok)
|
||||
}
|
||||
if _, ok := PricedBy([]string{"deepseek"}, "no-such-model"); ok {
|
||||
t.Fatal("an unlisted model priced")
|
||||
}
|
||||
runs := pricedRuns.Load()
|
||||
for range 5 {
|
||||
a2, aok2, b2, bok2 := ask()
|
||||
if a2.Input != a.Input || aok2 != aok || b2.Input != b.Input || bok2 != bok {
|
||||
t.Fatalf("the same look-up gave %+v %v, %+v %v", a2, aok2, b2, bok2)
|
||||
}
|
||||
if _, ok := PricedBy([]string{"deepseek"}, "no-such-model"); ok {
|
||||
t.Fatal("an unlisted model priced the second time")
|
||||
}
|
||||
}
|
||||
if n := pricedRuns.Load() - runs; n != 0 {
|
||||
t.Fatalf("the same models were looked for %d times again at one load", n)
|
||||
}
|
||||
// the same id from other providers is its own look-up
|
||||
if p, ok := PricedBy([]string{"alibaba-cn"}, "deepseek-v4.1-flash"); !ok || p.Input != 0.29754 {
|
||||
t.Fatalf("alibaba-cn's own price: %+v %v", p, ok)
|
||||
}
|
||||
writeCatalog(t, `{"deepseek":{"id":"deepseek","models":{"deepseek-flash":{"id":"deepseek-flash","cost":{"input":0.2,"output":0.8},"canonical_model_id":"deepseek/deepseek-v4.1-flash"}}}}`)
|
||||
if a, aok, b, bok := ask(); !aok || a.Input != 0.2 || !bok || b.Input != 0.2 {
|
||||
t.Fatalf("after a new catalog: %+v %v, %+v %v, want 0.2", a, aok, b, bok)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user