Repository navigation
Reuse RenderBatchWriter across render batches in RemoteRenderer - #70246
Conversation
RemoteRenderer allocated a new ArrayBuilderMemoryStream, RenderBatchWriter, BinaryWriter, string ArrayBuilder and dedup Dictionary for every render batch. Keep one writer per renderer and point its stream at each batch's own output buffer. RenderBatchWriter.Write now resets its string table in a finally block, so it holds no strings or pooled buffers between batches, including after a failed write. Dictionaries that grew past 128 entries are replaced rather than retained. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the render-batch writer and stream reuse changes.
No open issues 🎉
What changed in this PR
Reduces per-batch allocations in Blazor Server by reusing a RenderBatchWriter and stream within each RemoteRenderer, while preserving separate output buffers until client acknowledgment.
Changes:
- Reuses the writer and redirects its stream to each batch’s output buffer.
- Resets string-table state after every write, including failures, and replaces oversized deduplication dictionaries.
- Adds regression tests for writer reuse after successful and failed writes.
| File | Description |
|---|---|
src/Components/Shared/src/RenderBatchWriter.cs |
Clears per-batch string state and limits retained dictionary size. |
src/Components/Shared/src/ArrayBuilderMemoryStream.cs |
Allows switching the stream’s backing buffer. |
src/Components/Server/test/Circuits/RenderBatchWriterTest.cs |
Tests string-table isolation when reusing a writer. |
src/Components/Server/src/Circuits/RemoteRenderer.cs |
Retains a writer and stream across render batches. |
Review effort: Balanced
|
Could we improve |
|
/review |
Co-authored-by: Copilot <[email protected]>
|
Thanks @pavelsavara, good call. Done in 7e0ca87:
BenchmarkDotNet, 3 interleaved rounds:
*The real WASM path needs browser JS interop, so the benchmark reproduces its serialize + Server/WebView/WebAssembly Components tests pass (396/16/56). Details are in the PR description. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <[email protected]>
|
Retriggered CI with an empty commit (98dbe8d; the diff is unchanged, same 6 files). Build Analysis for 7e0ca87 reported only known-flaky failures, none on a path this PR changes:
Locally, Components.Server (396), Components.WebView (16) and Components.WebAssembly (56) tests pass. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <[email protected]>
|
Retriggered CI again with an empty commit (d0e7924; the diff is unchanged, same 6 files). On 98dbe8d, all other checks passed, including
Neither test's path changes in this PR: external-navigation 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <[email protected]>
|
Retriggered CI with an empty commit (c60432a; the diff is unchanged, same 6 files). On d0e7924, every check passed except one test on the CoreCLR E2E leg:
If this test fails again, it should clear once #70243 merges. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Co-authored-by: Copilot <[email protected]>
|
Retriggered CI with an empty commit (40b9298; the diff is unchanged, same 6 files). On c60432a, every check passed, including 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
✅ No documentation PR was created. Confidence that documentation is needed: 3% No docs needed (confident): internal refactor reusing RenderBatchWriter per RemoteRenderer, with tests. No signals detected. Documentation surfaces considered
|
Reuse RenderBatchWriter across render batches in RemoteRenderer
Stop allocating a new batch writer and stream for every Blazor Server render batch.
Description
Follow-up to @pavelsavara's suggestion on #70125.
RemoteRenderer.UpdateDisplayAsynccreated a newArrayBuilderMemoryStream,RenderBatchWriter,BinaryWriter, stringArrayBuilderand dedupDictionaryfor every render batch. This PR keeps one writer per renderer and points its stream at each batch's own output buffer.On the real
RemoteRenderer.UpdateDisplayAsync+ ack path, this halves allocations for a small interactive update (776 → 392 B) and cuts time by about 8%.Why this is safe
ArrayBuilder<byte>output buffer. That buffer is kept in the unacknowledged-batch queue until the client acks it, so it is never shared or reused.UpdateDisplayAsyncruns on the renderer's dispatcher, andRendererguards it with_isBatchInProgress, so a renderer never writes two batches at once. The writer belongs to one renderer, so nothing is shared across circuits.RenderBatchWriter.Writenow resets its string table in afinally, including when a write throws. Between batches the writer holds no strings, and its stringArrayBuilderhas returned its array to the pool. So the renderer never needs to dispose it, and a dispose racing a render can't return a buffer that is still in use.Tests: two new
RenderBatchWriterTestcases reuse one writer for two batches, one of them after a failed write. Both fail onmain, where the second batch's string table still holds the first batch's strings, and pass here. Server (396), WebView (16) and WebAssembly (56) unit tests pass locally.Benchmark
BenchmarkDotNet, in-process. The benchmark drives the real path:
RemoteRenderer.UpdateDisplayAsync(batch)followed byOnRenderCompletedAsync(batchId), with an offline client so no SignalR send. The batches are the same as in #70125'sRenderBatchWriterBenchmark(a 5-edit counter-style update, and a 200-row table initial render). I ran 3 interleaved rounds (main, PR, main, PR, main, PR) on a 2-core Linux VM.Benchmark harness (throwaway; not part of this PR)
A console project inside the repo with
AssemblyName=Microsoft.AspNetCore.Components.Server.Tests(for internals access), referencing Server and SignalR.Core, and usingInProcessEmitToolchain+MemoryDiagnoser:This is independent of #70125 (which changes how frames and edits are encoded); both touch
RenderBatchWriter.csbut in different methods.Follow-up:
IpcSender(WebView) andWebAssemblyRenderer(7e0ca87)At reviewer request, the same reuse is applied to the other two
RenderBatchWritercall sites:IpcSender.ApplyRenderBatch(WebView): the writer and stream are cached in a private field. Each batch takes them withInterlocked.Exchangeand puts them back infinally, so two concurrent callers can never share them; if the cache is empty, a new writer is created. The per-batchArrayBuilder<byte>is now also disposed (returned to the pool) infinally, once the base64 message string has copied its contents. Onmainit was never disposed.WebAssemblyRenderer.UpdateDisplayOutOfProcess: the writer and stream are cached in fields (the renderer is single-threaded) and retargeted at each batch's ownArrayBuilder. The existingfinally { arrayBuilder.Dispose(); }is unchanged.WebView
IpcSender.ApplyRenderBatch: real path. BenchmarkDotNet, 3 interleaved rounds, mean of each round:*R1 main small had high variance (median 1.273 µs).
WebAssembly out-of-process sequence: the real
UpdateDisplayOutOfProcessneeds browser JS interop, so this benchmark reproduces its exact serialize +ToArraysteps (UTF-16 string table) on CoreCLR, main pattern vs PR pattern:*outlier round.
Tests: Components.Server 396, Components.WebView 16 and Components.WebAssembly 56 all pass.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)