From 6a78060c6dbb23f0789c039c85f97d39f2b66708 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 6 Nov 2026 12:10:29 +0800 Subject: [PATCH] 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. --- internal/agent/alma.go | 5 ++++- internal/agent/alma_seen_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/agent/alma.go b/internal/agent/alma.go index b3ad57e83..df7cd6dc5 100644 --- a/internal/agent/alma.go +++ b/internal/agent/alma.go @@ -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 diff --git a/internal/agent/alma_seen_test.go b/internal/agent/alma_seen_test.go index e47036731..a351ff0f5 100644 --- a/internal/agent/alma_seen_test.go +++ b/internal/agent/alma_seen_test.go @@ -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()