ac4af582301a4e8a29245dbba767eba31334ddf9
1
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
68133754a3 |
gateway: a request's body is read in place, not copied by every reader (yg6888 on X: ~1 GB)
yg6888 on X: "magpie 占用内存有点高 随时1个g", with a group of 29
accounts and three agents sending at once.
Cause: each reader of a request decodes its turns into json.RawMessage
values (the images walk, the conversation id, the prompt, the edits, the
chat parse), and decodeJSON copied each value it filled, as
encoding/json does. A 1.5 MB conversation allocated ~42.8 MB on its way
through a group. Three agents at once kept the heap in the hundreds of
MB, and the GC ran behind it.
Fix:
- readRequestBody holds the body (holdBody) until the request's context
ends. A RawMessage decodeJSON fills from inside a held body shares the
body's bytes, capped so an append can't write into it. The body must
be at most 16x the value (heldShareMost), so a small value kept past
its request (a cache entry, a signature) never pins a whole body.
Anything not held is still copied: a stream's buffer, a scanner line,
a body built and changed in place. The list is bounded at 64 bodies
(heldBodiesMost) and each leaves it through context.AfterFunc.
- The ratio leaves a long conversation's many small turns copied, so
the two readers that run on every request take them in place:
- conversationID takes the turns with elementsOf, slices of the
body.
- walkImages, which runs several times a request, first scans the
turns without decoding (anyImageBlock/anyImageIn) and returns at
once when there is no image. Keys and kinds are compared with
jsonStringIs, so an escaped "type", a duplicate key, a tool
result, a Responses function_call_output and Gemini's inlineData,
fileData and functionResponse still count.
Numbers: sandbox gateway, fresh process each run, 150 requests of a
1.5 MB conversation, 3 at once through a group, machine load ~250:
peak RSS HeapSys GCs allocated bytes.Clone
before 273 MB 222 MB 117 6416 MB 3635 MB
held bodies 233 MB 186 MB 74 3379 MB 1480 MB
+ id/image scan 223 MB 174 MB 64 2853 MB 927 MB
That is ~42.8 MB allocated per request before and ~19 MB after. What is
still copied comes from the translators (stringOrText, editMessages,
parseChat, responsesContent), unquoteJSON (~670 MB), io.ReadAll
(~480 MB) and rewriteModel (~225 MB). None of these is changed here.
Tests:
- TestRequestBodyRawValuesShareIt, TestDecodeJSONCopiesWhatIsNotHeld and
TestHeldBodiesBounded (fastjson_held_test.go).
- TestImageWalkReadsTextOnlyTurnsInPlace: a 938 KB text-only
conversation allocates under len/8 per walk plus id, and eight image
shapes are still found.
Broken by hand, each red:
- no share cap
- no ratio
- no bound
- no release
- pre-scan removed (8,780,204 B per iteration)
- tool_result skipped (case not found)
- id decoding each turn (1,044,859 B over the 117,265 limit)
- kind compared as raw bytes (the escaped case is not found)
No subsystem page covers decodeJSON.
Run record, the three commits on origin/main
|