perf(frontend): serialize Anthropic stream deltas without a JSON value tree #18813
All Vouch review confidence checks passed
Residual risk 0.27 = Change Risk (0.50) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 441 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.364
Shallow Review Depth
shallow_review_depth
0.344
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #18813"
general
weight: 0.35
luckyq
Overview
On
/v1/messagesstreaming, eachcontent_block_deltaandpingevent carries ausageobject (#12473). To add it,event_json_with_usagebuilds 2serde_json::Valuetrees, inserts 2 keys, and writes the tree to a string. This occurs for each output token.This PR writes the same JSON directly from a borrowed wrapper struct. The bytes on the wire do not change. The converter takes 66% less time for each text token.
Key Changes
Core logic: 29 lines added and 19 removed, in one file:
stream_converter.rs.event_json_with_usagereturns the JSON string. For an event that carries usage, it serializes a wrapper struct with#[serde(flatten)]. Other events do not change.total_tokensuses the same saturating sum as before.publishedusage logic from #18377 does not change.Benchmark: the new Criterion bench
anthropic_stream_deltas, with a[[bench]]entry inlib/llm/Cargo.tomland a section inlib/llm/benches/README.md.Why the bytes do not change:
dynamo-llmalways enablesserde_json/preserve_orderthrough its required dependencies. Thus, both paths write the keys in the same order. Both paths skip cache fields that areNone.Validation
Wire bytes. The new test
test_usage_triple_bytes_match_value_insertioncompares the new string with the old path, byte for byte. It covers text, thinking, signature, input-JSON,pingandcontent_block_stopevents, each with 3 usage shapes. The existing snapshotanthropic_http_replay::streaming_text_baselinepasses with no change. On an earlier base (cde22ae519), I movedtotal_tokensto a different position. Both tests failed.Cost. The bench
anthropic_stream_deltasrunsAnthropicStreamConverter::append_chunk_eventsover 256 text chunks. Each delta carries theusagetriple. The bench uses only the public API, so the same file also runs onmain. I ran it onmain(9f1f0829de) with--save-baseline main, then on this PR with--baseline main, on the same machine.| 256 text chunks |
main| This PR | Change || --- | --- | --- | --- |
| Linux x86_64 (2 vCPU Xeon 2.6 GHz, rustc 1.96.1) | 321.4 µs | 109.5 µs | -66.5% |
| macOS arm64 (Apple Silicon) | 314.5 µs | 101.2 µs | -67.8% |
The Linux machine has 2 GB of RAM. Thus, for both Linux builds I set
CARGO_PROFILE_BENCH_CODEGEN_UNITS=16,CARGO_PROFILE_BENCH_LTO=falseandCARGO_PROFILE_BENCH_DEBUG=0. The macOS builds use the repository bench profile.This saves about 0.83 µs of frontend CPU for each output token. I did not do a mocker A/B test. The saving is smaller than the noise of an end-to-end run. The benchmark measures the changed step directly.
I ran the tests on macOS arm64 with
-p dynamo-llm --no-default-features, after I mergedmain(9f1f0829de). The 56stream_convertertests pass. I did not run the Linux CI matrix or the default features.Issues and Ongoing Work
pingto the events that carry usage. This PR keeps that rule.main.<details>
<summary>Pull Request Checklist</summary>
/pr-checklistskill.</details>