The Sessions read of a Claude history decoded a prompt line whole, so
its content (pasted text, images) was copied into a json.RawMessage
for each line, then copied again as the prompt's text; and each reply's
usage object, a few numbers, went through a json.Unmarshal of its own.
- claudeFull decodes its line as the calls' read does (ccDecode, now
ccDecodeLess for any line): content nulled, given as the bytes of the
line that hold it. Nothing keeps those bytes: the prompt's text and
title are copies, as before.
- ccReply reads its usage with ccDecodeUsage, in place through gjson.
What it can't be sure of reading as json.Unmarshal does (a known key
in another case, a number that isn't a plain integer, a value of
another type, cache_creation twice, invalid JSON) goes to
json.Unmarshal.
Measured on mote, cold start over a copy of real Claude and Codex
histories (acold.sh, 120s), against v0.1.3155's build at the same base:
ccReply 540MB -> 395MB, claudeFull 450MB -> 412MB,
encoding/json.(*RawMessage).UnmarshalJSON 209MB -> gone.
mote's load was 74 to 126 from other sessions' runs, so the 120s
totals and peaks moved between runs of one binary (footprint peak
193-228MB, heap peak 164-194MB for this build; 180-181MB and
163-176MB for the base); neither is claimed as a change.
Tests:
- TestCCUsageDecodedAsJSONUnmarshal: the same fields or the same
refusal as json.Unmarshal for 30 hand-made shapes and 3000 random
objects. Red by hand with the case-folded key check taken out.
- TestCCUsageReadInPlace: Claude Code's usage read with at most 1
allocation (its cache_creation); 7 by hand with json.Unmarshal.
- TestClaudePromptContentNotCopied: a 1 MB prompt line read whole
makes 1.05 MB (its text once), bar 1.5x the line; 2.09 MB by hand
with the old decode. The content given is the line's own bytes, and
the title, prompt count, cwd and session id are read as before.
Run on mote at the pushed tree: go vet and go test ./internal/sessions.
Not run: the full suite (last at v0.1.3152's change: 85 packages ok,
reds only #3949 and #5481).