ac4af582301a4e8a29245dbba767eba31334ddf9
1
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
bb5930b71b |
sessions: a trace poll reads a long line once, at its own size
TraceReader.Poll, which the OTel exporter runs each tick while session
export is on, read every appended line with bufio.ReadBytes through a
4 KiB reader. Every line was a copy of its own, and a line longer than
4 KiB (a function output, a pasted image in a Codex rollout) was gathered
in 4 KiB clones and then copied whole: about twice the line.
- Lines are read through the pooled line reader every other session
reader uses (scanBuffer's size, 16 KiB to 1 MiB), with ReadSlice. A
line the buffer holds is handed to the cursor in it; the cursor's
parse copies what it keeps (encoding/json copies into RawMessage and
strings), so nothing outlives the read.
- A longer line is gathered into one buffer made at its length, read
ahead with lineLen as scanAt does, and reused for the next long line
of the same poll. It is dropped when the poll returns.
- A line with no newline yet (half-written) is left for the next poll,
as before. When the read ahead finds no end the gathering stops at
once instead of growing the line first. A file rewritten between the
read ahead and the read only grows the buffer again, never cuts a
line.
Measured (TotalAlloc of one cold Poll, a fresh TraceReader over one
Codex rollout in a sandbox CODEX_HOME, 5 runs averaged, 3 runs each,
all equal to within 0.1%):
rollout bodies before after
3 turns, images of 1/3/5 MiB (9.5 MB) off 38.2 MB 28.7 MB
on 57.3 MB 47.6 MB
300 turns, 20 KB images (9.9 MB, 760 spans) off 35.6 MB 20.8 MB
on 60.0 MB 45.1 MB
pprof (memprofilerate 1) puts ReadBytes at 18.7 MB per poll before and
the new read at 9.2 MB after, exactly the long lines read once. The rest
is the cursor's own parse (RawMessage copies of payload and content),
which this change leaves alone.
Tests (internal/sessions/trace_lines_test.go):
- TestNextTraceLineReadsWhatReadBytesRead: lines and end offset equal
ReadBytes' for long lines (1/3/5 MiB, past the 1 MiB buffer), a line
of the buffer's size, empty lines, CRLF, a short and a long unended
last line, no line yet, an empty file; from 0 and from the first
line's end.
- TestNextTraceLineWaitsForALongLineToEnd: a 3 MiB line without its
newline is not read, and is read whole once it has one.
- TestTracePollOfLongLinesIsUnchanged: the spans Poll exports (bodies
off and on) from a rollout with 3 and 2 MiB images, written in three
parts that end inside long lines, are reflect.DeepEqual to those a
cursor fed the whole file's ReadBytes lines exports.
- TestTracePollReadsALongLineOnce: a poll of a 6 MiB reasoning line
allocates at most 2.5 times the line (12.6-13.7 MB over -count=20).
With Poll's ReadBytes loop put back by hand it is red 3 of 3: "a poll
of a 6291589-byte line allocated 19025024 bytes, want at most
15728972".
Run at this commit's tree on origin/main
|