/api/providers prices every model of every provider on each poll, and pricedNames ran grokWindow and grokEffort over each id, lowercasing it twice. Both patterns need "grok-" in the lowercased id, so any other id now returns before them; what a Grok id is priced as is unchanged. pricedOnce's memo key is sized once instead of growing by doublings. Run (on mote unless named): - go test -count=1 -timeout 45m ./internal/provider ok (103.8s), ./internal/catalog ok; GOOS=linux and windows go vet of both ok; gofmt clean (here). - Broken by hand: the guard narrowed to "grokx-" turns TestGrokEffortListPrice, TestCursorGrokFastPrice and TestGrokWindowWithoutKListPrice red, so the Grok aliases are covered. - Here, 200 GET /api/providers on the sandbox with the real config copy (offline, proxy :9), against v0.1.2548's tree: pricedNames 0.28s -> 0.02s, ListPrice 0.63s -> 0.40s; process CPU 4.62s -> 4.24s, median 20.4ms -> 19.0ms. pricedOnce stayed ~0.20s: its cost is the key's bytes, not the growth. Not run: the full ./... suite; GUI tests (the JSON is the same).
73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
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
|
|
n := 2 + len(id)
|
|
for _, p := range providers {
|
|
n += len(p) + 1
|
|
}
|
|
k.Grow(n) // one allocation, not one a doubling: it is made for every price asked
|
|
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
|
|
}
|