Files
magpie/internal
yetone 85e4c693ee sessions: a packed rollout's long line is gathered once, at its own size
scanAt reads a line longer than its 1 MiB read buffer as several chunks.
For a plain file, a7e2c7235 (#2515) reads ahead to the end of the line and
gathers it into one buffer of exactly that size. A packed (.zst) rollout
can't read ahead, so it still grew the line by append, a chunk at a time.
That allocated about 3.8 times the line. A cold read of 120 of the owner's
real packed rollouts allocated 2.2 GB, and 1.61 GB of it was this append.

Lines the head says no to were already stepped over without being
gathered. On this corpus the gathered long lines were all wanted ones.
There were 63 user messages with pasted images on the session side
(422 MB), which codexHead lets through for Prompts and Title. There were
17 compactions (70 MB). There were also 14 plain-file messages (19 MB),
which were already sized exactly. These lines are read whole:
cxFastPrompt looks through every content part, and cxCompactionIn uses
gjson to find fields anywhere in the line. So the line still has to be
handed over whole, and this change is about how it is gathered.

- A packed file's long line is copied into pooled 1 MiB blocks until its
  end. Then it is gathered once, into a buffer of its own length.
- That buffer comes from a pool (longLines) and goes back to it when the
  scan ends. A line from it is good only until then, which is the same
  rule as for the read buffer. Under the tests (poisonReleased) the
  buffer is filled with 0xFF when it is given back.
- The plain-file read-ahead path is unchanged, apart from taking its
  buffer from the same pool.

Measured on copies in a sandbox: 120 real .jsonl.zst rollouts (1.30 GB
packed, 1.95 GB unpacked; the 60 largest and 60 recent), 30 plain Codex
.jsonl (213 MB) and 40 Claude Code project .jsonl (60 MB). CODEX_HOME and
CLAUDE_CONFIG_DIR pointed at the copies, with a fresh testenv home each
run. Each run was List(100000), StatsAt(3650, fixed now), ReadCallSource
for each of the 190 CallSources, and Calls(time.Time{}), in its own
process. Three runs each, at load average ~30-40:

                    TotalAlloc         wall
  base 9afdeb89c    2161/2198/2203 MB  3.12/2.39/2.52 s
  this commit        668/ 678/ 724 MB  2.54/2.32/2.22 s

All 4 JSON encodings (List, StatsAt, every source's calls, and Calls)
are byte-identical to the base's, checked with cmp on all 3 runs of
each binary. After the change, pprof puts scanAt's own allocation at
~134 MB for long-line buffers and ~96 MB for blocks that the pool
dropped at a GC, down from 1.79 GB.

Tests (internal/sessions/scan_buffer_test.go):
- TestScanGathersAPackedLongLineOnce: a .jsonl.zst with a wanted 20 MiB
  line, a 20 MiB line the head says no to, and two short lines. The
  wanted line is handed over byte for byte. The scan allocates at most
  2.5 times the line plus 16 MiB for the decoder's window. It passed
  -count=20. With the base scanAt put back by hand it is red 3 of 3:
  "allocated 106639008/117266584/106633368 bytes, want at most
  69206078".
- TestBlocksKeepALineInOrder: blocks give back across a block's end
  what they were given.

Run in a sandbox HOME, Go caches pinned, set -o pipefail:
- gofmt -l internal cmd: none.
- GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet -tags
  nogui ./...: both exit 0.
- go test -count=1 -timeout 45m ./internal/sessions/ ./internal/usage/
  ./internal/gui/: ok (23.8s, 21.1s, 105.7s).
- go test -count=1 -race ./internal/sessions/: ok (52.9s).
- Not run: the full suite, the GUI Playwright suite (no asset changed),
  the Windows box, a live magpie's RSS.

Siblings not changed here:
- traces.go's TraceReader reads plain rollouts with ReadBytes. That read
  is bounded by its 8 MiB poll budget.
- frames.go's dsh frames append a line split across frames.
- plugin/host.go's readLine is plugin IPC, not session files.
2026-10-21 00:07:49 +08:00
..
…
…