Commit Graph
2 Commits
Author SHA1 Message Date
yetone ba1dc7f975 tests: a decoder test counts its cheapest read, so another goroutine's allocations don't fail it (#3283)
TestPackedScanKeepsItsDecoder went red on macOS CI at 3a213e1dc, a
CSS-only commit: 665,955 bytes against a 512 KiB bar. allocated()
averaged runtime.MemStats.TotalAlloc over its calls, and TotalAlloc
counts every goroutine in the process, so another test's leftover work
was billed to the scan. It now returns the least any one call
allocated. A decoder made anew costs its window on every call (about
3 MiB for a packed rollout), so the least still shows it; work that
overlaps one call no longer does. TestDshFramesKeepTheirDecoder shares
the helper and gets the same fix.

Run at 8c68b6802 (origin/main), internal/sessions only (test-only change):
- Injected noise (a goroutine allocating 256 KiB every 8 ms): old
  helper red 3 of 4 runs, new helper green 4 of 4. Two forced
  runtime.GC() calls between scans leave the old helper green, so the
  pool being emptied is not the cause.
- Broken by hand: openLines making a new decoder each read turns
  TestPackedScanKeepsItsDecoder red (3,330,376 bytes); frameDecoders.New
  in place of Get turns TestDshFramesKeepTheirDecoder red (165,512).
- go test ./internal/sessions/ ok; the two tests -count=10 ok; gofmt
  clean; vet darwin, linux (-tags nogui), windows ok.

Not run: the full suite. Nothing outside this test file changes.
2026-10-22 22:03:16 +08:00
yetone 21c2ab6fa5 sessions: packed session files are decompressed by decoders kept between reads
Why: a live heap profile of v0.1.1296 (pid 554, up 67 min) allocated
173 GB, 75 GB of it in zstd decoding, through sessions.scanAt from both
usage.prefetchSources (ReadCallSource → readCalls) and sessions.refresh
(parse). openLines made a new zstd decoder for every packed file it
opened, and a decoder's first frame allocates its window: the Codex app
packs rollouts with a 2 MiB window, which a low-memory decoder holds in
3 MiB, plus ~350 KB of block buffers. scanFrames (dsh .zstd) did the same
on every read on.

What was found, on 180 real packed rollouts (360 MB, copied with their
times) and the profile:
- An unchanged packed file (same size and time) is not opened again by
  either path, while magpie runs or after it starts from what it saved
  (Saved, Reset): 180 scans each path cold, 0 on every later round. A
  changed one is read again alone.
- The 75 GB was each path's one full pass over all 11,591 packed rollouts
  at the app's start, both caches invalid for them then: about 3.4 MB a
  file for each path's decoder, and its 1 MiB copy buffer to pass over
  what came before off.
- dsh files are already read on from their last whole frame
  (TestDshPackedReadsOnlyWhatWasAppended, TestDshCallsReadOnlyWhatWasAppended).

What changed:
- openLines takes a decoder from a pool (streams) and Resets it onto the
  file; zstdFile's Close Resets it to nil and puts it back. Read or Close
  after Close is os.ErrClosed. IOReadCloser isn't used, since its Close
  closes the decoder for good.
- scanFrames decodes frames with a pooled decoder of its own pool
  (frameDecoders): a decoder that has read a stream holds its block
  decoder, which DecodeAll would wait for. Its reader comes from
  lineReader's pooled buffers, not a new bufio.Reader.
- scanAt passes over what comes before off in a packed file through its
  pooled line reader (Discard), not io.CopyN's buffer of its own.
- Decoder options and window limits are unchanged, so what is read is
  the same; an idle pooled decoder is let go by the garbage collector
  with the pool, and as many are held at once as files are read at once,
  as before.

Numbers:
- go test -bench 'PackedScan|DshFrames' -benchtime 40x, before → after:
  - an 8 MB Codex rollout, 2 MiB window, scanAt from 0: 3,364,175 →
    110,902 B/op, 25 → 6 allocs/op;
  - a 6 MB dsh session of 8-line frames, scanFrames whole: 1,959,606 →
    491,117 B/op, 63 → 22 allocs/op;
  - the same, read on from its last frame: 182,087 → 74,361 B/op, 34 → 9
    allocs/op.
- TestPackedScanKeepsItsDecoder (a warmed scan): 3,330,508 → 452 bytes.
- The 180 real rollouts, read cold by List, CallSources and
  ReadCallSource: 1,294 and 1,299 MB → 375 and 386 MB allocated (two
  runs each). The sessions and calls read (177 sessions, 306 calls) are
  byte for byte the same JSON before and after.

Tests (packed_decoder_test.go):
- TestPackedScanKeepsItsDecoder and TestDshFramesKeepTheirDecoder fail on
  the old code (3,330,508 and 181,995 bytes) and with either Put taken
  out by hand (3,330,344 and 165,539 bytes). Skipped under -race, where
  sync.Pool drops what it is given.
- TestPackedScanSameThroughAKeptDecoder: lines and places through kept
  decoders are what a decoder of its own reads, for a large file after a
  small one cut short, read on from a line, past the end, and by 8
  readers at once.
- TestUnchangedPackedNotReadAgain passes on the old code too (it held
  already). It fails when refresh's or readCalls's size-and-time check is
  broken by hand for packed files.

Run, at this commit's tree:
- go test -timeout 45m -count=1 ./internal/sessions/ ./internal/usage/:
  ok (79s, 39s).
- go test -race -count=3 -run 'TestUnchangedPacked|TestPackedScan|
  TestDshFrames|TestCodexPacked|TestDsh.*Packed|TestDsh.*Append|Frame'
  ./internal/sessions/: ok.
- gofmt -l internal/sessions: nothing. GOOS=linux go vet -tags nogui
  ./... and GOOS=windows go vet -tags nogui ./...: exit 0.
- go test -tags nogui -timeout 45m -count=1 ./... under a scratch HOME,
  GOPATH, GOMODCACHE and GOCACHE pinned: exit 0, every package ok (65,
  no FAIL); gateway 802s, agent 733s, provider 355s, sessions 273s,
  usage 92s.
Not tried: in the running app; on real dsh files (none on this machine,
so the profile's zstd was all Codex .zst). Why both caches were invalid
for the packed rollouts at that start is not found here.
2026-10-20 23:11:32 +08:00