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.
Why: a 48-minute heap profile of the user's magpie allocated 6.3 GB
in sessions.scanAt, while ~157 MB was live. bufio.NewReaderSize made
~3.5 GB of it: each scan made its own buffer, up to 1 MiB (scanBuffer)
for a big file, and threw it away at the end. The earlier commits cut
how often unchanged files are scanned. This one removes the buffer
cost for the scans that still happen: a first read, a grown file, the
calls ledger.
What changed:
- scanAt takes its bufio.Reader from lineReader. lineReader keeps a
sync.Pool per power-of-two size from 16 KiB to 1 MiB and gives the
reader back (Reset to nil) when the scan returns.
- A line from the reader was already valid only for the callback that
got it: long lines are copied into `long`, and callers that keep a
line copy it.
- Tests run with poisonReleased set in TestMain. A buffer given back
is overwritten with 0xFF, so a caller that kept a line past its
scan reads garbage and fails, rather than reading the next scan's
bytes in a user's magpie.
- sync.Pool drops items at random under -race, so the allocation test
is skipped there (race_on_test.go / race_off_test.go).
Tests:
- TestScansShareTheirBuffers scans a ~2.4 MB file 50 times and needs
under 128 KiB allocated per scan.
- TestALineKeptPastItsScanIsGarbageInTests checks the poison.
- Broken by hand: with the pool off (a new reader every time), the
first test goes red with "a scan of a 2.4 MB file allocated 1049911
bytes". Green again once restored.
- The whole sessions package passes with the poison on.
Measured on a read-only APFS clone of the user's ~/.claude/projects
and ~/.codex/sessions/2026/10, in a sandbox HOME, at load ~500. Cold
StatsAndNative(0) reads every file once:
| Code | Allocated | Allocations | Time |
| --- | --- | --- | --- |
| origin/main dee5c9d75 | 11,999.5 MB | 24,817,390 | 2m4.7s |
| this series | 5,672.0 MB | 24,431,553 | 1m5.9s |
An earlier run with the pool and the stats change but not the lazy
subagent title: 5,773.3 MB, 24,497,066 allocations. The cold-pass
drop is this commit's.
Run at this tree:
- gofmt -l internal: nothing.
- GOOS=linux and GOOS=windows go vet -tags nogui ./...: clean.
- go test -count=1 -timeout 45m ./internal/sessions/ ./internal/gui/:
- sessions: ok, 366.7s.
- gui: only TestMainWindowCorners failed, "measuring the windows:
signal: killed". It also fails alone on an untouched origin/main
export, at load ~500. Filed as #1936 and #2645. Every other gui
test passed.
- go test -race -count=3 on the scan, subagent and stats tests: ok,
207.8s.
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.
d7e02ae08 sized scanAt's buffer by the rest of the file. The other line
readers of a session file still took 1 MiB per file read:
- readMiniMaxCalls, readAgyCalls and readWorkBuddyCalls (WorkBuddy and
CodeBuddy Code), which read a changed file whole;
- scanFrames, dsh's packed (.zstd) sessions, read on from a frame;
- mayHold, a search's first look at each file.
(calls_pi, calls_kimi, calls_dsh and dsh.go no longer hold one since
the incremental readers before a77f6ae16.) Each now takes
scanBuffer(f, off): the rest of a plain file plus one byte, 16 KiB to
1 MiB. ReadBytes and readFrame read a line or frame longer than the
buffer whole, as before; a packed WorkBuddy file (a zstd stream, not
an *os.File) keeps 1 MiB.
TestReadersOfASmallFileTakeASmallBuffer reads a two-line file 50 times
through each of the five and allows 256 KiB per read. With the five
readers put back (the test kept): red in all five, 1,049,161 (minimax),
1,049,271 (agy), 1,049,256 (workbuddy), 1,068,296 (frames) and
1,056,984 (search) bytes per read.
Not measured on the 600k sandbox: it holds no MiniMax, agy, WorkBuddy or
dsh sessions, so the saving there is the test's: ~1 MiB per file each
time one of these is read again.
Full suite on this series' final tree (aa678b482 before this note was
added), rebased on origin/main f76201a84: go test -p 4 ./... -timeout
45m, exit 0, 64 packages ok and none failed (gateway 1783 s, gui 361 s),
41 minutes at load ~190-200. gofmt -l: no files. go vet: darwin, linux
(-tags nogui) and windows exit 0. The GUI suite wasn't run: no file
under internal/gui/assets changed.
scanAt gave every scan a 1 MiB bufio buffer. A start reads ~4,600
mostly small session files (and reads each again from where it was
left), so those buffers were the largest single allocation of a start:
9.4 GB of the 22.4 GB a warm start allocated on the sandbox below. The
buffer is now the rest of a plain file plus one byte, at least 16 KiB
and at most 1 MiB; a packed (zstd) file keeps 1 MiB. A line longer than
the buffer (a file grown since its size was read) is still read whole
through scanAt's long-line path.
Tests (internal/sessions/scan_buffer_test.go):
- TestScanOfASmallFileTakesASmallBuffer: 200 scans of a two-line file
allocate under 64 KiB each. With the 1 MiB buffer back: red, "a scan
of a two-line file allocated 1048871 bytes".
- TestScanReadsALineLongerThanItsBuffer: a line three buffers long, then
a short one, are both handed over whole.
Measured on a sandbox instance (600k gateway calls, ~4,600 sessions),
base 1a3b92688 against this branch with every #2776 commit, back to back
at a load average of 340-450 (nine agents running suites), heap profile
after the four first API calls and 60s idle:
allocated of it scan buffers live heap
warm base 22.40 GB 9.40 GB 483 MB
warm new 15.86 GB 1.98 GB 498 MB
cold base 22.48 GB 9.31 GB 450 MB
cold new 15.76 GB 1.98 GB 498 MB
What is left in bufio.NewReaderSize is the other call readers' own
1 MiB buffers (calls_pi, calls_kimi, ...). Peak RSS did not drop at this
load (base 4.38/4.41 GB, new 4.59/4.54 GB): the peak comes from the four
first calls each materialising the 600k-call ledger at once
(gatewayRecords appends every Record, 2.56 GB allocated; ledgerOfAt
6.2 GB for CodexModelEffort), which this commit does not change.
Run on origin/main 2ceff7370 with all four #2776 commits' changes
(this one and the three beside it), load average 291-568 (nine agents
running suites), macOS:
- go test -p 4 ./... -timeout 45m, 09:35-10:49: 62 packages ok,
among them agent (2310s), gateway (1707s), provider (764s), usage
(83s). Red:
- internal/sessions: TestWSLOmpProfiles, TestWSLStoppedDistroKept,
TestWSLStoppedSinceNotOpened, TestWSLNoneWithoutHomes, each
"TempDir: mkdir ...: no space left on device" (the disk filled
mid-run). After space was freed, ./internal/sessions alone: ok
(139s).
- internal/gui: TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt,
TestPanelJoinsFullScreenSpaces, TestPluginPageLoadsItsFilesInTheWindow,
TestDockReopenKeepsTheWindowsSpace (each killed at 30s) and
TestMainWindowCorners (86s): the real-window AppKit tests under
load, already filed as #2645 and #1936. Rerun alone at load ~335:
all pass, with TestSetAnswersWhileDevinAndProvidersHang (17s).
- gofmt -l: none. go vet: darwin, GOOS=linux -tags nogui and
GOOS=windows all exit 0.
- No internal/gui/assets file changed, so the GUI Playwright suite was
not run.