Files
magpie/internal/catalog
yetone f73f39050d catalog: a provider's model list is built once a load, without copying every model to the heap for each
Why magpie's own process sat at ~650 MB: a live heap profile of a
user's magpie (magpie heap, 48 minutes) held ~157 MB live but had
allocated 34.8 GB, ~725 MB a minute, so Go's heap peaked near 885 MB
and the footprint followed it. KeepDshWired's round every 30 seconds
was 11.8 GB of it: magpieModels("dsh") → Provider.Catalog → ... →
catalog.Provider for each catalog, which built every model again, and
mdProvider.levels alone was 5.97 GB flat. Its `kin = &x` took the
address of the range variable, so every model of the provider was
copied to the heap for every model asked: allocations grew with the
square of a provider's models.

- levels keeps the kin by id, date and levels, the same choice as
  before (newest by release date, then id, with levels, not after the
  model), with no pointer to the loop's copy.
- Provider keeps each provider's list for the load it was built from
  and gives callers a copy of it; Reset drops them, and a list built
  from a load that a Reset ended meanwhile isn't kept (gen).

Run:
- real models.json (5.8 MB), Provider for every provider id, a pass:
  63.5 MB / 272,952 allocs before; 8.96 MB / 10,478 with the levels fix
  alone; 3.2 MB / 232 with both (a throwaway benchmark, not committed).
- TestProviderAllocsGrowWithModels: red with `kin = &x` put back
  (40012 allocations for 200 models, at most 2000 wanted); red with the
  memo not stored; red with Provider handing out its own slice
  (a caller's change reached the next one). Green on this code.
- go test -race -count=3 ./internal/catalog: ok.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on
  catalog, provider and agent: ok.
- Full suite, go test -count=1 -timeout 45m ./... on this batch at load
  ~380: every package ok but agent and gateway (past 45m, #2869),
  TestQueueFullAndWaitedOut (#3061), TestRequestBuildsProvidersOnce
  (#2965), TestMainWindowCorners (#2645, red alone on main too) and the
  app.js budget (raised in its own commit). gateway alone: ok but
  TestClaudeAccountsSpentFallBackToCodex (#2858); agent alone past 45m
  at load ~600, its codex/running/connect tests ok alone.

Not done here: a singleflight in provider's heldOf was written and
taken out again; a waiter blocking on another request's build can
deadlock if that build needs a lock the waiter holds, and nothing here
proved it can't.
2026-10-20 20:23:58 +08:00
..