Skip to content

Serialize a stream item straight into its notification, in a buffer each thread keeps - #71

Merged
jeff377 merged 2 commits into
mainfrom
claude/stream-item-copies
Dec 2, 2026
Merged

jeff377 merged 2 commits into
mainfrom
claude/stream-item-copies

Conversation

@jeff377

@jeff377 jeff377 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A health check measured each item of a streamed call being copied on the server before it reaches the sink. This PR removes the two copies that do not depend on the public IJsonRpcStreamSink contract.

  • Direct serialization. JsonRpcStreamItem gains Create(JsonRpcId, object?, JsonTypeInfo), which serializes the item into the notification's params directly. JsonRpcStreamState uses it for plain items, instead of JsonSerializer.SerializeToElement followed by writing that element again. Encrypted items still go through Create(JsonRpcId, JsonElement), since PayloadFilter's item serializer returns a JsonElement.
  • A buffer each thread keeps. Both overloads write into an ArrayBufferWriter and a Utf8JsonWriter held per thread, instead of a new buffer grown from its default size for every item. The write is synchronous. A converter that creates an item while its own item is written gets a fresh buffer, every use resets the writer, and a buffer that grew past 64 KiB for a large item is dropped afterwards, so a thread does not hold it for its lifetime.

Measured with a benchmark through JsonRpcDispatcher.DispatchStreamingAsync and a sink that discards, three rounds each:

per item
before (main) 940–1180 ns, 1168 B
after 750–1130 ns, 488 B

The health check suggested JsonElement.ParseValue instead of JsonDocument.Parse + Clone. Measured here, it allocated more (1296 B vs 1112 B with a fresh buffer) and was slower with a reused one, so it is not used.

Left as is:

  • The sink still receives a JsonRpcRequest that EventStreamSink serializes, because that is the public sink contract.
  • The data: copy (16 ns, 136 B) would need three writes per event, and a half-written event on failure.

Downstream

  • The bytes on the wire do not change: the existing wire, streaming and SSE tests pass unchanged, and a new test compares the new overload with the JsonElement one byte for byte.
  • Public API: one overload added to JsonRpcStreamItem (PublicAPI.Unshipped.txt). The class is unreleased (Define the rpc.item notification once, in JsonRpcStreamItem #70), so nothing shipped changes.
  • CHANGELOG (both languages) records the overload and the measured change.

Verification

  • dotnet build 0 warnings; 1518 tests pass; check-md-links.sh clean.
  • New tests cover the new overload, a converter creating an item while its own item is written, an item that fails mid-write followed by another, and 2000 items created on many threads at once. Others compare escaped text (non-ASCII, <&+>, U+2028) with what a fresh writer writes, and check that a buffer grown past 64 KiB is dropped. Each fails under a mutant: no nesting guard, no writer reset, one buffer for every thread, buffer not emptied, large buffer kept.
  • Adversarial review: nothing blocking. It compared 342 inputs before and after (encoders, WriteIndented, WriteAsString, both overloads, the encrypted path): identical bytes on the wire; reproduced 1168 → 488 B; 2000 Task.Run items spread over 18 threads. Its P2 (a thread kept a 4 MB buffer after a 4 MB item; measured 64 MB over 16 threads after idling) and wording findings are fixed in the second commit.

…ach thread keeps

Each plain item of a streaming method was serialized into a JsonElement of its own, then written into a
new buffer with the request id, grown from its default size, and parsed back with JsonDocument and a
clone. JsonRpcStreamItem gains an overload that takes the item with its JsonTypeInfo and serializes it
into the notification directly, and both overloads write into a buffer and a writer each thread keeps.
A converter that creates an item while its own item is written gets a buffer of its own, and a writer
left in the middle of an item by an exception is reset before the next. Through DispatchStreamingAsync an
item allocates 488 bytes instead of 1168 and takes about a fifth less time; the bytes on the wire do
not change. JsonElement.ParseValue, which the health check suggested, was measured too and allocated
more than the document and clone here.
…he review's findings

A buffer that grew for a large item stayed with its thread for as long as the thread lived, where the
buffer it replaced went back to the pool. It is now dropped once it has grown past 64 KiB. The new
overload lists its exceptions, the comments say why every use resets the writer, the CHANGELOG says the
numbers are those of a small item, and a test compares escaped text with what a fresh writer writes.
@jeff377
jeff377 enabled auto-merge (squash) December 2, 2026 02:24
@jeff377
jeff377 merged commit 829b28a into main Dec 2, 2026
4 checks passed
@jeff377
jeff377 deleted the claude/stream-item-copies branch December 2, 2026 02:25
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant