providers: a known price and a known hostname are answered without garbage
The Providers list asks the price memo for every model of every provider on each poll, and the hit built its key as a new string each time. The key is now built in a stack buffer and looked up through string(k), which Go doesn't allocate for a map index; it is copied out only when the price is new. hostnameOf parsed every provider's base URL with url.Parse on every poll; the hostname is now kept per URL (reset at 512). Measured on mote with the real account copy (aalloc.sh, 40 asks of /api/providers, alloc_space delta): 213.4MB at origin/main (t12), 186.9MB with this change (t13), -12%. CPU for 30 asks 0.64s -> 0.62s. Run: go vet ./internal/catalog ./internal/provider; go test -count=1 -timeout 45m ./internal/catalog ./internal/provider (ok, ok) on mote. New TestPricedOnceHitAllocatesNothing and TestHostnameOfKept fail on the old code (1 allocation each, broken by hand) and pass with it. Not tried: Windows and Linux (no OS-specific code).
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user