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, on56602426d): - 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 onto27bf14790(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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 ""
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user