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.