36ddd5761 made sessions.Active.Hours the unexported hours type, and
internal/gui's TestStatsAnswerWritesWhatEncodeDid still built it as
[]int64, so the package's tests didn't compile on main. The test now
reads its Active from JSON, as the cache gives it, so the hours still
go through MarshalJSON in both answers.
Run on mote at this head: go vet ./internal/gui/ ok;
go test -count=1 -run 'TestStatsAnswer|TestSameAnswer' ./internal/gui/ ok.
Not run: the full suite (follows after the push).
Fixes#5806
Under -race sync.Pool lets a quarter of what is put in it go, so json's
encoders and regexp's matchers are made again: four gui tests bounding
what a path allocates went red under -race at fd70def82, and at the
base before it. TestProviderInfoReadsTheListOnce measured a row at 88
MB against 13 MB without -race, all in catalog.FamilyOf's regexps.
raceOn (as internal/gateway has it) now skips the bound in
TestStateWriteGrowsNoBufferItsSize, TestProvidersWriteGrowsNoBufferItsSize,
TestJSONRowsNotHeldWhole and TestProviderInfoReadsTheListOnce (after its
row checks). Without -race they run as before.
Run on mote at this tree:
- gofmt, go vet ./internal/gui, GOOS=windows and GOOS=linux go vet
-tags nogui: ok.
- The four tests without -race: PASS.
- go test -race ./internal/gui: only TestSettingsSaveKeepsWhatItDoesNotSend
(#4514) and TestMainWindowCorners (#3949) red, which are red without
-race too.
Picked onto 7da5b34aa (sessions, gateway); not run again there.
writeSame wrote each answer into a buffer to hash and send it, and kept
two such buffers between polls (answerLinger). The Sessions tab's stats
answers are ~6 MB each over a full history, so 12.5 MB of a warm start's
169 MB live heap was those buffers (heap profile, bytes.growSlice under
statsAnswer). Now write is given an io.Writer and called twice: once into
the hash for the tag, and once straight to rw (32 KB bufio) when the page
hasn't got that answer. statsAnswer encodes a part at a time into a small
buffer. The stats route still answers "same" from its kept tag without
encoding at all when nothing changed, so the second encode costs only on
a changed answer. answerBuffers, answerBuffer, keepAnswer go.
Run on mote (M5 Pro), from this tree:
- go vet ./internal/gui ok; gofmt -l clean.
- go test -count=1 ./internal/gui: FAIL only TestSettingsSaveKeepsWhatItDoesNotSend
(#4514) and TestMainWindowCorners/macOS_26_SDK (#3949), both filed, both
untouched by this change. TestStatsAnswerWritesWhatEncodeDid (bytes are
the whole Encoder's) and the new TestSameAnswerNotHeldWhole pass.
- Broken by hand: writeSame's bufio sized 64 MB, TestSameAnswerNotHeldWhole
red ("allocated 67117776 bytes for a 4194304-byte answer").
- Warm start on a copy of the owner's real history (11,925 Codex .zst,
4,696 Claude .jsonl, 5,000 listed sessions), the page's 10 reads every
5s, interleaved, load ~25-34:
before (bb5eb83c3): footprint peak 267, 269, 274 MB; cpu 9.6-10.1s
after: footprint peak 255, 257, 258 MB; cpu 9.9-10.2s
Not tried: a cold start A/B (the buffers matter once polls begin).
writeJSONRows built the whole answer in a buffer to hash it and send it.
Over the owner's full history (16,600 sessions, 5.8 MB list) that buffer
grew to 25 MB, was kept between polls, and was 25 MB of a warm start's
183 MB live heap. Now a first pass encodes each row into one small
buffer for its hash and the answer's tag. What is sent is encoded again,
straight to the ResponseWriter through a 32 KiB bufio.Writer, and a delta
holds only the rows the page hasn't got. The bytes and the tag are the
same as before, so the page's delta and "same" answers don't change.
Run on mote (macOS 27), real history copy (~16,600 session files):
- warm start, 75 s of poll.sh (what an open window asks every 5 s):
- before (v0.1.2382): steady footprint 240-265 MB, live heap 183-193 MB;
- after: steady footprint 216-233 MB, live heap 168-178 MB.
- The phys_footprint peak is about the same (257-267 vs 264-289 MB over
3 interleaved pairs): a poll's own spike still sets it.
- /api/sessions from each binary on the same history: the same tag
06a661d3…, the same sha256 of 5,842,606 bytes, X-Magpie-Same on its
tag, and the full list for an unknown tag.
- TestJSONRowsNotHeldWhole is new: red on the old writeJSONRows (9.5 MB
allocated for a 4.2 MB answer), green now. TestWriteJSONRowsWritesWhatMarshalDid
(bytes and delta) is green.
- go test ./internal/gui: ok except three reds that are already filed.
TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestMainWindowCorners
(#3949) are red the same way with the old api.go in the same copy.
TestColdTurnsTaken (#4510) was red in the full run and passed 3 of 3
alone.
Not run: the GUI (Playwright) suite. app.js isn't touched and the
answer's bytes are the same.
With the Sessions tab open, every poll grew each long answer's buffer
from nothing: writeJSONRows' `whole` (~8 MB of rows) and writeJSONSame's
buffer plus encoding/json's own encodeState for the stats (~6 MB of
days), which the Encoder grows to the whole value after each
collection. In 90 s of polls while an agent worked, writeJSONRows was
245 MB and the stats' writeJSONSame 209 MB of 2702 MB allocated.
- answerBuffer/keepAnswer keep two buffers, dropped 30 s after the last
answer (as the usage scratch lingers). A ResponseWriter copies what
it is written, and only the rows' sums outlive the call, so nothing
reads a buffer once it is put back.
- writeSame is writeJSONSame over a write func; writeJSONRows and the
stats handler use the kept buffers.
- statsAnswer encodes the stats a Day at a time, byte for byte the
Encoder of struct{ sessions.Stats; Agents } (the page's tag rests on
those bytes).
Bench (mote, ~/r2278/full, hmw.sh: a gateway call every 3 s, 10
endpoints polled every 5 s with X-Magpie-Had; second runs compared),
e13 = 3bce283df, e14 = this:
total alloc 2702 -> 2308 MB, writeJSONRows 245 -> 110 MB,
stats encoding 209 -> 46 MB, /api/sessions/stats handler 306 -> 128 MB,
GCs in 90 s 85 -> 71, fp_peak 347 -> 360 MB (noise band 345-370).
Polls 170 x 200 in both. One e14 run (y14) had 60 answers of "400 Bad
Request" (15 bytes, <6 ms): Go's net/http refusing a malformed request
header from the bench's tag file; y12 at the old code had the same 60,
and a rerun had none. That run is not compared.
Tests: TestStatsAnswerWritesWhatEncodeDid (nil/empty/HTML/U+2028 days,
keys, agents, a hidden Sessions), TestAnswerBufferKeptForTheNext
(writeSame and writeJSONRows put a buffer back that the next answer
takes). Broken by hand, each red: writeSame without keepAnswer (red),
writeJSONRows without keepAnswer (red), statsAnswer keeping Encode's
newline (red).
At 9d6461416 rebased on 9e73f69af (mote): gofmt clean; go vet -tags nogui
./internal/gui/ ./internal/sessions/ for linux, windows, darwin ok;
go test ./internal/gui/ -run 'TestStatsAnswer|TestAnswerBuffer|
TestWriteJSONRows|Sessions|Stats|Same' ok; go test ./internal/sessions/ ok.
Full internal/gui on mote at the pre-rebase tree: every test ok but ten
real-window AppKit tests killed at their 20-40 s deadline (dock order,
panel close/space, plugin page, dock reopen, titlebar corners/RTL, tray
shown, released panel, window up), mote's #4125 / #2645, which this
change doesn't reach.