scanAt gathered a line longer than its read buffer by appending one
buffer's worth at a time. Past 256 elements append grows a slice by
1.25x, so an 8 MiB line allocated ~37 MB, and a line past maxLine was
gathered to 32 MiB before being dropped (~175 MB allocated). The session
list and the call readers both read every session file this way at a
start, so a history whose prompts carry pasted screenshots (each a
multi-MB base64 line) allocated GBs in scanAt alone.
On the first overflow of a plain file's line, scanAt now reads ahead
(ReadAt, which moves nothing) to the line's end and gathers it into one
buffer of exactly that size. A line longer than maxLine plus the buffer
is stepped over at once, as before, but without being gathered first. A
packed (zstd) file, or a line with no end yet, keeps the old path.
Tests (internal/sessions/scan_buffer_test.go):
- TestScanGathersALongLineOnce: an 8 MiB line and a short one are both
handed over whole, and the scan allocates at most 1.5x the line.
Fixed: 9,451,632 bytes for an 8,388,633-byte line (bound 12,582,949).
With the read-ahead turned off by hand: red, "allocated 36856408
bytes".
- TestScanStepsOverATooLongLineUngathered: a maxLine+4 MiB line is
stepped over and the next line read, with at most 8 MiB allocated.
Fixed: 1,049,032 bytes. Turned off by hand: red, "allocated 175179080
bytes".
Measured: a sandbox `magpie web` on a fixture HOME (150 Claude Code
sessions, each with six 4 MiB pasted-image prompts, 3.5 GB), main and
panel pages loaded in Playwright Chromium, heap profile at ~2.5 min,
macOS at a load average of ~290, two runs each:
allocated in scanAt peak RSS
base 5b267fbd8 3.97 GB 3.31 GB 394 MB
base 5b267fbd8 4.05 GB 3.37 GB 277 MB
this commit 1.52 GB 1.14 GB 109 MB (pages failed to load)
this commit 1.89 GB 1.20 GB 348 MB
What allocation shows is halved; peak RSS on this loaded machine is
noise (277-394 MB on the base) and does not show a change. The #2515
report's 3.2 GiB peak (v0.1.1265, Windows) was not reproduced: on a
5,501-file fixture v0.1.1265 peaked at 250 MB RSS and the base at 288
MB, and on 950 MB of large sessions the base peaked at 256 MB. This
commit is not claimed to be its cause; it is the largest start
allocation left on these fixtures.
Run in a sandbox HOME, Go caches pinned:
- go test -count=1 -timeout 45m ./internal/sessions/ ./internal/usage/:
both ok (959s, 272s), set -o pipefail.
- gofmt -l internal/sessions: none. go vet ./internal/sessions/,
GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet -tags
nogui ./...: all exit 0.
- Not run: the full suite, the GUI suite (no asset changed), the
Windows box.