providers: a model's list price skips Grok's two name patterns unless it names grok-

/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).
This commit is contained in:
yetone
2026-11-01 13:52:21 +08:00
parent 1576ccdbbc
commit 21882a68dc
2 changed files with 13 additions and 2 deletions
+5
View File
@@ -35,6 +35,11 @@ var pricedRuns atomic.Uint64
// 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)
+8 -2
View File
@@ -2608,10 +2608,16 @@ func pricedNames(model string) []string {
if model == "codex-auto-review" {
out = append(out, "gpt-5.6-luna")
}
if m := grokWindow.FindStringSubmatch(strings.ToLower(strings.TrimSpace(model))); m != nil {
// both patterns name grok-: every other model of every provider's list
// went through two regexps on each Providers poll
l := strings.ToLower(strings.TrimSpace(model))
if !strings.Contains(l, "grok-") {
return out
}
if m := grokWindow.FindStringSubmatch(l); m != nil {
out = append(out, m[1])
}
if m := grokEffort.FindStringSubmatch(strings.ToLower(strings.TrimSpace(model))); m != nil {
if m := grokEffort.FindStringSubmatch(l); m != nil {
out = append(out, m[1])
}
return out