Every poll while an agent works matches a year's history again:
matchedBlocks and codexChunkResponsePairs grew their maps (byID,
reserved, used, groupOf, picks, picked, ...) and slices from nothing on
each call. They were ~3.0 GB of the ~40 GB a poll-heavy 80 s allocated
on the real-history harness (d1951 heap diff, 120 s to 200 s).
A sync.Pool was tried first and kept nothing in real use: under
tightenGC a poll collects 4-5 times a second (1029 GCs in 230 s), each
collection empties the pool, and the matcher's flat allocation went
only from 2241 to 1840 MB. So one scratch of each is kept in an
atomic.Pointer, emptied after a call (clear() keeps the maps' buckets,
and drops every chunk the call saw), and let go by usage.ReleaseScratch,
which releaseIfPagesRest calls before FreeOSMemory: once the window's
reads have rested 3 s, what the matching kept goes back with the rest.
A call that finds the scratch taken makes its own, sized as before.
matchedBlocks' result map is now made once the id matches are known,
not grown from the response pairs alone.
Measured on mote (Apple M5 Pro), at aeacc6358 + this change:
- BenchmarkPairRows (2000 files x 100 calls), -count 3: 100.8 MB/op,
7,056 allocs/op at aeacc6358; 48.4 MB/op, 5,458 allocs/op after.
- Real history (copy of the user's usage.jsonl, sandbox HOME, polls +
appended calls, run2278d.sh 230 s), heap alloc 120 s to 200 s:
matchedBlocks flat 2241 -> 1295 MB, codexChunkResponsePairs flat
806 -> 143 MB, of a total about the same (39.8 vs 39.3 GB). That run
was before the result-map change. Its CPU and peak RSS (974 MiB vs
936) aren't comparable: mote's load went from ~25 to 50-88 from other
sessions during it.
Tests:
- New TestPairScratchKeepsNothingBetweenCalls (pairRows on one history
after another matches what a first call does; ReleaseScratch lets go)
and TestMatchScratchForgetsTheLastCall (a gateway call one call used by
id or reserved by a response pair is matched in the next; ids the last
call looked up in another gateway aren't).
- Broken by hand, each red: without clear of byID (index out of range),
used ("the last call's id match held the gateway's call"), reserved
("the last call's response pair held"), groupOf and picks (index out
of range). ids, named and picked only choose which chunks are opened,
so a stale entry there costs a read, not a match.
- go test -count=1 -timeout 45m ./internal/usage/ ./internal/usageapi/
./internal/sessions/ ./internal/gui/ at aeacc6358 + this change: usage,
usageapi, sessions ok; gui red in TestMainWindowCorners/macOS_26_SDK
alone, mote's macOS 27 (#3949). An earlier gui run there had ten
AppKit window tests killed at their 20-40 s deadlines at load ~40-88
(#2645).
- GOOS=linux, windows, darwin go vet -tags nogui ./internal/usage/
./internal/gui/ ./internal/usageapi/: ok.
Rebased onto origin/main fe05f0a72 (library and Claude-account changes,
nothing in internal/usage or page_memory.go); after the rebase
./internal/usage/ and ./internal/usageapi/ ran again at the pushed
tree: ok, and go build . ok.