gjson.GetBytes copies the value it finds. For "input" or "messages"
that is the conversation, so tailOf, the effort, deferred-tools and
refs-only checks, Cline's images, Codex's images, the compaction
lookups and the title digest each copied a 44 MiB Codex turn once
per request to look at it. getIn reads it in place (gjson.Get over
the body's bytes). None of the nine sites changes the body while a
result is held, and none keeps a string past the request: Codex's
image jobs read url.Str and write a new buffer, the compaction encs
are lookups, the title keeps a digest.
Together with the masked-record copy, a 44 MiB Responses turn sent
on as Chat allocates ~89 MB less (1,068 to 1,005 MB over the turn)
and runs fewer GCs. Peak live heap is unchanged (282 MB at GOGC=10
with gctrace, before and after): the body, the parsed request and
the outgoing Chat body are what is held at the peak. This is less
allocation and GC CPU, not a lower RSS peak.
TestTailOfReadsInPlace: a 4 MiB conversation's last 64 items come back
right, allocating under the tail plus a quarter of the body. Red with
GetBytes put back (5,480,048 bytes for a 1.3 MB tail of a 4.1 MB body).
Run (mote-run at aa8ded04e, both commits, on 56602426d):
- gofmt -l clean; go vet ./internal/gateway ./internal/maskrec for
linux, windows and darwin: ok.
- go test -count=1 -timeout 45m ./internal/gateway ./internal/maskrec:
ok (gateway 533s).
- The new test red with the old code put back (above), green with it.
- The allocation and peak numbers: a scratch test of one 44 MiB
Responses turn sent on as Chat (not committed), GOGC=10 gctrace for
the peak.
- Rebased onto 27bf14790 (lan.go changed in the gateway): the same vet
and go test -timeout 45m ./internal/gateway ./internal/maskrec again
at this head: ok (gateway 549s).
Reported-by: jihuayu