[dotnet] [bidi] ~Zero allocation per command/event - #17214
Conversation
Review Summary by QodoImplement pooled buffers for zero-allocation BiDi messaging
WalkthroughsDescription• Implement pooled buffer writers for zero-allocation message handling • Replace byte array allocations with reusable buffer pools • Update transport interface to use IBufferWriter pattern • Enhance trace-level logging for BiDi commands and events File Changes1. dotnet/src/webdriver/BiDi/Broker.cs
|
Code Review by Qodo
1.
|
There was a problem hiding this comment.
Pull request overview
This PR refactors the .NET BiDi transport layer to reduce per-message allocations by switching from byte[]-returning receive APIs to writing into a pooled IBufferWriter<byte> and by pooling JSON serialization buffers for outbound messages.
Changes:
- Updated
ITransportto receive into anIBufferWriter<byte>and adjusted send to useTask. - Reworked
WebSocketTransport.ReceiveAsyncto write directly into a provided buffer instead of allocating/aggregating via aMemoryStream. - Added a pooled buffer writer in
Brokerand switched outbound command serialization toUtf8JsonWriterover pooled buffers (plus adjusted trace logging).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
dotnet/src/webdriver/BiDi/WebSocketTransport.cs |
Refactors receive/send signatures and implements buffered receive into an IBufferWriter<byte>. |
dotnet/src/webdriver/BiDi/ITransport.cs |
Updates the transport interface to support pooled/buffered receive and Task-based send. |
dotnet/src/webdriver/BiDi/Broker.cs |
Pools outbound/inbound buffers and switches command serialization to write into a pooled buffer. |


This is big improvement how we use memory.
💥 What does this PR do?
This pull request introduces a significant refactor to the BiDi transport and broker layers, focusing on performance improvements and memory management. The main changes include replacing byte array and memory stream usage with pooled buffer writers, updating interfaces for more efficient message handling, and enhancing logging for trace-level diagnostics.
These changes collectively improve the efficiency, reliability, and maintainability of the BiDi transport and broker components.
🔄 Types of changes