Files
magpie/internal/sessions/cc_decode_test.go
T
yetone 81d6a3294b sessions: a first start's read of Claude Code history makes 39% less garbage (#2278)
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.
2026-11-06 14:58:40 +08:00

68 lines
4.2 KiB
Go

package sessions
import (
"encoding/json"
"reflect"
"runtime"
"runtime/debug"
"strings"
"testing"
)
// ccDecode reads a reply line as json.Unmarshal of it whole did, less its
// content: strings escaped or not, bytes that aren't UTF-8, a field that
// holds no string, a line with no content or with content first.
func TestCCDecodeAsUnmarshal(t *testing.T) {
big := strings.Repeat(`{"type":"text","text":"a \"quoted\" line\n"},`, 2000)
lines := []string{
`{"parentUuid":"p","isSidechain":false,"userType":"external","cwd":"/Users/x/w","sessionId":"s-1","version":"2.1.5","gitBranch":"main","message":{"model":"claude-opus","id":"msg_1","type":"message","role":"assistant","content":[` + big + `{"type":"text","text":"end"}],"stop_reason":null,"usage":{"input_tokens":3,"cache_creation_input_tokens":10,"cache_read_input_tokens":20,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":10},"output_tokens":7,"service_tier":"standard"}},"requestId":"req_1","type":"assistant","uuid":"u-1","timestamp":"2026-11-05T10:00:00.123Z","entrypoint":"cli","effort":"high"}`,
`{"type":"assistant","cwd":"C:\\Users\\x \"q\"","sessionId":"\u00e9t\u00e9","requestId":"r\/2","message":{"content":"plain","id":"m","model":"m"},"uuid":"u","timestamp":"2026-11-05T10:00:00Z"}`,
"{\"type\":\"assistant\",\"cwd\":\"bad \xff\xfe utf8\",\"sessionId\":\"ok\",\"message\":{\"id\":\"m\",\"content\":[]},\"timestamp\":\"2026-11-05T10:00:00Z\"}",
`{"type":"assistant","cwd":42,"sessionId":null,"requestId":["x"],"error":{"a":1},"effort":true,"perTurnEffort":null,"message":{"id":"m","model":"m","usage":{"input_tokens":1}},"timestamp":"2026-11-05T10:00:00Z"}`,
`{"message":{"content":[{"type":"tool_use","input":{"cwd":"no"}}],"id":"m"},"type":"assistant","cwd":"after","timestamp":"t"}`,
`{"type":"assistant","isApiErrorMessage":true,"error":"rate_limit","message":{"model":"<synthetic>","content":[{"type":"text","text":"API Error"}]}}`,
`{"type":"assistant","cwd":"","sessionId":"\u2028\t","message":{"content":null}}`,
`not json`,
``,
}
for _, line := range lines {
var want, got ccCall
werr := json.Unmarshal([]byte(line), &want)
gerr := ccDecode([]byte(line), &got)
if (werr == nil) != (gerr == nil) || !reflect.DeepEqual(got, want) {
t.Fatalf("%.80s:\n got %+v %v\nwant %+v %v", line, got, gerr, want, werr)
}
}
}
// A reply line is decoded without a copy of it or of its content, and
// with no decoder of its own for each string field: a first start's read
// of a 21 GB history made ~140 MB of those that nothing kept (#2278).
func TestCCDecodeAllocations(t *testing.T) {
content := `[{"type":"thinking","thinking":"` + strings.Repeat("x", 40<<10) + `","signature":"s"},{"type":"text","text":"done"}]`
line := []byte(`{"parentUuid":"0b6e","isSidechain":false,"userType":"external","cwd":"/Users/someone/workspace/projects/app","sessionId":"5f2a9c1e-8d1b-4a5e-9c33-2f1e0a7b6c4d","version":"2.1.5","gitBranch":"main","message":{"model":"claude-opus-4-1","id":"msg_01ABCDEF","type":"message","role":"assistant","content":` + content + `,"stop_reason":null,"usage":{"input_tokens":3,"cache_creation_input_tokens":10,"cache_read_input_tokens":20,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":10},"output_tokens":7,"service_tier":"standard"}},"requestId":"req_011CABCDEF","type":"assistant","uuid":"8c1d2e3f-0000-4000-8000-000000000001","timestamp":"2026-11-05T10:00:00.123Z","entrypoint":"cli","effort":"high"}`)
var l ccCall
if err := ccDecode(line, &l); err != nil || l.Cwd != "/Users/someone/workspace/projects/app" || l.Message.Usage == nil || l.Message.Usage.Output != 7 {
t.Fatalf("%+v %v", l, err)
}
runtime.GC()
defer debug.SetGCPercent(debug.SetGCPercent(-1)) // a collection empties the pool
var ms runtime.MemStats
least := uint64(1 << 62)
for range 5 {
runtime.ReadMemStats(&ms)
before := ms.TotalAlloc
for range 50 {
var l ccCall
ccDecode(line, &l)
}
runtime.ReadMemStats(&ms)
least = min(least, (ms.TotalAlloc-before)/50)
}
// ~900 bytes: the strings and usage it keeps, and json.Unmarshal's own;
// a decoder for each string field made ~1.7 KB, a copy of the line 52 KB
if least > 1300 {
t.Fatalf("a %d-byte reply line made %d bytes, want at most 1300", len(line), least)
}
}