Skip to content

[Fusion] Write minimized results without per-token JsonWriter calls - #10617

Merged
michaelstaib merged 6 commits into
mainfrom
tte/fusion-minimized-result-writer
Oct 22, 2026
Merged

michaelstaib merged 6 commits into
mainfrom
tte/fusion-minimized-result-writer

Conversation

@tobias-tengler

Copy link
Copy Markdown
Member

The composite result is serialized through one JsonWriter call per token, even when the output is minimized. Every property name goes through an escape check, length validation and its own GetSpan/Advance pair, although response names never need escaping.

This PR writes minimized results (no indentation, no null omission, default or relaxed encoder) straight into the response buffer through a cached span. Property names come from a token cached on the selection that already holds the quotes and the colon, and source leaves and source-owned JSON scalars are copied as raw bytes. Indented output, null omission and custom encoders keep the existing JsonWriter path. The output is byte-identical, and new tests compare both paths on composed gateway results.

Method Version Mean Ratio Allocated Alloc Ratio
Format 87 KB result main 33.30 μs 1.00 96 B 1.00
Format 87 KB result PR 21.91 μs 0.66 96 B 1.00

k6 constant scenario, A B B A per gateway (3 × 30 s runs per invocation; values are the mean of the per-invocation medians). The first composite schema invocation of the PR was discarded: in all three of its runs the gateway, subgraph and k6 cores all idled at about half their usual load while latency doubled, which points to an external stall, so the composite PR column is the second invocation only.

Gateway Version RPS CPU per request p95 p99
Composite schema (net10.0) main 4,417 589 μs 19.48 ms 25.46 ms
Composite schema (net10.0) PR 4,575 573 μs (−2.8%) 18.89 ms 24.85 ms
Apollo Federation (net11.0) main 4,123 563 μs 19.82 ms 25.41 ms
Apollo Federation (net11.0) PR 4,188 547 μs (−2.8%) 19.81 ms 25.70 ms

Gateway CPU per request drops by 2.8% on both gateways, well beyond the run-to-run noise measured on main (CPU per request CV about 0.5% for the composite schema gateway and 0.2% for the Apollo Federation gateway). The RPS gains (+158 and +65) are not significant on their own given that noise (σ about 65 RPS for the composite schema gateway, 40 RPS for the Apollo Federation gateway), and latency is unchanged within noise.

The composite result is written straight into the response buffer when the
writer neither indents nor omits nulls. Property names come from a token cached
on the selection that already holds the quotes and the colon, so no name is
checked for escaping, and every token is placed through one cached span.
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Patch coverage

91.5% of changed lines covered (367/401)

File Covered Changed Patch %
…/Fusion/src/Fusion.Execution/Text/Json/ThrowHelper.cs 0 1 0.0% 🔴
…/src/Fusion.Execution/Text/Json/MinimizedJsonEmitter.cs 107 129 82.9% 🟡
…/Text/Json/CompositeResultDocument.WriteTo.cs 137 144 95.1% 🟡
…/Fusion.Execution/Text/Json/SourceResultDocument.WriteTo.cs 111 115 96.5% 🟡
…/Fusion/src/Fusion.Execution/Execution/Nodes/Selection.cs 3 3 100.0% 🟢
…/Execution/Nodes/SourceResponseNameMapping.cs 2 2 100.0% 🟢
…/src/Fusion.Execution/Text/Json/CompositeResultDocument.cs 1 1 100.0% 🟢
…/Fusion/src/Fusion.Execution/Text/Utf8StringCache.cs 5 5 100.0% 🟢
src/HotChocolate/Json/src/Json/JsonWriter.cs 1 1 100.0% 🟢
Uncovered changed lines (JSON)
{
  "sha": "9b0ec888e8188e0e2c908c03d6c765248e17dc9c",
  "files": [
    { "path": "src/HotChocolate/Fusion/src/Fusion.Execution/Text/Json/ThrowHelper.cs", "ranges": [[45, 45]] },
    { "path": "src/HotChocolate/Fusion/src/Fusion.Execution/Text/Json/MinimizedJsonEmitter.cs", "ranges": [[116, 116], [120, 122], [124, 127], [129, 135], [143, 147], [182, 183]] },
    { "path": "src/HotChocolate/Fusion/src/Fusion.Execution/Text/Json/CompositeResultDocument.WriteTo.cs", "ranges": [[335, 337], [341, 341], [362, 364]] },
    { "path": "src/HotChocolate/Fusion/src/Fusion.Execution/Text/Json/SourceResultDocument.WriteTo.cs", "ranges": [[152, 152], [165, 166], [250, 250]] }
  ]
}

Project coverage: 59.2% (314259/530629 lines)

@michaelstaib
michaelstaib marked this pull request as ready for review October 22, 2026 02:28
Copilot AI balanced review requested due to automatic review settings October 22, 2026 02:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

3 participants