portRollout and repairLineages ran every rollout they looked at through
editRollout, which opened a temp file (and a zstd encoder for a packed
one) and wrote the whole rollout out before it could know whether any
line changed; a rollout with nothing to change was then thrown away. On
every pass over ~/.codex/sessions that was a full rewrite per file.
editRollout now reads first (eachEditedLine with no writer, which stops
at the first line that changes) and only then makes the temp file and the
encoder. If the second, writing pass changes nothing the file moved
between the reads and is held (errRolloutHeld), as a stat change already
was. The edit is built afresh per pass, so per-file state (portRollout's
"ours") starts over. Lines are read with ReadSlice into a 64KB buffer; a
longer line is gathered whole. The encoder is closed on every early
return now; it was left open before.
Run on mote (macOS 27.2), 10 rollouts of 4MB each with nothing to change:
- plain: 686ms -> 106ms per pass, 46MB -> 0.66MB allocated,
232k -> 120 allocations.
- zst: 127ms -> 120ms per pass, 237MB -> 0.67MB allocated; the process's
CPU (with the same setup) 2.55s -> 1.43s, sys 0.6s -> 0.17s.
- TestPortCodexRolloutsOnlyReadsWhatHasNothingToChange, plain and zst,
with a 300KB line: nothing written out and the bytes the same; then
with one line to change it is rewritten, the long line kept. With the
reading pass taken out by hand it fails: "1 rollouts written out for
nothing".
- TestPortCodex* and TestPortLine*: ok.
- go test ./internal/sessions ./internal/agent: ok (36.7s, 228.9s).
- GOOS=windows and GOOS=linux go vet of both: ok. gofmt -l: clean.
Not tried: a real ~/.codex/sessions pass in the app; the numbers are the
benchmark's, not a sample of the reporter's machine.