writeCalls held a whole write in one bytes.Buffer, grown by doublings,
before any of it went to the file, and the frame's patch list grew by
append from empty whenever a collection had emptied its pool, as it does
all through a first start. In a 120s cold start on mote over real
history copies (#2278 recipe), the list's growth was 148 MB and the
buffer's 150 MB of writeCalls' 556 MB.
Now each frame is encoded into one pooled buffer and written to the file
at once, so a write holds at most one frame (~1 MB, where a 52k-call
session held 8 MB), and the list is grown once to the first frame's size.
A write that fails after its first frame leaves frames ending in More,
which loadCalls already reads as a write cut off: the file is read again.
Measured, rc4 (303a31dd6's tree) against rc5 (this change), 120s cold on
mote: the list's line is gone from the profile, bytes.growSlice 150 ->
70 MB while rc5 did 1.39x the writes (gob's encoder buffers 240 -> 334
MB); writeCalls 556 -> 583 MB for that 1.39x, ~25% less a write. What is
left is each frame's gob encoder's own buffer, which a frame decoded on
its own needs.
TestCallsWriteAfterACollection writes 10240 calls after two collections:
4.9 MB, bar 6 MB. By hand: the list not grown at once 7.4 MB red, the
whole write held 6.8 MB red; the old code 9.3 MB. It reads the file back
call for call.
Run at the rebased head on mote: gofmt; go vet ./internal/sessions for
darwin, linux and windows; go test -count=1 ./internal/sessions ok.
Not run: the full suite (last at v0.1.3152's change, 85 packages ok).
A first start (or one after the session caches are gone) reads every
Claude Code transcript to count calls. On a real 21 GB history this
allocated 14.6 GB and ran 460 GCs. Three things made most of what was
thrown away right after:
- ccWithoutContent made a new copy of each reply line with its content
set to null (82 MB), and gjson.GetBytes copied the content to find it
(27 MB). ccDecode now builds the stripped line in a pooled buffer and
finds the content through an unsafe.String view, with no copy.
- ccStr.UnmarshalJSON went through json.Unmarshal for each string, which
made a decoder for each one (29 MB). A string with no escapes and valid
UTF-8 is now taken as its bytes. Anything else still goes to
json.Unmarshal, which writes U+FFFD for bad bytes.
- writeCalls made a new Patches list for each frame (1 GB). It now reuses
one list from a pool and clears it before putting it back, so the pool
keeps no reference to the call state.
Measured on mote with the real 21 GB history copied into a sandbox HOME,
with the session caches deleted before each run, over 120s:
v0.1.3118: 14.6 GB allocated, 460 GCs, CPU 3:17, footprint peak 195 MB
this: 8.96 GB allocated, 277 GCs, CPU 3:19, footprint peak 197 MB
The footprint peak is what is live (the caches), so it does not change.
Tests:
- TestCCDecodeAsUnmarshal holds ccDecode to json.Unmarshal of the whole
line. Its cases are escaped content, non-UTF-8 bytes, content first,
an error line, not JSON, and an empty line.
- TestCCDecodeAllocations allows at most 1300 bytes for a 41 KB reply
line.
- TestCallsWriteKeepsNoPatchListPerFrame allows at most 6 MB for a
6144-call write.
Both allocation tests turn GC off while they measure, because a GC
empties the pool; they passed 5 of 5. Broken by hand: with the line
pool removed, the decode test made 1984 bytes and went red. With the
Patches list made per frame again, the write test made 13056 KB and
went red.
Full suite on mote at 3f53fa1f3 plus this change: gofmt clean, go vet ok (darwin, linux nogui, windows), go test -timeout 45m ./...: 86 packages ok; internal/gui red only in TestMainWindowCorners/macOS_26_SDK, the known #3949 (macOS 27 corners), not touched here.