From 56d26f0bba653237fef61cff1798842f06f79947 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 28 Oct 2026 19:59:55 +0800 Subject: [PATCH] gateway: a conversation's list is looked at where it sits in the body, not copied (#4004) 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 --- internal/gateway/anthropic_shapes.go | 6 +++--- internal/gateway/cline_pin.go | 2 +- internal/gateway/codex_images.go | 2 +- internal/gateway/fastjson.go | 12 ++++++++++++ internal/gateway/native_compaction.go | 4 ++-- internal/gateway/title_link.go | 2 +- internal/gateway/tool_loop.go | 2 +- internal/gateway/tool_loop_test.go | 21 +++++++++++++++++++++ 8 files changed, 42 insertions(+), 9 deletions(-) diff --git a/internal/gateway/anthropic_shapes.go b/internal/gateway/anthropic_shapes.go index 7e3b20e57..8cc6749e6 100644 --- a/internal/gateway/anthropic_shapes.go +++ b/internal/gateway/anthropic_shapes.go @@ -191,7 +191,7 @@ func hasTurnEffort(body []byte) bool { return false } found := false - gjson.GetBytes(body, "messages").ForEach(func(_, m gjson.Result) bool { + getIn(body, "messages").ForEach(func(_, m gjson.Result) bool { found = m.Get("output_config").Exists() return !found }) @@ -325,7 +325,7 @@ func hasDeferredTools(body []byte) bool { return false } found := false - gjson.GetBytes(body, "messages").ForEach(func(_, m gjson.Result) bool { + getIn(body, "messages").ForEach(func(_, m gjson.Result) bool { m.Get("content").ForEach(func(_, b gjson.Result) bool { found = deferredBlock(b.Get("type").String()) return !found @@ -449,7 +449,7 @@ func hasRefsOnlyResult(body []byte) bool { return false } found := false - gjson.GetBytes(body, "messages").ForEach(func(_, m gjson.Result) bool { + getIn(body, "messages").ForEach(func(_, m gjson.Result) bool { m.Get("content").ForEach(func(_, b gjson.Result) bool { found = b.Get("type").String() == "tool_result" && refsOnly(b.Get("content")) return !found diff --git a/internal/gateway/cline_pin.go b/internal/gateway/cline_pin.go index 6a528b3b1..a05969dd3 100644 --- a/internal/gateway/cline_pin.go +++ b/internal/gateway/cline_pin.go @@ -57,7 +57,7 @@ func clinePin(p provider.Provider, to provider.Protocol, body []byte) []byte { // chatImages is whether a Chat body carries an image part func chatImages(body []byte) bool { found := false - gjson.GetBytes(body, "messages").ForEach(func(_, m gjson.Result) bool { + getIn(body, "messages").ForEach(func(_, m gjson.Result) bool { m.Get("content").ForEach(func(_, part gjson.Result) bool { if t := part.Get("type").String(); t == "image_url" || t == "image" || t == "input_image" { found = true diff --git a/internal/gateway/codex_images.go b/internal/gateway/codex_images.go index fd022a975..8d7d638ac 100644 --- a/internal/gateway/codex_images.go +++ b/internal/gateway/codex_images.go @@ -50,7 +50,7 @@ func codexImagesFit(p provider.Provider, to provider.Protocol, body []byte) []by // every image: in a Computer Use turn of 160 screenshots, gigabytes of // JSON read for one request (fflake33, Discord). var jobs []fitJob - gjson.GetBytes(body, "input").ForEach(func(_, item gjson.Result) bool { + getIn(body, "input").ForEach(func(_, item gjson.Result) bool { if !strings.Contains(item.Raw, "input_image") { return true } diff --git a/internal/gateway/fastjson.go b/internal/gateway/fastjson.go index 01ca843cb..28e29b504 100644 --- a/internal/gateway/fastjson.go +++ b/internal/gateway/fastjson.go @@ -16,6 +16,8 @@ import ( "unicode/utf16" "unicode/utf8" "unsafe" + + "github.com/tidwall/gjson" ) // decodeJSON is json.Unmarshal(data, dst), made for a request body: one @@ -1427,6 +1429,16 @@ func appendHTMLEscaped(dst, s []byte) []byte { return append(dst, s[start:]...) } +// getIn is gjson.GetBytes read in place: what it finds points into body +// rather than into a copy. GetBytes copies the value it finds, and for a +// conversation's list that is the conversation, 45 MiB of a Codex turn +// for one look at its items (#4004). A string kept from the result keeps +// all of body: clone one that outlives the request, and never change body +// while one is held. +func getIn(body []byte, path string) gjson.Result { + return gjson.Get(unsafe.String(unsafe.SliceData(body), len(body)), path) +} + // marshalBody is json.Marshal of a request body being built, byte for // byte, its lists (the messages, the input items) written an item at a // time into a buffer of the size they come to. json.Marshal writes the diff --git a/internal/gateway/native_compaction.go b/internal/gateway/native_compaction.go index 5f3bb968f..f1f671f22 100644 --- a/internal/gateway/native_compaction.go +++ b/internal/gateway/native_compaction.go @@ -48,7 +48,7 @@ func compactionTriggerIn(body []byte) bool { if !bytes.Contains(body, []byte(`"compaction_trigger"`)) { return false } - for _, it := range gjson.GetBytes(body, "input").Array() { + for _, it := range getIn(body, "input").Array() { if it.Get("type").Str == "compaction_trigger" { return true } @@ -64,7 +64,7 @@ func sealedCompactions(body []byte) []string { return nil } var out []string - for _, it := range gjson.GetBytes(body, "input").Array() { + for _, it := range getIn(body, "input").Array() { if !slices.Contains(compactionKinds, it.Get("type").Str) { continue } diff --git a/internal/gateway/title_link.go b/internal/gateway/title_link.go index 0c4bdc800..62f588d28 100644 --- a/internal/gateway/title_link.go +++ b/internal/gateway/title_link.go @@ -112,7 +112,7 @@ func titleUserText(content gjson.Result, title bool) string { func titlePromptDigest(body []byte, title bool) string { var digest string i := 0 - input := gjson.GetBytes(body, "input") + input := getIn(body, "input") if !input.IsArray() { return "" } diff --git a/internal/gateway/tool_loop.go b/internal/gateway/tool_loop.go index dd9951e63..e29e0b47a 100644 --- a/internal/gateway/tool_loop.go +++ b/internal/gateway/tool_loop.go @@ -98,7 +98,7 @@ const tailItems = 8 * toolLoopRepeats // took ~30ms and 20MB a turn of 1.5MB; gjson finds the items without // decoding them. A body whose conversation isn't such a list is as it came. func tailOf(from provider.Protocol, body []byte) []byte { - list := gjson.GetBytes(body, conversationKey[from]) + list := getIn(body, conversationKey[from]) if !list.IsArray() || list.Index <= 0 { return body } diff --git a/internal/gateway/tool_loop_test.go b/internal/gateway/tool_loop_test.go index 34654e8a2..b6f47a991 100644 --- a/internal/gateway/tool_loop_test.go +++ b/internal/gateway/tool_loop_test.go @@ -329,3 +329,24 @@ func BenchmarkToolLoopCheck(b *testing.B) { } } } + +// The conversation's tail is read where it sits in the body: GetBytes +// copied the whole input list to find the last items, 45 MiB of a Codex +// turn on every request (#4004). +func TestTailOfReadsInPlace(t *testing.T) { + item := `{"type":"function_call_output","call_id":"c%d","output":"` + strings.Repeat("o", 20<<10) + `"}` + items := make([]string, 200) + for i := range items { + items[i] = fmt.Sprintf(item, i) + } + body := []byte(`{"model":"m","input":[` + strings.Join(items, ",") + `],"stream":true}`) + var tail []byte + n := allocated(func() { tail = tailOf(provider.Responses, body) }) + want := `{"model":"m","input":[` + strings.Join(items[len(items)-tailItems:], ",") + `],"stream":true}` + if string(tail) != want { + t.Fatalf("tail is %.200s…", tail) + } + if n > uint64(len(want))+uint64(len(body))/4 { + t.Fatalf("%d bytes allocated for a %d byte tail of a %d byte body", n, len(want), len(body)) + } +}