agent: Alma's providers are not decoded again from the same answer

almaProvidersSeen decoded Alma's providers list each time it was asked
past almaReadFor, on each poll of the Agents page. On the live magpie
(v0.1.2906) Alma's answer is 227 KB, and its json.Unmarshal was ~13 MB of
what an idle two minutes allocated (heap profile diff from
/v1/magpie/heap). The same bytes are now answered with the providers they
gave last time, by pointer or by bytes.Equal; changed bytes are decoded
as before.

Run (mote, at this tree):
- go vet ./internal/agent/, gofmt -l: clean
- go test -count=1 -timeout 45m ./internal/agent/: ok (216.6s)
- TestAlmaProvidersSeenDecodeOnce's new part (almaReadFor 0, AllocsPerRun of a
  look) is green at most 400 allocs; with the bytes.Equal check removed
  by hand it measures 7232 and goes red.

Not tried: a live Alma app against this build; the bytes compared are
what almaGet already returned.
This commit is contained in:
yetone
2026-11-06 12:10:43 +08:00
parent c42522e241
commit 6a78060c6d
2 changed files with 21 additions and 1 deletions
+4 -1
View File
@@ -227,7 +227,10 @@ func almaProvidersSeen() ([]almaProvider, error) {
}
almaSeen.Lock()
defer almaSeen.Unlock()
if len(b) > 0 && len(b) == len(almaSeen.body) && &b[0] == &almaSeen.body[0] {
// the same bytes asked again (past almaReadFor, each poll of the
// Agents page) are the same providers: 4 MB a minute decoded again on
// a live magpie with Alma's 227 KB answer
if len(b) > 0 && len(b) == len(almaSeen.body) && (&b[0] == &almaSeen.body[0] || bytes.Equal(b, almaSeen.body)) {
return almaSeen.ps, nil
}
var ps []almaProvider
+17
View File
@@ -56,6 +56,23 @@ func TestAlmaProvidersSeenDecodeOnce(t *testing.T) {
if g := asked(); g != 1 {
t.Fatalf("looks asked Alma %d times", g)
}
// the same answer asked for again, past the time one is kept, is not
// decoded again either
almaReadFor = 0
if n := testing.AllocsPerRun(5, func() {
if ps, err := almaProvidersSeen(); err != nil || len(ps) != 1 {
t.Fatal(err, ps)
}
}); n > 400 {
t.Errorf("a look at the same answer asked again allocates %v times", n)
}
if g := asked(); g < 6 {
t.Fatalf("Alma asked %d times with nothing kept", g)
}
almaReadFor = time.Minute
mu.Lock()
gets = 1
mu.Unlock()
mu.Lock()
model = "gpt-5.5"
mu.Unlock()