Skip to content

[Fusion] Complete subgraph responses at the end of the JSON root - #131

Closed
michaelstaib wants to merge 1 commit into
mainfrom
perf/complete-at-json-root-end
Closed

michaelstaib wants to merge 1 commit into
mainfrom
perf/complete-at-json-root-end

Conversation

@michaelstaib

@michaelstaib michaelstaib commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Subgraphs send chunked responses, and the gateway waited for the final empty chunk before parsing, even when the JSON was already complete, which cost about 0.23 ms per request on the critical path. The gateway now notices when the JSON root has ended and stops reading there, and the HTTP handler drains the rest when the response is disposed, so the connection stays reusable. Finding the end of the JSON uses a vectorized scan that takes about 12 us for the 87 KB benchmark response. The scan and the drain raise CPU per request by about 0.03 ms in exchange for the lower latency.

Metric Before Optimization After Optimization
Constant-latency req/s, AOT 3,353 3,402 (+1.5%)
Constant-latency p50 / p95 ms, AOT 13.87 / 19.31 13.68 / 18.80
Constant-latency req/s, JIT 3,407 3,475 (+2.0%)
Constant-latency p50 / p95 ms, JIT 13.64 / 19.16 13.39 / 18.51
Constant req/s, AOT 4,768 4,784 (+0.3%)
Constant req/s, JIT 5,041 5,152 (+2.2%)
CPU ms/req, constant-latency, AOT 0.787 0.826 (+5.0%)
CPU ms/req, constant, AOT 0.774 0.784 (+1.4%)
Allocated KiB/req, constant-latency, JIT 25.7 25.5
RSS avg / max MB, constant-latency, AOT 97.4 / 100.0 98.7 / 101.5
RSS avg / max MB, constant, AOT 105.2 / 118.4 94.3 / 110.1
Idle RSS MB, AOT 50.3 50.6
Startup ms, AOT 77 75

Measured on the benchmark twin with the official 16.7.0 subgraphs and the official gateway settings (alias batching, inline scheduling and inline socket completions), before is upstream 592c379, after is this change on top of it, means of 2-3 interleaved rounds with 40 s warm-up and 45 s measured, responses byte-identical.

With chunked transfer encoding, a source schema writes the terminating chunk when
its response completes, often in a later segment than the body, and the gateway read
every chunked body to its end. The arena read now finds the end of the root object or
array as bytes arrive and stops once the root ended and only whitespace followed it in
the same read. Disposing the content stream lets the handler drain the rest of the body,
which keeps the connection reusable. The root end is found with 64-byte vector masks.
@michaelstaib

Copy link
Copy Markdown
Owner Author

Moved upstream to ChilliCream#10847, rebased on current main and with the Codex review fixes: early completion is limited to HTTP/1.x, the trailing-data test awaits the read directly, and long documents cover the vectorized scanner path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant