Each poll of the Agents page encoded the whole state (1.3 MB with real
accounts) into json's buffer, and the GC between polls empties the pool
that buffer comes from, so it grew to the state's size again each poll:
48 MB of what twenty rounds allocated (v0.1.2477 build, real-account
sandbox), and 68 MB at v0.1.2463.
writeStateJSON encodes the state without its agents twice (as sent, and
as keepState keeps it, without the settings), and each agent once,
straight to the response and to keepState's sha256, through one encoder.
keepState is split so the handler hands it that sum (keepStateSummed).
When the splice can't be made (no agents, or a field put before
"agents"), it writes and sums the state whole, as before.
Measured on this Mac with the real-account sandbox (abr.sh, 20 rounds of
drift + state + providers):
- v0.1.2477: writeJSON under /api/state 48 MB, 511 MB in all.
- this change: writeStateJSON 15 MB, 477 MB in all. The /api/state
bytes are identical to v0.1.2477's (cmp).
Run on mote (mote-run, Go 1.26.8):
- gofmt clean; go vet ok, also GOOS=windows.
- TestStateWrittenAsEncoded (same bytes as Encode, same sum as keepState,
for a large state, an empty agent list and none) and
TestStateWriteGrowsNoBufferItsSize, with TestKeptState* and TestLast*:
ok 3 of 3.
- Broken by hand (the whole-state path always taken): red, "writing a
1614586-byte state allocated 4207139 bytes, want under half its size".
- Not run before this push: the full internal/gui package; it runs next.