From d7ad3f2a3eea4fadc6f7131bf8b0957024892ba1 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 24 Oct 2026 21:27:02 +0800 Subject: [PATCH] gateway: input scanners read a request's items in place, not copied (#3383) Seven readers of a Responses request's input (the seal stripper and withoutKinds in affinity, hasSealedAgentMessage, joinToolRuns, orphanedToolOutputs, pairToolItems, withReasoningText) and the prompt builder decoded `input` into a []json.RawMessage. fillJSONIn clones each RawMessage under a sixteenth of the held body, so every item of a long Codex history was copied once per reader, per request. None of them keeps an item past its return (they marshal new output or assign into the slice), so inputList hands them elementsOf's capped subslices of the raw input instead. hasSealedAgentMessage reads an item's content the same way. Run at 83234fe92 + this change: - BenchmarkZZCodexStream (a 680 KB Codex request through the gateway, a probe not committed), -benchtime 30x -count 3: before 20,077,746 B/op 76,687 allocs/op; after 16,417,942 / 16,430,955 / 16,469,180 B/op, ~72,780 allocs/op (-18% bytes). - Real account, sandbox HOME with only the DeepSeek provider, through a recording proxy to api.deepseek.com: 5 Codex-UA /v1/responses requests each with a user message, reasoning, function_call, function_call_output and assistant message in the history, on the old and new binary. All 10 answered "ok"; the 5 bodies sent upstream are byte-identical between old and new. - gofmt -l clean; GOOS=linux and windows go vet -tags nogui ./internal/gateway ok. - go test -timeout 45m ./internal/gateway/: ok (667s). Rebased onto 1537817e0: build, both vets and go test -run 'Responses|Affin|Seal|Prompt|Tool|Codex|Reasoning|Orphan|DecodeJSON' ./internal/gateway/ ok (64s). Not tried: the full ./... suite at this head (it runs after the push). --- internal/gateway/affinity.go | 8 ++++---- internal/gateway/codex_backend.go | 10 +++++----- internal/gateway/fastjson.go | 20 ++++++++++++++++++++ internal/gateway/prompt.go | 3 +-- internal/gateway/responses.go | 16 ++++++++-------- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/internal/gateway/affinity.go b/internal/gateway/affinity.go index a30da86dd..d2ba0a859 100644 --- a/internal/gateway/affinity.go +++ b/internal/gateway/affinity.go @@ -801,8 +801,8 @@ func withoutRefused(key, who string, body []byte) ([]byte, bool) { if decodeJSON(body, &q) != nil { return nil, false } - var items []json.RawMessage - if decodeJSON(q["input"], &items) != nil { + items, isList := inputList(q["input"]) + if !isList { return nil, false } kept := items[:0:0] @@ -855,8 +855,8 @@ func withoutKinds(body []byte, kinds ...string) ([]byte, bool) { if decodeJSON(body, &q) != nil { return nil, false } - var items []json.RawMessage - if decodeJSON(q["input"], &items) != nil { + items, isList := inputList(q["input"]) + if !isList { return nil, false } kept := items[:0:0] diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go index 50b86aaf9..8cf8be00c 100644 --- a/internal/gateway/codex_backend.go +++ b/internal/gateway/codex_backend.go @@ -504,19 +504,19 @@ func hasSealedAgentMessage(body []byte) bool { if decodeJSON(body, &request) != nil { return false } - var items []json.RawMessage - if decodeJSON(request["input"], &items) != nil { + items, isList := inputList(request["input"]) + if !isList { return false } for _, raw := range items { var item struct { - Type string `json:"type"` - Content []json.RawMessage `json:"content"` + Type string `json:"type"` + Content json.RawMessage `json:"content"` } if decodeJSON(raw, &item) != nil || item.Type != "agent_message" { continue } - for _, content := range item.Content { + for _, content := range elementsOf(trimJSON(item.Content)) { var part struct { Type string `json:"type"` EncryptedContent string `json:"encrypted_content"` diff --git a/internal/gateway/fastjson.go b/internal/gateway/fastjson.go index 8523f1054..1b0c6e5d1 100644 --- a/internal/gateway/fastjson.go +++ b/internal/gateway/fastjson.go @@ -556,6 +556,26 @@ func elementsOf(raw []byte) []json.RawMessage { return out } +// inputList is a request's input list, raw from a decoded body (so valid +// JSON), for a reader that looks at its items and keeps none past its +// return: elementsOf, so no item is copied. Decoding it as a +// []json.RawMessage copied every item, since each is under a sixteenth of +// the body (heldShareMost): 3.7 of the 20 MB a 680 KB Codex request +// allocated went to such copies (#3383). ok is what that decode's error +// was: false when the input is missing or neither a list nor null. +func inputList(raw json.RawMessage) ([]json.RawMessage, bool) { + raw = trimJSON(raw) + switch { + case len(raw) == 0: + return nil, false + case raw[0] == '[': + return elementsOf(raw), true + case string(raw) == "null": + return nil, true + } + return nil, false +} + // skipJSONString is the end of the valid JSON string at i. func skipJSONString(data []byte, i int) int { i++ diff --git a/internal/gateway/prompt.go b/internal/gateway/prompt.go index 5796af3bd..51147e56d 100644 --- a/internal/gateway/prompt.go +++ b/internal/gateway/prompt.go @@ -830,8 +830,7 @@ func (b *promptBuilder) responses(body []byte) bool { b.userText(s) return true } - var items []json.RawMessage - _ = decodeJSON(req.Input, &items) + items, _ := inputList(req.Input) for _, it := range items { b.responsesItem(it) } diff --git a/internal/gateway/responses.go b/internal/gateway/responses.go index 64f586701..94d98d4a9 100644 --- a/internal/gateway/responses.go +++ b/internal/gateway/responses.go @@ -597,8 +597,8 @@ func joinToolRuns(body []byte) []byte { if decodeJSON(body, &q) != nil { return body } - var input []json.RawMessage - if decodeJSON(q["input"], &input) != nil { + input, isList := inputList(q["input"]) + if !isList { return body } type item struct { @@ -699,8 +699,8 @@ func orphanedToolOutputs(body []byte) []byte { if decodeJSON(body, &q) != nil { return body } - var input []json.RawMessage - if decodeJSON(q["input"], &input) != nil { + input, isList := inputList(q["input"]) + if !isList { return body } changed := false @@ -793,8 +793,8 @@ func pairToolItems(body []byte) []byte { if decodeJSON(body, &q) != nil { return body } - var input []json.RawMessage - if decodeJSON(q["input"], &input) != nil { + input, isList := inputList(q["input"]) + if !isList { return body } type item struct { @@ -917,8 +917,8 @@ func withReasoningText(body []byte) []byte { if decodeJSON(body, &q) != nil { return body } - var items []json.RawMessage - if decodeJSON(q["input"], &items) != nil { + items, isList := inputList(q["input"]) + if !isList { return body } changed := false