diff --git a/internal/catalog/pricememo.go b/internal/catalog/pricememo.go index 56a820630..f7006f9f2 100644 --- a/internal/catalog/pricememo.go +++ b/internal/catalog/pricememo.go @@ -1,7 +1,6 @@ package catalog import ( - "strings" "sync" "sync/atomic" ) @@ -34,28 +33,24 @@ var pricedRuns atomic.Uint64 // 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) + // the key is made on the stack and looked up without a copy (Go reads + // m[string(b)] in place): made into a string for every price asked, + // it was ~0.6 MB of a provider list's ~5 MB of garbage an ask + var buf [160]byte + k := append(buf[:0], kind) for _, p := range providers { - n += len(p) + 1 + k = append(append(k, p...), 0) } - 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() + k = append(append(k, 1), id...) pricedMemo.Lock() if pricedMemo.gen == at && pricedMemo.by != nil { - if f, ok := pricedMemo.by[key]; ok { + if f, ok := pricedMemo.by[string(k)]; ok { pricedMemo.Unlock() return f.pr, f.ok } } pricedMemo.Unlock() + key := string(k) pricedRuns.Add(1) pr, ok := find() pricedMemo.Lock() diff --git a/internal/catalog/pricememo_test.go b/internal/catalog/pricememo_test.go index 63e8db2b1..78eace59b 100644 --- a/internal/catalog/pricememo_test.go +++ b/internal/catalog/pricememo_test.go @@ -41,3 +41,22 @@ func TestPricedOnceALoad(t *testing.T) { t.Fatalf("after a new catalog: %+v %v, %+v %v, want 0.2", a, aok, b, bok) } } + +// TestPricedOnceHitAllocatesNothing: the provider list asks the memo for +// every model of every provider on each poll, and a price it has is +// answered without a byte of garbage (its key was a new string each ask). +func TestPricedOnceHitAllocatesNothing(t *testing.T) { + pricedMemo.Lock() + at := pricedMemo.gen + pricedMemo.Unlock() + providers := []string{"anthropic", "deepseek"} + find := func() (Price, bool) { return Price{Input: 1}, true } + pricedOnce(at, 'p', providers, "deepseek-v4.1-flash", find) + runs := pricedRuns.Load() + if n := testing.AllocsPerRun(100, func() { pricedOnce(at, 'p', providers, "deepseek-v4.1-flash", find) }); n != 0 { + t.Errorf("a price the memo has made %v allocations, want 0", n) + } + if n := pricedRuns.Load() - runs; n != 0 { + t.Errorf("a price the memo has was looked for %d times again", n) + } +} diff --git a/internal/provider/balance_probe.go b/internal/provider/balance_probe.go index 15d91bff5..0ef89936a 100644 --- a/internal/provider/balance_probe.go +++ b/internal/provider/balance_probe.go @@ -135,11 +135,30 @@ func notProbed(host string) bool { } func hostnameOf(raw string) string { - u, err := url.Parse(raw) - if err != nil { - return "" + hostnames.Lock() + h, ok := hostnames.m[raw] + hostnames.Unlock() + if ok { + return h } - return u.Hostname() + if u, err := url.Parse(raw); err == nil { + h = u.Hostname() + } + hostnames.Lock() + if hostnames.m == nil || len(hostnames.m) >= 512 { + hostnames.m = map[string]string{} + } + hostnames.m[raw] = h + hostnames.Unlock() + return h +} + +// hostnames are the base URLs hostnameOf has parsed: the provider list +// asks every provider's on each poll, and parsing them again was ~0.4 MB +// of its garbage an ask. A URL's hostname is the same every time. +var hostnames struct { + sync.Mutex + m map[string]string } // probedBalanceURL is the query found for p, without asking: "" when none diff --git a/internal/provider/balance_probe_test.go b/internal/provider/balance_probe_test.go index 67228e0a2..4ecf4cfc6 100644 --- a/internal/provider/balance_probe_test.go +++ b/internal/provider/balance_probe_test.go @@ -251,3 +251,21 @@ func ageProbes(t *testing.T, d time.Duration) { probed.m[k] = f } } + +// TestHostnameOfKept: the provider list asks each provider's base URL's +// hostname on every poll; it is parsed once, then answered without +// garbage, and an unparsable one is "". +func TestHostnameOfKept(t *testing.T) { + const raw = "https://api.example.com:8443/v1" + if h := hostnameOf(raw); h != "api.example.com" { + t.Fatalf("hostnameOf(%q) = %q", raw, h) + } + if n := testing.AllocsPerRun(100, func() { hostnameOf(raw) }); n != 0 { + t.Errorf("a hostname parsed before made %v allocations, want 0", n) + } + for range 2 { + if h := hostnameOf("http://[::1"); h != "" { + t.Errorf("an unparsable URL's hostname = %q", h) + } + } +}