Files
magpie/internal/catalog/pricememo.go
T
yetone 1eebd1ff3c providers: a known maker price is answered without garbage too
b53f4e7c4 built the price memo's key in a 160-byte stack buffer. A
maker price names every maker catalog, which is longer, so on the
real account every maker-price hit still grew the key on the heap:
pricedOnce was 25.5MB of 186.9MB allocated over 40 asks of
/api/providers. The buffer is now 1 KB.

Measured on mote with the real account copy (aalloc.sh, 40 asks,
alloc_space delta): 186.9MB (t13, v0.1.3446) -> 169.8MB (t14), -9%;
213.4MB before v0.1.3446.

Run: go vet ./internal/catalog ./internal/provider; go test -count=1
-timeout 45m ./internal/catalog ./internal/provider (ok, ok) on mote.
New TestMakerPriceKnownAllocatesNothing asks MakerPriced with the real
maker catalog list; with the 160-byte buffer put back by hand it fails
(1 allocation per ask), and it also fails if that list's key outgrows
1 KB. Not tried: Windows and Linux (no OS-specific code).
2026-11-10 02:10:10 +08:00

70 lines
2.1 KiB
Go

package catalog
import (
"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) {
// 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
// (1 KB: a maker price names every maker catalog, past 160 bytes;
// TestMakerPriceKnownAllocatesNothing holds the bound)
var buf [1024]byte
k := append(buf[:0], kind)
for _, p := range providers {
k = append(append(k, p...), 0)
}
k = append(append(k, 1), id...)
pricedMemo.Lock()
if pricedMemo.gen == at && pricedMemo.by != nil {
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()
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
}